Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
+
|
6 |
+
# ๐น Download & load the model from Hugging Face
|
7 |
+
model_name = "HyperX-Sen/Qwen-2.5-7B-Reasoning"
|
8 |
+
model_path = snapshot_download(repo_id=model_name, repo_type="model")
|
9 |
+
|
10 |
+
# ๐น Load the model & tokenizer
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
13 |
+
|
14 |
+
# ๐น System prompt
|
15 |
+
SYSTEM_PROMPT = """
|
16 |
+
Respond in the following format:
|
17 |
+
<reasoning>
|
18 |
+
...
|
19 |
+
</reasoning>
|
20 |
+
<answer>
|
21 |
+
...
|
22 |
+
</answer>
|
23 |
+
"""
|
24 |
+
|
25 |
+
# ๐น Function to generate response
|
26 |
+
def chat_response(user_input, top_p, top_k, temperature, max_length):
|
27 |
+
messages = [
|
28 |
+
{"role": "system", "content": f"{SYSTEM_PROMPT}"},
|
29 |
+
{"role": "user", "content": user_input}
|
30 |
+
]
|
31 |
+
|
32 |
+
# ๐น Format & tokenize input
|
33 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
34 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
35 |
+
|
36 |
+
# ๐น Generate response
|
37 |
+
with torch.no_grad():
|
38 |
+
output = model.generate(
|
39 |
+
**inputs,
|
40 |
+
max_length=max_length,
|
41 |
+
do_sample=True,
|
42 |
+
top_p=top_p,
|
43 |
+
top_k=top_k,
|
44 |
+
temperature=temperature
|
45 |
+
)
|
46 |
+
|
47 |
+
# ๐น Decode output
|
48 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
49 |
+
return response
|
50 |
+
|
51 |
+
# ๐น Gradio UI
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("# ๐ค Qwen-2.5-7B-Reasoning Chatbot")
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
chatbot = gr.Textbox(label="Model Response", lines=8, interactive=False)
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
user_input = gr.Textbox(label="Your Prompt", placeholder="Ask me anything...", lines=2)
|
60 |
+
|
61 |
+
with gr.Accordion("๐ง Advanced Settings", open=False):
|
62 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, label="Top-p")
|
63 |
+
top_k = gr.Slider(1, 100, value=50, label="Top-k")
|
64 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
|
65 |
+
max_length = gr.Slider(128, 1024, value=512, label="Max Length")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
submit_button = gr.Button("Generate Response")
|
69 |
+
|
70 |
+
submit_button.click(chat_response, inputs=[user_input, top_p, top_k, temperature, max_length], outputs=[chatbot])
|
71 |
+
|
72 |
+
# ๐น Launch the Gradio app
|
73 |
+
demo.launch()
|