Spaces:
Running
Running
Upload 2 files
Browse files- app.py +18 -35
- requirements.txt +2 -2
app.py
CHANGED
@@ -3,48 +3,31 @@ 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(
|
|
|
|
|
|
|
|
|
17 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
|
19 |
examples = [
|
20 |
-
"
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"
|
24 |
-
"Generate a CSS style for a responsive navbar."
|
25 |
]
|
26 |
|
27 |
with gr.Blocks() as demo:
|
28 |
-
gr.Markdown("## 💻
|
29 |
-
prompt = gr.Textbox(label="Enter your
|
30 |
-
output = gr.Textbox(label="Generated
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
|
|
3 |
import torch
|
4 |
|
5 |
model_name = "deepseek-ai/deepseek-coder-1.3b-base"
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def generate_code(prompt):
|
10 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
11 |
+
outputs = model.generate(
|
12 |
+
**inputs,
|
13 |
+
max_new_tokens=300,
|
14 |
+
pad_token_id=tokenizer.eos_token_id
|
15 |
+
)
|
16 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
17 |
|
18 |
examples = [
|
19 |
+
"Create a Python function to reverse a string.",
|
20 |
+
"Write a JavaScript function that returns the factorial of a number.",
|
21 |
+
"Build a simple HTML page with a form and a submit button.",
|
22 |
+
"Create a Python script to fetch weather data using an API."
|
|
|
23 |
]
|
24 |
|
25 |
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown("## 💻 Generate Code with DeepSeek")
|
27 |
+
prompt = gr.Textbox(label="Enter your prompt", lines=4, scale=2)
|
28 |
+
output = gr.Textbox(label="Generated code", lines=10)
|
29 |
+
gen_button = gr.Button("Generate")
|
30 |
+
gen_button.click(fn=generate_code, inputs=prompt, outputs=output)
|
31 |
+
gr.Examples(examples=examples, inputs=prompt)
|
32 |
+
|
33 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
transformers
|
2 |
torch
|
3 |
-
|
4 |
-
|
|
|
1 |
transformers
|
2 |
torch
|
3 |
+
accelerate
|
4 |
+
gradio
|