Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import spaces
|
5 |
+
|
6 |
+
# Initialize the model and tokenizer
|
7 |
+
model_name = "Qwen/Qwen2.5-Math-1.5B-Instruct"
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
11 |
+
model_name,
|
12 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
13 |
+
device_map="auto" if device == "cuda" else None
|
14 |
+
)
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
|
17 |
+
# System instruction
|
18 |
+
SYSTEM_INSTRUCTION = (
|
19 |
+
"You are a math tutor providing hints and guidance. "
|
20 |
+
"Do not reveal final answers. Offer step-by-step assistance only."
|
21 |
+
)
|
22 |
+
|
23 |
+
def apply_chat_template(messages):
|
24 |
+
"""
|
25 |
+
Prepares the messages for the model using the tokenizer's chat template.
|
26 |
+
"""
|
27 |
+
return tokenizer.apply_chat_template(
|
28 |
+
messages,
|
29 |
+
tokenize=False,
|
30 |
+
add_generation_prompt=True
|
31 |
+
)
|
32 |
+
|
33 |
+
@spaces.GPU
|
34 |
+
def generate_response(history, user_input):
|
35 |
+
"""
|
36 |
+
Generates a response from the model based on the chat history and user input.
|
37 |
+
"""
|
38 |
+
# Append user input to the chat history
|
39 |
+
history.append({"role": "user", "content": user_input})
|
40 |
+
|
41 |
+
# Build messages for the model
|
42 |
+
messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + history
|
43 |
+
|
44 |
+
# Tokenize input for the model
|
45 |
+
text = apply_chat_template(messages)
|
46 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
47 |
+
|
48 |
+
# Generate response
|
49 |
+
generated_ids = model.generate(
|
50 |
+
**model_inputs,
|
51 |
+
max_new_tokens=512
|
52 |
+
)
|
53 |
+
generated_ids = [
|
54 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
55 |
+
]
|
56 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
57 |
+
|
58 |
+
# Append the assistant's response to history
|
59 |
+
history.append({"role": "assistant", "content": response})
|
60 |
+
|
61 |
+
# Format the conversation for display
|
62 |
+
formatted_history = format_chat_history(history)
|
63 |
+
|
64 |
+
return formatted_history, history
|
65 |
+
|
66 |
+
def format_chat_history(history):
|
67 |
+
"""
|
68 |
+
Formats the conversation history for a user-friendly chat display.
|
69 |
+
"""
|
70 |
+
chat_display = ""
|
71 |
+
for message in history:
|
72 |
+
if message["role"] == "user":
|
73 |
+
chat_display += f"**User:** {message['content']}\n\n"
|
74 |
+
elif message["role"] == "assistant":
|
75 |
+
chat_display += f"**MathTutor:** {message['content']}\n\n"
|
76 |
+
return chat_display
|
77 |
+
|
78 |
+
# Gradio chat interface
|
79 |
+
def create_chat_interface():
|
80 |
+
"""
|
81 |
+
Creates the Gradio interface for the chat application.
|
82 |
+
"""
|
83 |
+
with gr.Blocks() as chat_app:
|
84 |
+
gr.Markdown("## Math Hint Chat")
|
85 |
+
gr.Markdown(
|
86 |
+
"This chat application helps with math problems by providing hints and guidance. "
|
87 |
+
"It keeps a history of your conversation and ensures no direct answers are given."
|
88 |
+
)
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column():
|
92 |
+
user_input = gr.Textbox(
|
93 |
+
label="Your Math Query",
|
94 |
+
placeholder="Ask about a math problem (e.g., Solve for x: 4x + 5 = 6x + 7)",
|
95 |
+
lines=2
|
96 |
+
)
|
97 |
+
send_button = gr.Button("Send")
|
98 |
+
with gr.Column():
|
99 |
+
chat_history = gr.Textbox(
|
100 |
+
label="Chat History",
|
101 |
+
placeholder="Chat history will appear here.",
|
102 |
+
lines=20,
|
103 |
+
interactive=False
|
104 |
+
)
|
105 |
+
|
106 |
+
# Hidden state for storing conversation history
|
107 |
+
history_state = gr.State([])
|
108 |
+
|
109 |
+
# Button interaction
|
110 |
+
send_button.click(
|
111 |
+
fn=generate_response,
|
112 |
+
inputs=[history_state, user_input],
|
113 |
+
outputs=[chat_history, history_state]
|
114 |
+
)
|
115 |
+
|
116 |
+
return chat_app
|
117 |
+
|
118 |
+
|
119 |
+
app = create_chat_interface()
|
120 |
+
app.launch(debug=True)
|