Upload 2 files
Browse files- app.py +52 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Set model ID
|
6 |
+
# comment out the model you want to use
|
7 |
+
# model_id = "gpt2" # for testing purposes only
|
8 |
+
# model_id = "deepseek-ai/deepseek-coder-1.3b"
|
9 |
+
# model_id = "deepseek-ai/deepseek-coder-1.3b-base"
|
10 |
+
model_id = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
11 |
+
|
12 |
+
|
13 |
+
# Load tokenizer and model
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
15 |
+
model = AutoModelForCausalLM.from_pretrained(
|
16 |
+
model_id,
|
17 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
18 |
+
)
|
19 |
+
|
20 |
+
# Move model to GPU if available
|
21 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
model.to(device)
|
23 |
+
|
24 |
+
def generate_code(prompt):
|
25 |
+
if not prompt.strip():
|
26 |
+
return "⚠ Please enter a valid prompt."
|
27 |
+
|
28 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
29 |
+
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = model.generate(
|
32 |
+
**inputs,
|
33 |
+
max_new_tokens=200,
|
34 |
+
temperature=0.7
|
35 |
+
)
|
36 |
+
|
37 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
+
|
39 |
+
# Strip the prompt if it appears at the start
|
40 |
+
if output_text.startswith(prompt):
|
41 |
+
output_text = output_text[len(prompt):].lstrip()
|
42 |
+
|
43 |
+
return output_text
|
44 |
+
|
45 |
+
demo = gr.Interface(
|
46 |
+
fn=generate_code,
|
47 |
+
inputs=gr.Textbox(lines=5, label="Enter Prompt"),
|
48 |
+
outputs=gr.Textbox(label="Generated Output"),
|
49 |
+
title="Code Generator using DeepSeek"
|
50 |
+
)
|
51 |
+
|
52 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio
|