Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
| 7 |
+
print("Loading model...")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
torch_dtype=torch.float32
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def generate_code(prompt, max_length=512, temperature=0.7):
|
| 16 |
+
"""Generate code based on user prompt"""
|
| 17 |
+
# Format for instruct model
|
| 18 |
+
formatted_prompt = f"### Instruction:
|
| 19 |
+
{prompt}
|
| 20 |
+
|
| 21 |
+
### Response:
|
| 22 |
+
"
|
| 23 |
+
|
| 24 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt")
|
| 25 |
+
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
outputs = model.generate(
|
| 28 |
+
**inputs,
|
| 29 |
+
max_new_tokens=max_length,
|
| 30 |
+
temperature=temperature,
|
| 31 |
+
do_sample=True,
|
| 32 |
+
top_p=0.95,
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
# Extract only the generated part
|
| 38 |
+
response = response.split("### Response:")[-1].strip()
|
| 39 |
+
|
| 40 |
+
return response
|
| 41 |
+
|
| 42 |
+
# Create Gradio interface
|
| 43 |
+
demo = gr.Interface(
|
| 44 |
+
fn=generate_code,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Textbox(
|
| 47 |
+
label="Enter your coding prompt",
|
| 48 |
+
placeholder="Write a Python function to calculate fibonacci numbers",
|
| 49 |
+
lines=5
|
| 50 |
+
),
|
| 51 |
+
gr.Slider(
|
| 52 |
+
minimum=128,
|
| 53 |
+
maximum=1024,
|
| 54 |
+
value=512,
|
| 55 |
+
step=64,
|
| 56 |
+
label="Max Length"
|
| 57 |
+
),
|
| 58 |
+
gr.Slider(
|
| 59 |
+
minimum=0.1,
|
| 60 |
+
maximum=2.0,
|
| 61 |
+
value=0.7,
|
| 62 |
+
step=0.1,
|
| 63 |
+
label="Temperature"
|
| 64 |
+
)
|
| 65 |
+
],
|
| 66 |
+
outputs=gr.Textbox(label="Generated Code", lines=15),
|
| 67 |
+
title="🚀 DeepSeek Coder 1.3B - Code Assistant",
|
| 68 |
+
description="An AI coding assistant powered by DeepSeek-Coder-1.3B. Ask coding questions, request implementations, or get help debugging!",
|
| 69 |
+
examples=[
|
| 70 |
+
["Write a Python function to perform binary search"],
|
| 71 |
+
["Create a REST API endpoint using Flask for user authentication"],
|
| 72 |
+
["Write a quick sort algorithm in JavaScript"],
|
| 73 |
+
["Write a SQL query to find duplicate records in a users table"]
|
| 74 |
+
],
|
| 75 |
+
theme=gr.themes.Soft()
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|