Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
model_name = "Qwen/Qwen3-4B-Instruct-2507"
|
6 |
+
|
7 |
+
# Load the tokenizer and the model
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype="auto",
|
12 |
+
device_map="auto"
|
13 |
+
)
|
14 |
+
|
15 |
+
@spaces.GPU(duration=120)
|
16 |
+
def generate_response(prompt):
|
17 |
+
# Prepare the model input
|
18 |
+
messages = [
|
19 |
+
{"role": "user", "content": prompt}
|
20 |
+
]
|
21 |
+
text = tokenizer.apply_chat_template(
|
22 |
+
messages,
|
23 |
+
tokenize=False,
|
24 |
+
add_generation_prompt=True,
|
25 |
+
)
|
26 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
27 |
+
|
28 |
+
# Conduct text completion
|
29 |
+
generated_ids = model.generate(
|
30 |
+
**model_inputs,
|
31 |
+
max_new_tokens=1024 # Reduced for performance and safety
|
32 |
+
)
|
33 |
+
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
34 |
+
|
35 |
+
content = tokenizer.decode(output_ids, skip_special_tokens=True)
|
36 |
+
return content
|
37 |
+
|
38 |
+
# Create Gradio interface
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
gr.Markdown("# Qwen Chatbot")
|
41 |
+
chatbot = gr.Chatbot()
|
42 |
+
msg = gr.Textbox(label="Input")
|
43 |
+
clear = gr.Button("Clear")
|
44 |
+
|
45 |
+
def respond(message, chat_history):
|
46 |
+
if not message:
|
47 |
+
return "", chat_history
|
48 |
+
|
49 |
+
bot_response = generate_response(message)
|
50 |
+
chat_history.append((message, bot_response))
|
51 |
+
return "", chat_history
|
52 |
+
|
53 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
54 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
55 |
+
|
56 |
+
# Launch the app
|
57 |
+
demo.launch()
|