Spaces:
Sleeping
Sleeping
walidsobhie-code commited on
Commit ·
f39e166
1
Parent(s): 969a9d8
Fix: Replace Slider with Number, remove Examples to avoid caching error
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
Stack 2.9 - HuggingFace Space
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -19,7 +19,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 19 |
)
|
| 20 |
print("Model loaded!")
|
| 21 |
|
| 22 |
-
def generate(prompt, max_tokens
|
| 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")
|
|
@@ -27,8 +27,8 @@ def generate(prompt, max_tokens=256, temperature=0.7):
|
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model.generate(
|
| 29 |
**inputs,
|
| 30 |
-
max_new_tokens=max_tokens,
|
| 31 |
-
temperature=temperature,
|
| 32 |
do_sample=True,
|
| 33 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 34 |
)
|
|
@@ -39,19 +39,13 @@ def generate(prompt, max_tokens=256, temperature=0.7):
|
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=generate,
|
| 41 |
inputs=[
|
| 42 |
-
gr.Textbox(label="Prompt", placeholder="Write a Python function to calculate fibonacci...", lines=
|
| 43 |
-
gr.
|
| 44 |
-
gr.
|
| 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)
|
|
|
|
| 1 |
"""
|
| 2 |
Stack 2.9 - HuggingFace Space
|
| 3 |
+
Gradio 6.x compatible
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 19 |
)
|
| 20 |
print("Model loaded!")
|
| 21 |
|
| 22 |
+
def generate(prompt, max_tokens, temperature):
|
| 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")
|
|
|
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model.generate(
|
| 29 |
**inputs,
|
| 30 |
+
max_new_tokens=int(max_tokens),
|
| 31 |
+
temperature=float(temperature),
|
| 32 |
do_sample=True,
|
| 33 |
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 34 |
)
|
|
|
|
| 39 |
demo = gr.Interface(
|
| 40 |
fn=generate,
|
| 41 |
inputs=[
|
| 42 |
+
gr.Textbox(label="Prompt", placeholder="Write a Python function to calculate fibonacci...", lines=4),
|
| 43 |
+
gr.Number(label="Max tokens", value=256, minimum=64, maximum=512),
|
| 44 |
+
gr.Number(label="Temperature", value=0.7, minimum=0.1, maximum=1.0),
|
| 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 |
)
|
| 50 |
|
| 51 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|