Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,17 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# Model
|
| 6 |
-
model_name = "deepseek-ai/deepseek-coder-1.3b-
|
| 7 |
|
| 8 |
# Load tokenizer
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 10 |
|
| 11 |
-
# Load model (
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_name,
|
| 14 |
-
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True
|
| 16 |
)
|
| 17 |
|
|
@@ -23,31 +24,51 @@ def generate_code(prompt):
|
|
| 23 |
if not prompt.strip():
|
| 24 |
return "Please enter a prompt."
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
with torch.no_grad():
|
| 29 |
outputs = model.generate(
|
| 30 |
**inputs,
|
| 31 |
-
max_new_tokens=
|
| 32 |
-
temperature=0.
|
|
|
|
| 33 |
do_sample=True,
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return result
|
| 39 |
|
| 40 |
-
#
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=generate_code,
|
| 43 |
inputs=gr.Textbox(
|
| 44 |
lines=5,
|
| 45 |
-
placeholder="
|
| 46 |
),
|
| 47 |
-
outputs=gr.Textbox(lines=
|
| 48 |
-
title="DeepSeek Coder
|
| 49 |
-
description="
|
| 50 |
)
|
| 51 |
|
| 52 |
-
# Launch app
|
| 53 |
iface.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Model
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
| 7 |
|
| 8 |
# Load tokenizer
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 11 |
|
| 12 |
+
# Load model (CPU optimized)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
model_name,
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
low_cpu_mem_usage=True
|
| 17 |
)
|
| 18 |
|
|
|
|
| 24 |
if not prompt.strip():
|
| 25 |
return "Please enter a prompt."
|
| 26 |
|
| 27 |
+
formatted_prompt = f"""You are a professional programmer.
|
| 28 |
+
Write clean, complete, and correct code.
|
| 29 |
+
|
| 30 |
+
Instruction:
|
| 31 |
+
{prompt}
|
| 32 |
+
|
| 33 |
+
Response:
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
inputs = tokenizer(
|
| 37 |
+
formatted_prompt,
|
| 38 |
+
return_tensors="pt",
|
| 39 |
+
truncation=True,
|
| 40 |
+
max_length=512
|
| 41 |
+
)
|
| 42 |
|
| 43 |
with torch.no_grad():
|
| 44 |
outputs = model.generate(
|
| 45 |
**inputs,
|
| 46 |
+
max_new_tokens=120,
|
| 47 |
+
temperature=0.2,
|
| 48 |
+
top_p=0.9,
|
| 49 |
do_sample=True,
|
| 50 |
+
repetition_penalty=1.2,
|
| 51 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 52 |
+
pad_token_id=tokenizer.eos_token_id
|
| 53 |
)
|
| 54 |
|
| 55 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
# Clean output
|
| 58 |
+
result = output_text.replace(formatted_prompt, "").strip()
|
| 59 |
+
|
| 60 |
return result
|
| 61 |
|
| 62 |
+
# UI
|
| 63 |
iface = gr.Interface(
|
| 64 |
fn=generate_code,
|
| 65 |
inputs=gr.Textbox(
|
| 66 |
lines=5,
|
| 67 |
+
placeholder="Example: Create a login page using HTML and CSS"
|
| 68 |
),
|
| 69 |
+
outputs=gr.Textbox(lines=12),
|
| 70 |
+
title="DeepSeek Coder AI (Optimized)",
|
| 71 |
+
description="Code generator running on Hugging Face Spaces (CPU optimized)."
|
| 72 |
)
|
| 73 |
|
|
|
|
| 74 |
iface.launch()
|