Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,21 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
MODEL_NAME = "Qwen/Qwen2.5-Coder-
|
| 6 |
|
| 7 |
-
print("Loading model...
|
| 8 |
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_NAME,
|
|
|
|
| 13 |
torch_dtype=torch.float32,
|
| 14 |
-
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
print("Model loaded!")
|
| 18 |
|
| 19 |
|
|
@@ -21,28 +24,38 @@ def generate_code(prompt):
|
|
| 21 |
if not prompt.strip():
|
| 22 |
return "Please enter a prompt."
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
with torch.no_grad():
|
| 27 |
outputs = model.generate(
|
| 28 |
**inputs,
|
| 29 |
-
max_new_tokens=
|
| 30 |
-
|
| 31 |
-
do_sample=True
|
| 32 |
)
|
| 33 |
|
| 34 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
if result
|
| 38 |
-
result = result
|
| 39 |
|
| 40 |
return result.strip()
|
| 41 |
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("# π»
|
| 45 |
-
gr.Markdown("
|
| 46 |
|
| 47 |
prompt = gr.Textbox(
|
| 48 |
label="Your Prompt",
|
|
@@ -50,13 +63,19 @@ with gr.Blocks() as demo:
|
|
| 50 |
lines=4
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
output = gr.Code(
|
| 56 |
label="Generated Code",
|
| 57 |
language="python"
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
MODEL_NAME = "Qwen/Qwen2.5-Coder-0.5B-Instruct"
|
| 6 |
|
| 7 |
+
print("Loading model...")
|
| 8 |
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_NAME,
|
| 13 |
+
device_map="cpu",
|
| 14 |
torch_dtype=torch.float32,
|
| 15 |
+
low_cpu_mem_usage=True
|
| 16 |
)
|
| 17 |
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
print("Model loaded!")
|
| 21 |
|
| 22 |
|
|
|
|
| 24 |
if not prompt.strip():
|
| 25 |
return "Please enter a prompt."
|
| 26 |
|
| 27 |
+
messages = [
|
| 28 |
+
{"role": "system", "content": "You are a helpful coding assistant. Output only clean code without explanations."},
|
| 29 |
+
{"role": "user", "content": prompt}
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
text = tokenizer.apply_chat_template(
|
| 33 |
+
messages,
|
| 34 |
+
tokenize=False,
|
| 35 |
+
add_generation_prompt=True
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 39 |
|
| 40 |
with torch.no_grad():
|
| 41 |
outputs = model.generate(
|
| 42 |
**inputs,
|
| 43 |
+
max_new_tokens=128, # keeps it fast
|
| 44 |
+
do_sample=False # faster + more stable
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
|
| 49 |
+
# Extract only assistant output
|
| 50 |
+
if "assistant" in result:
|
| 51 |
+
result = result.split("assistant")[-1]
|
| 52 |
|
| 53 |
return result.strip()
|
| 54 |
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# π» AI Code Generator (Local CPU)")
|
| 58 |
+
gr.Markdown("Fast, simple, and runs fully locally π")
|
| 59 |
|
| 60 |
prompt = gr.Textbox(
|
| 61 |
label="Your Prompt",
|
|
|
|
| 63 |
lines=4
|
| 64 |
)
|
| 65 |
|
| 66 |
+
generate_btn = gr.Button("Generate Code")
|
| 67 |
|
| 68 |
output = gr.Code(
|
| 69 |
label="Generated Code",
|
| 70 |
language="python"
|
| 71 |
)
|
| 72 |
|
| 73 |
+
generate_btn.click(
|
| 74 |
+
fn=generate_code,
|
| 75 |
+
inputs=prompt,
|
| 76 |
+
outputs=output
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
gr.Markdown("π Use the built-in copy button in the code box!")
|
| 80 |
|
| 81 |
demo.launch()
|