Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import spaces
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
4 |
import torch
|
5 |
from threading import Thread
|
|
|
6 |
|
7 |
phi4_model_path = "Daemontatox/Qwen3-14B-Griffon"
|
8 |
|
@@ -11,6 +12,16 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
11 |
phi4_model = AutoModelForCausalLM.from_pretrained(phi4_model_path, device_map="auto", torch_dtype="auto")
|
12 |
phi4_tokenizer = AutoTokenizer.from_pretrained(phi4_model_path)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
@spaces.GPU(duration=120)
|
15 |
def generate_response(user_message, max_tokens, temperature, top_k, top_p, repetition_penalty, history_state):
|
16 |
if not user_message.strip():
|
@@ -24,7 +35,7 @@ def generate_response(user_message, max_tokens, temperature, top_k, top_p, repet
|
|
24 |
end_tag = "<|im_end|>"
|
25 |
|
26 |
# Recommended prompt settings by Microsoft
|
27 |
-
system_message = "Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> {Thought section} </think> {Solution section}. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion. Now, try to solve the following question through the above guidelines:"
|
28 |
prompt = f"{start_tag}system{sep_tag}{system_message}{end_tag}"
|
29 |
for message in history_state:
|
30 |
if message["role"] == "user":
|
@@ -64,7 +75,9 @@ def generate_response(user_message, max_tokens, temperature, top_k, top_p, repet
|
|
64 |
for new_token in streamer:
|
65 |
cleaned_token = new_token.replace("<|im_start|>", "").replace("<|im_sep|>", "").replace("<|im_end|>", "")
|
66 |
assistant_response += cleaned_token
|
67 |
-
|
|
|
|
|
68 |
yield new_history, new_history
|
69 |
|
70 |
yield new_history, new_history
|
@@ -72,13 +85,26 @@ def generate_response(user_message, max_tokens, temperature, top_k, top_p, repet
|
|
72 |
example_messages = {
|
73 |
"Math reasoning": "If a rectangular prism has a length of 6 cm, a width of 4 cm, and a height of 5 cm, what is the length of the longest line segment that can be drawn from one vertex to another?",
|
74 |
"Logic puzzle": "Four people (Alex, Blake, Casey, and Dana) each have a different favorite color (red, blue, green, yellow) and a different favorite fruit (apple, banana, cherry, date). Given the following clues: 1) The person who likes red doesn't like dates. 2) Alex likes yellow. 3) The person who likes blue likes cherries. 4) Blake doesn't like apples or bananas. 5) Casey doesn't like yellow or green. Who likes what color and what fruit?",
|
75 |
-
"Physics problem": "A ball is thrown upward with an initial velocity of 15 m/s from a height of 2 meters above the ground. Assuming the acceleration due to gravity is 9.8 m/s², determine: 1) The maximum height the ball reaches. 2) The total time the ball is in the air before hitting the ground. 3) The velocity with which the ball hits the ground."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
}
|
|
|
77 |
|
78 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
79 |
gr.Markdown(
|
80 |
"""
|
81 |
-
#
|
82 |
"""
|
83 |
)
|
84 |
|
@@ -122,7 +148,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
122 |
)
|
123 |
|
124 |
with gr.Column(scale=4):
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
with gr.Row():
|
127 |
user_input = gr.Textbox(
|
128 |
label="Your message",
|
@@ -136,6 +170,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
136 |
example1_button = gr.Button("Math reasoning")
|
137 |
example2_button = gr.Button("Logic puzzle")
|
138 |
example3_button = gr.Button("Physics problem")
|
|
|
139 |
|
140 |
submit_button.click(
|
141 |
fn=generate_response,
|
@@ -168,5 +203,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
168 |
inputs=None,
|
169 |
outputs=user_input
|
170 |
)
|
|
|
|
|
|
|
|
|
|
|
171 |
|
172 |
demo.launch(ssr_mode=False)
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
4 |
import torch
|
5 |
from threading import Thread
|
6 |
+
import re
|
7 |
|
8 |
phi4_model_path = "Daemontatox/Qwen3-14B-Griffon"
|
9 |
|
|
|
12 |
phi4_model = AutoModelForCausalLM.from_pretrained(phi4_model_path, device_map="auto", torch_dtype="auto")
|
13 |
phi4_tokenizer = AutoTokenizer.from_pretrained(phi4_model_path)
|
14 |
|
15 |
+
# Function to process LaTeX in the output
|
16 |
+
def process_latex(text):
|
17 |
+
"""
|
18 |
+
Process LaTeX equations in text:
|
19 |
+
- Inline equations: $...$
|
20 |
+
- Block equations: $$...$$
|
21 |
+
"""
|
22 |
+
# No special processing needed as Gradio's markdown component supports LaTeX
|
23 |
+
return text
|
24 |
+
|
25 |
@spaces.GPU(duration=120)
|
26 |
def generate_response(user_message, max_tokens, temperature, top_k, top_p, repetition_penalty, history_state):
|
27 |
if not user_message.strip():
|
|
|
35 |
end_tag = "<|im_end|>"
|
36 |
|
37 |
# Recommended prompt settings by Microsoft
|
38 |
+
system_message = "Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracing, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: <think> {Thought section} </think> {Solution section}. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion. You can use LaTeX for mathematical expressions, enclosed in $ for inline equations and $$ for block equations. Now, try to solve the following question through the above guidelines:"
|
39 |
prompt = f"{start_tag}system{sep_tag}{system_message}{end_tag}"
|
40 |
for message in history_state:
|
41 |
if message["role"] == "user":
|
|
|
75 |
for new_token in streamer:
|
76 |
cleaned_token = new_token.replace("<|im_start|>", "").replace("<|im_sep|>", "").replace("<|im_end|>", "")
|
77 |
assistant_response += cleaned_token
|
78 |
+
# Process the response to handle LaTeX content
|
79 |
+
processed_response = process_latex(assistant_response.strip())
|
80 |
+
new_history[-1]["content"] = processed_response
|
81 |
yield new_history, new_history
|
82 |
|
83 |
yield new_history, new_history
|
|
|
85 |
example_messages = {
|
86 |
"Math reasoning": "If a rectangular prism has a length of 6 cm, a width of 4 cm, and a height of 5 cm, what is the length of the longest line segment that can be drawn from one vertex to another?",
|
87 |
"Logic puzzle": "Four people (Alex, Blake, Casey, and Dana) each have a different favorite color (red, blue, green, yellow) and a different favorite fruit (apple, banana, cherry, date). Given the following clues: 1) The person who likes red doesn't like dates. 2) Alex likes yellow. 3) The person who likes blue likes cherries. 4) Blake doesn't like apples or bananas. 5) Casey doesn't like yellow or green. Who likes what color and what fruit?",
|
88 |
+
"Physics problem": "A ball is thrown upward with an initial velocity of 15 m/s from a height of 2 meters above the ground. Assuming the acceleration due to gravity is 9.8 m/s², determine: 1) The maximum height the ball reaches. 2) The total time the ball is in the air before hitting the ground. 3) The velocity with which the ball hits the ground.",
|
89 |
+
"Math with LaTeX": "Solve the quadratic equation $ax^2 + bx + c = 0$ for $x$ using the quadratic formula. Then, solve the specific equation $3x^2 - 6x + 2 = 0$."
|
90 |
+
}
|
91 |
+
|
92 |
+
# Customize the Gradio CSS to enhance the LaTeX rendering
|
93 |
+
css = """
|
94 |
+
.math-container .katex {
|
95 |
+
font-size: 1.2em;
|
96 |
+
}
|
97 |
+
.math-block .katex {
|
98 |
+
font-size: 1.5em;
|
99 |
+
display: block;
|
100 |
+
margin: 1em 0;
|
101 |
}
|
102 |
+
"""
|
103 |
|
104 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
105 |
gr.Markdown(
|
106 |
"""
|
107 |
+
# Try the example problems below to see how the model breaks down complex reasoning problems with LaTeX support.
|
108 |
"""
|
109 |
)
|
110 |
|
|
|
148 |
)
|
149 |
|
150 |
with gr.Column(scale=4):
|
151 |
+
# Use Chatbot with latex_delimiters to enable LaTeX rendering
|
152 |
+
chatbot = gr.Chatbot(
|
153 |
+
label="Chat",
|
154 |
+
type="messages",
|
155 |
+
latex_delimiters=[
|
156 |
+
{"left": "$$", "right": "$$", "display": True},
|
157 |
+
{"left": "$", "right": "$", "display": False}
|
158 |
+
]
|
159 |
+
)
|
160 |
with gr.Row():
|
161 |
user_input = gr.Textbox(
|
162 |
label="Your message",
|
|
|
170 |
example1_button = gr.Button("Math reasoning")
|
171 |
example2_button = gr.Button("Logic puzzle")
|
172 |
example3_button = gr.Button("Physics problem")
|
173 |
+
example4_button = gr.Button("Math with LaTeX")
|
174 |
|
175 |
submit_button.click(
|
176 |
fn=generate_response,
|
|
|
203 |
inputs=None,
|
204 |
outputs=user_input
|
205 |
)
|
206 |
+
example4_button.click(
|
207 |
+
fn=lambda: gr.update(value=example_messages["Math with LaTeX"]),
|
208 |
+
inputs=None,
|
209 |
+
outputs=user_input
|
210 |
+
)
|
211 |
|
212 |
demo.launch(ssr_mode=False)
|