Spaces:
Running
Running
Upload 2 files
Browse files- app.py +50 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-base"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype=torch.float16,
|
11 |
+
device_map="auto"
|
12 |
+
)
|
13 |
+
|
14 |
+
def generate_code(prompt):
|
15 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
16 |
+
outputs = model.generate(**inputs, max_new_tokens=300, pad_token_id=tokenizer.eos_token_id)
|
17 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
examples = [
|
20 |
+
"Write a Python function to calculate the factorial of a number.",
|
21 |
+
"Generate a React component for a simple to-do list.",
|
22 |
+
"Create a SQL query to find the top 5 selling products.",
|
23 |
+
"Write a JavaScript function to validate email addresses.",
|
24 |
+
"Generate a CSS style for a responsive navbar."
|
25 |
+
]
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("## 💻 DeepSeek Code Generator")
|
29 |
+
prompt = gr.Textbox(label="Enter your code description here", lines=3)
|
30 |
+
output = gr.Textbox(label="Generated Code", lines=15)
|
31 |
+
generate_btn = gr.Button("Generate Code")
|
32 |
+
examples_box = gr.Column(visible=False)
|
33 |
+
toggle_examples_btn = gr.Button("Show Examples")
|
34 |
+
|
35 |
+
def generate_click(prompt_text):
|
36 |
+
return generate_code(prompt_text)
|
37 |
+
|
38 |
+
def toggle_examples():
|
39 |
+
return gr.update(visible=not examples_box.visible)
|
40 |
+
|
41 |
+
generate_btn.click(fn=generate_click, inputs=prompt, outputs=output)
|
42 |
+
toggle_examples_btn.click(fn=toggle_examples, inputs=None, outputs=examples_box)
|
43 |
+
|
44 |
+
with examples_box:
|
45 |
+
for ex in examples:
|
46 |
+
ex_btn = gr.Button(ex, variant="secondary", size="sm")
|
47 |
+
ex_btn.click(fn=lambda x=ex: x, inputs=None, outputs=prompt)
|
48 |
+
|
49 |
+
demo.launch()
|
50 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio
|
4 |
+
|