Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,50 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# 🔧 Choose a modern model for code generation
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
|
| 7 |
|
| 8 |
+
# Load tokenizer & model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 13 |
+
device_map="auto" # Automatically use GPU if available
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def generate_code(prompt, max_length=200, temperature=0.7, top_p=0.95):
|
| 17 |
+
if not prompt.strip():
|
| 18 |
+
return "⚠️ Please enter a prompt before generating code."
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_length=max_length,
|
| 24 |
+
temperature=temperature,
|
| 25 |
+
top_p=top_p,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
pad_token_id=tokenizer.eos_token_id
|
| 28 |
+
)
|
| 29 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
# 🎨 Gradio Interface
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("## 💻 Code Generation with DeepSeek Coder")
|
| 34 |
+
gr.Markdown("Enter a description of the code you want. Example: *Write a Python function to calculate Fibonacci numbers.*")
|
| 35 |
+
|
| 36 |
+
prompt = gr.Textbox(lines=6, label="Enter your prompt")
|
| 37 |
+
max_length = gr.Slider(50, 1024, value=200, step=10, label="Max Length")
|
| 38 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature")
|
| 39 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top P (Nucleus Sampling)")
|
| 40 |
+
|
| 41 |
+
output_box = gr.Textbox(lines=20, label="Generated Code")
|
| 42 |
+
generate_button = gr.Button("🚀 Generate Code")
|
| 43 |
+
|
| 44 |
+
generate_button.click(
|
| 45 |
+
fn=generate_code,
|
| 46 |
+
inputs=[prompt, max_length, temperature, top_p],
|
| 47 |
+
outputs=output_box
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|