Spaces:
Sleeping
Sleeping
walidsobhie-code commited on
Commit ·
969a9d8
1
Parent(s): 446d77e
Simplify for Gradio 6.x compatibility
Browse files- app.py +22 -53
- requirements.txt +5 -5
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Stack 2.9 - HuggingFace Space
|
| 3 |
-
|
| 4 |
-
Pinned to Gradio 4.12.0 for compatibility
|
| 5 |
"""
|
| 6 |
import gradio as gr
|
| 7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -9,26 +8,19 @@ import torch
|
|
| 9 |
|
| 10 |
print("Loading model...")
|
| 11 |
|
| 12 |
-
# Load model on CPU to fit free tier
|
| 13 |
MODEL_NAME = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
MODEL_NAME,
|
| 17 |
-
torch_dtype=torch.float32,
|
| 18 |
device_map="cpu",
|
| 19 |
trust_remote_code=True,
|
| 20 |
low_cpu_mem_usage=True
|
| 21 |
)
|
| 22 |
print("Model loaded!")
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
""
|
| 26 |
-
messages = [
|
| 27 |
-
{"role": "user", "content": prompt}
|
| 28 |
-
]
|
| 29 |
-
if system_prompt:
|
| 30 |
-
messages.insert(0, {"role": "system", "content": system_prompt})
|
| 31 |
-
|
| 32 |
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 33 |
inputs = tokenizer([text], return_tensors="pt")
|
| 34 |
|
|
@@ -44,45 +36,22 @@ def generate_code(prompt, system_prompt="You are a helpful coding assistant.", m
|
|
| 44 |
response = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
| 45 |
return response.strip()
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
with gr.Row():
|
| 65 |
-
max_tokens = gr.Slider(64, 512, value=256, label="Max tokens")
|
| 66 |
-
temp = gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
|
| 67 |
-
btn = gr.Button("Generate", variant="primary")
|
| 68 |
-
|
| 69 |
-
with gr.Column():
|
| 70 |
-
output = gr.Code(label="Generated code", language="python", lines=20)
|
| 71 |
-
clear = gr.Button("Clear")
|
| 72 |
-
|
| 73 |
-
gr.Examples(
|
| 74 |
-
examples=[
|
| 75 |
-
["Write a Python function to calculate fibonacci numbers"],
|
| 76 |
-
["Explain what this code does: def foo(x): return x * 2"],
|
| 77 |
-
["Write a SQL query to find duplicate emails"],
|
| 78 |
-
["How do I handle exceptions in Python?"],
|
| 79 |
-
],
|
| 80 |
-
inputs=prompt
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
btn.click(generate_code, [prompt, system, max_tokens, temp], output)
|
| 84 |
-
prompt.submit(generate_code, [prompt, system, max_tokens, temp], output)
|
| 85 |
-
clear.click(lambda: "", None, prompt)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
"""
|
| 2 |
Stack 2.9 - HuggingFace Space
|
| 3 |
+
Compatible with Gradio 6.x
|
|
|
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 8 |
|
| 9 |
print("Loading model...")
|
| 10 |
|
|
|
|
| 11 |
MODEL_NAME = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
MODEL_NAME,
|
| 15 |
+
torch_dtype=torch.float32,
|
| 16 |
device_map="cpu",
|
| 17 |
trust_remote_code=True,
|
| 18 |
low_cpu_mem_usage=True
|
| 19 |
)
|
| 20 |
print("Model loaded!")
|
| 21 |
|
| 22 |
+
def generate(prompt, max_tokens=256, temperature=0.7):
|
| 23 |
+
messages = [{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 25 |
inputs = tokenizer([text], return_tensors="pt")
|
| 26 |
|
|
|
|
| 36 |
response = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
| 37 |
return response.strip()
|
| 38 |
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
fn=generate,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Textbox(label="Prompt", placeholder="Write a Python function to calculate fibonacci...", lines=6),
|
| 43 |
+
gr.Slider(64, 512, value=256, label="Max tokens"),
|
| 44 |
+
gr.Slider(0.1, 1.0, value=0.7, label="Temperature"),
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Textbox(label="Response", lines=10),
|
| 47 |
+
title="Stack 2.9 Code Assistant",
|
| 48 |
+
description="Powered by Qwen2.5-Coder-1.5B",
|
| 49 |
+
examples=[
|
| 50 |
+
["Write a Python function to calculate fibonacci numbers"],
|
| 51 |
+
["Explain what this code does: def foo(x): return x * 2"],
|
| 52 |
+
["Write a SQL query to find duplicate emails"],
|
| 53 |
+
["How do I handle exceptions in Python?"],
|
| 54 |
+
]
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
gradio=
|
| 2 |
-
transformers=
|
| 3 |
-
torch=
|
| 4 |
-
accelerate=
|
| 5 |
-
sentencepiece=
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.40.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
accelerate>=0.25.0
|
| 5 |
+
sentencepiece>=0.2.0
|