Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,30 @@
|
|
1 |
-
# -----------------------------------------------------------
|
2 |
-
# Fathom-R1 14B Chatbot β per-user conversations version
|
3 |
-
# -----------------------------------------------------------
|
4 |
import gradio as gr
|
5 |
import spaces
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
7 |
-
import torch
|
8 |
from threading import Thread
|
|
|
|
|
9 |
from openai import OpenAI
|
10 |
-
import tiktoken
|
11 |
|
12 |
-
# ----------------------- OpenAI client ---------------------
|
13 |
client = OpenAI(
|
14 |
base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
|
15 |
-
api_key="hf_XXXXX"
|
16 |
)
|
17 |
|
18 |
-
# ------------------ helper / formatting --------------------
|
19 |
def format_math(text):
|
20 |
text = re.sub(r"\[(.*?)\]", r"$$\1$$", text, flags=re.DOTALL)
|
21 |
text = text.replace(r"\(", "$").replace(r"\)", "$")
|
22 |
return text
|
23 |
|
24 |
-
|
|
|
|
|
25 |
return str(uuid.uuid4())[:8]
|
26 |
|
27 |
-
|
|
|
28 |
|
29 |
-
# ------------------ generation -----------------------------
|
30 |
def generate_response(user_message,
|
31 |
max_tokens,
|
32 |
temperature,
|
@@ -45,7 +43,7 @@ def generate_response(user_message,
|
|
45 |
response = client.chat.completions.create(
|
46 |
model="tgi",
|
47 |
messages=messages,
|
48 |
-
max_tokens=int(max_tokens),
|
49 |
temperature=temperature,
|
50 |
top_p=top_p,
|
51 |
stream=True
|
@@ -76,46 +74,45 @@ def generate_response(user_message,
|
|
76 |
|
77 |
token_text = chunk.choices[0].delta.content
|
78 |
assistant_response += token_text
|
|
|
79 |
tokens_seen += len(enc.encode(token_text))
|
80 |
|
81 |
new_history[-1]["content"] = assistant_response.strip()
|
82 |
yield new_history, new_history
|
83 |
|
84 |
if tokens_seen >= token_budget:
|
85 |
-
break
|
86 |
except Exception:
|
87 |
pass
|
88 |
|
89 |
yield new_history, new_history
|
90 |
|
91 |
-
|
92 |
example_messages = {
|
93 |
-
"IIT-JEE 2024 Mathematics": "
|
94 |
-
"IIT-JEE 2025 Physics": "
|
95 |
-
"Goldman Sachs Interview Puzzle": "
|
96 |
-
"IIT-JEE 2025 Mathematics": "
|
97 |
}
|
98 |
|
99 |
-
# ===========================================================
|
100 |
-
# UI / Gradio
|
101 |
-
# ===========================================================
|
102 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
103 |
-
|
104 |
-
#
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
#
|
110 |
-
gr.HTML(
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
</div>
|
116 |
-
<h1 style="margin:0;">Fathom R1 14B Chatbot</h1>
|
117 |
</div>
|
118 |
-
|
|
|
|
|
|
|
119 |
|
120 |
with gr.Sidebar():
|
121 |
gr.Markdown("## Conversations")
|
@@ -124,20 +121,44 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
124 |
|
125 |
with gr.Row():
|
126 |
with gr.Column(scale=1):
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
gr.Markdown("### Settings")
|
129 |
-
max_tokens_slider = gr.Slider(6144, 32768, step=1024, value=16384, label="Max Tokens")
|
130 |
with gr.Accordion("Advanced Settings", open=True):
|
131 |
-
temperature_slider = gr.Slider(0.1, 2.0, value=0.6, label="Temperature")
|
132 |
-
top_p_slider
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
with gr.Column(scale=4):
|
|
|
135 |
chatbot = gr.Chatbot(label="Chat", type="messages", height=520)
|
136 |
with gr.Row():
|
137 |
-
user_input
|
138 |
with gr.Column():
|
139 |
submit_button = gr.Button("Send", variant="primary", scale=1)
|
140 |
-
clear_button
|
141 |
gr.Markdown("**Try these examples:**")
|
142 |
with gr.Row():
|
143 |
example1_button = gr.Button("IIT-JEE 2025 Mathematics")
|
@@ -145,17 +166,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
145 |
example3_button = gr.Button("Goldman Sachs Interview Puzzle")
|
146 |
example4_button = gr.Button("IIT-JEE 2024 Mathematics")
|
147 |
|
148 |
-
#
|
149 |
def update_conversation_list(conversations):
|
150 |
return [conversations[cid]["title"] for cid in conversations]
|
151 |
|
152 |
def start_new_conversation(conversations):
|
153 |
new_id = generate_conversation_id()
|
154 |
conversations[new_id] = {"title": f"New Conversation {new_id}", "messages": []}
|
155 |
-
return
|
156 |
-
gr.update(choices=update_conversation_list(conversations),
|
157 |
-
value=conversations[new_id]["title"]),
|
158 |
-
conversations) # updated dict
|
159 |
|
160 |
def load_conversation(selected_title, conversations):
|
161 |
for cid, convo in conversations.items():
|
@@ -163,30 +181,21 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
163 |
return cid, convo["messages"], convo["messages"]
|
164 |
return current_convo_id.value, history_state.value, history_state.value
|
165 |
|
166 |
-
def send_message(user_message, max_tokens, temperature, top_p,
|
167 |
-
convo_id, history, conversations):
|
168 |
-
|
169 |
if convo_id not in conversations:
|
|
|
170 |
title = " ".join(user_message.strip().split()[:5])
|
171 |
conversations[convo_id] = {"title": title, "messages": history}
|
172 |
-
|
173 |
if conversations[convo_id]["title"].startswith("New Conversation"):
|
|
|
174 |
conversations[convo_id]["title"] = " ".join(user_message.strip().split()[:5])
|
175 |
-
|
176 |
-
for updated_history, new_history in generate_response(
|
177 |
-
user_message, max_tokens, temperature, top_p, history):
|
178 |
conversations[convo_id]["messages"] = new_history
|
179 |
-
yield
|
180 |
-
new_history,
|
181 |
-
gr.update(choices=update_conversation_list(conversations),
|
182 |
-
value=conversations[convo_id]["title"]),
|
183 |
-
conversations) # updated dict each stream chunk
|
184 |
|
185 |
-
# ------------- UI bindings ----------------------
|
186 |
submit_button.click(
|
187 |
fn=send_message,
|
188 |
-
inputs=[user_input, max_tokens_slider, temperature_slider, top_p_slider,
|
189 |
-
current_convo_id, history_state, conversations_state],
|
190 |
outputs=[chatbot, history_state, conversation_selector, conversations_state],
|
191 |
concurrency_limit=16
|
192 |
).then(
|
@@ -213,16 +222,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
213 |
outputs=[current_convo_id, history_state, chatbot]
|
214 |
)
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
example4_button.click(lambda: gr.update(value=example_messages["IIT-JEE 2024 Mathematics"]),
|
224 |
-
None, user_input)
|
225 |
-
|
226 |
-
# If running as a Space, `share=True` can be removed.
|
227 |
if __name__ == "__main__":
|
|
|
228 |
demo.queue().launch(share=True, ssr_mode=False)
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
4 |
+
import torch
|
5 |
from threading import Thread
|
6 |
+
import re
|
7 |
+
import uuid
|
8 |
from openai import OpenAI
|
|
|
9 |
|
|
|
10 |
client = OpenAI(
|
11 |
base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
|
12 |
+
api_key="hf_XXXXX"
|
13 |
)
|
14 |
|
|
|
15 |
def format_math(text):
|
16 |
text = re.sub(r"\[(.*?)\]", r"$$\1$$", text, flags=re.DOTALL)
|
17 |
text = text.replace(r"\(", "$").replace(r"\)", "$")
|
18 |
return text
|
19 |
|
20 |
+
# --------- removed the old global conversations = {} ---------
|
21 |
+
|
22 |
+
def generate_conversation_id():
|
23 |
return str(uuid.uuid4())[:8]
|
24 |
|
25 |
+
import tiktoken
|
26 |
+
enc = tiktoken.encoding_for_model("gpt-3.5-turbo") # any OpenAI encoding works
|
27 |
|
|
|
28 |
def generate_response(user_message,
|
29 |
max_tokens,
|
30 |
temperature,
|
|
|
43 |
response = client.chat.completions.create(
|
44 |
model="tgi",
|
45 |
messages=messages,
|
46 |
+
max_tokens=int(max_tokens), # server-side limit
|
47 |
temperature=temperature,
|
48 |
top_p=top_p,
|
49 |
stream=True
|
|
|
74 |
|
75 |
token_text = chunk.choices[0].delta.content
|
76 |
assistant_response += token_text
|
77 |
+
# count how many tokens that piece is worth
|
78 |
tokens_seen += len(enc.encode(token_text))
|
79 |
|
80 |
new_history[-1]["content"] = assistant_response.strip()
|
81 |
yield new_history, new_history
|
82 |
|
83 |
if tokens_seen >= token_budget:
|
84 |
+
break # stop the local loop
|
85 |
except Exception:
|
86 |
pass
|
87 |
|
88 |
yield new_history, new_history
|
89 |
|
90 |
+
|
91 |
example_messages = {
|
92 |
+
"IIT-JEE 2024 Mathematics": "A student appears for a quiz consisting of only true-false type questions and answers all the questions. The student knows the answers of some questions and guesses the answers for the remaining questions. Whenever the student knows the answer of a question, he gives the correct answer. Assume that probability of the student giving the correct answer for a question, given that he has guessed it, is $\\frac{1}{2}$. Also assume that the probability of the answer for a question being guessed, given that the student's answer is correct, is $\\frac{1}{6}$. Then the probability that the student knows the answer of a randomly chosen question is?",
|
93 |
+
"IIT-JEE 2025 Physics": "A person sitting inside an elevator performs a weighing experiment with an object of mass 50 kg. Suppose that the variation of the height π¦ (in m) of the elevator, from the ground, with time π‘ (in s) is given by π¦ = 8 [1 + sin ( 2ππ‘/π )], where π = 40π s. Taking acceleration due to gravity, π = 10 m/s^2 , the maximum variation of the objectβs weight (in N) as observed in the experiment is ?",
|
94 |
+
"Goldman Sachs Interview Puzzle": "Four friends need to cross a dangerous bridge at night. Unfortunately, they have only one torch and the bridge is too dangerous to cross without one. The bridge is only strong enough to support two people at a time. Not all people take the same time to cross the bridge. Times for each person: 1 min, 2 mins, 7 mins and 10 mins. What is the shortest time needed for all four of them to cross the bridge?",
|
95 |
+
"IIT-JEE 2025 Mathematics": "Let π be the set of all seven-digit numbers that can be formed using the digits 0, 1 and 2. For example, 2210222 is in π, but 0210222 is NOT in π.Then the number of elements π₯ in π such that at least one of the digits 0 and 1 appears exactly twice in π₯, is ?"
|
96 |
}
|
97 |
|
|
|
|
|
|
|
98 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
99 |
+
# --------- session-scoped states ---------
|
100 |
+
conversations_state = gr.State({}) # NEW: one dict per user
|
101 |
+
current_convo_id = gr.State(generate_conversation_id())
|
102 |
+
history_state = gr.State([])
|
103 |
+
|
104 |
+
# Global heading stays at top
|
105 |
+
#gr.Markdown("# Fathom R1 14B Chatbot")
|
106 |
+
gr.HTML(
|
107 |
+
"""
|
108 |
+
<div style="display: flex; align-items: center; gap: 16px; margin-bottom: 1em;">
|
109 |
+
<div style="background-color: black; padding: 6px; border-radius: 8px;">
|
110 |
+
<img src="https://framerusercontent.com/images/j0KjQQyrUfkFw4NwSaxQOLAoBU.png" alt="Fractal AI Logo" style="height: 48px;">
|
|
|
|
|
111 |
</div>
|
112 |
+
<h1 style="margin: 0;">Fathom R1 14B Chatbot</h1>
|
113 |
+
</div>
|
114 |
+
"""
|
115 |
+
)
|
116 |
|
117 |
with gr.Sidebar():
|
118 |
gr.Markdown("## Conversations")
|
|
|
121 |
|
122 |
with gr.Row():
|
123 |
with gr.Column(scale=1):
|
124 |
+
# INTRO TEXT MOVED HERE
|
125 |
+
gr.Markdown(
|
126 |
+
"""
|
127 |
+
Welcome to the Fathom R1 14B Chatbot, developed by Fractal AI Research!
|
128 |
+
|
129 |
+
Our model excels at reasoning tasks in mathematics and science. Given that our model has been optimised for tasks requiring critical thinking, it might overthink for simple chat queries.
|
130 |
+
|
131 |
+
To check out our GitHub repository, click [here](https://github.com/FractalAIResearchLabs/Fathom-R1)
|
132 |
+
|
133 |
+
For training recipe details on how this model was built, please check [here](https://huggingface.co/FractalAIResearch/Fathom-R1-14B)
|
134 |
+
|
135 |
+
Try the example problems below from various popular entrance examinations and interviews or type in your own problems to see how our model breaks down and solves complex reasoning problems.
|
136 |
+
|
137 |
+
NOTE: Once you close this demo window, all currently saved conversations will be lost.
|
138 |
+
"""
|
139 |
+
)
|
140 |
+
|
141 |
gr.Markdown("### Settings")
|
142 |
+
max_tokens_slider = gr.Slider(minimum=6144, maximum=32768, step=1024, value=16384, label="Max Tokens")
|
143 |
with gr.Accordion("Advanced Settings", open=True):
|
144 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=2.0, value=0.6, label="Temperature")
|
145 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p")
|
146 |
+
|
147 |
+
# New acknowledgment line at bottom
|
148 |
+
gr.Markdown("""
|
149 |
+
|
150 |
+
We sincerely acknowledge [VIDraft](https://huggingface.co/VIDraft) for their Phi 4 Reasoning Plus [space](https://huggingface.co/spaces/VIDraft/phi-4-reasoning-plus), which served as the starting point for this demo.
|
151 |
+
"""
|
152 |
+
)
|
153 |
+
|
154 |
with gr.Column(scale=4):
|
155 |
+
#chatbot = gr.Chatbot(label="Chat", type="messages")
|
156 |
chatbot = gr.Chatbot(label="Chat", type="messages", height=520)
|
157 |
with gr.Row():
|
158 |
+
user_input = gr.Textbox(label="User Input", placeholder="Type your question here...", lines=3, scale=8)
|
159 |
with gr.Column():
|
160 |
submit_button = gr.Button("Send", variant="primary", scale=1)
|
161 |
+
clear_button = gr.Button("Clear", scale=1)
|
162 |
gr.Markdown("**Try these examples:**")
|
163 |
with gr.Row():
|
164 |
example1_button = gr.Button("IIT-JEE 2025 Mathematics")
|
|
|
166 |
example3_button = gr.Button("Goldman Sachs Interview Puzzle")
|
167 |
example4_button = gr.Button("IIT-JEE 2024 Mathematics")
|
168 |
|
169 |
+
# ---------- helper functions now receive/return conversations ----------
|
170 |
def update_conversation_list(conversations):
|
171 |
return [conversations[cid]["title"] for cid in conversations]
|
172 |
|
173 |
def start_new_conversation(conversations):
|
174 |
new_id = generate_conversation_id()
|
175 |
conversations[new_id] = {"title": f"New Conversation {new_id}", "messages": []}
|
176 |
+
return new_id, [], gr.update(choices=update_conversation_list(conversations), value=conversations[new_id]["title"]), conversations
|
|
|
|
|
|
|
177 |
|
178 |
def load_conversation(selected_title, conversations):
|
179 |
for cid, convo in conversations.items():
|
|
|
181 |
return cid, convo["messages"], convo["messages"]
|
182 |
return current_convo_id.value, history_state.value, history_state.value
|
183 |
|
184 |
+
def send_message(user_message, max_tokens, temperature, top_p, convo_id, history, conversations):
|
|
|
|
|
185 |
if convo_id not in conversations:
|
186 |
+
#title = user_message.strip().split("\n")[0][:40]
|
187 |
title = " ".join(user_message.strip().split()[:5])
|
188 |
conversations[convo_id] = {"title": title, "messages": history}
|
|
|
189 |
if conversations[convo_id]["title"].startswith("New Conversation"):
|
190 |
+
#conversations[convo_id]["title"] = user_message.strip().split("\n")[0][:40]
|
191 |
conversations[convo_id]["title"] = " ".join(user_message.strip().split()[:5])
|
192 |
+
for updated_history, new_history in generate_response(user_message, max_tokens, temperature, top_p, history):
|
|
|
|
|
193 |
conversations[convo_id]["messages"] = new_history
|
194 |
+
yield updated_history, new_history, gr.update(choices=update_conversation_list(conversations), value=conversations[convo_id]["title"]), conversations
|
|
|
|
|
|
|
|
|
195 |
|
|
|
196 |
submit_button.click(
|
197 |
fn=send_message,
|
198 |
+
inputs=[user_input, max_tokens_slider, temperature_slider, top_p_slider, current_convo_id, history_state, conversations_state],
|
|
|
199 |
outputs=[chatbot, history_state, conversation_selector, conversations_state],
|
200 |
concurrency_limit=16
|
201 |
).then(
|
|
|
222 |
outputs=[current_convo_id, history_state, chatbot]
|
223 |
)
|
224 |
|
225 |
+
example1_button.click(fn=lambda: gr.update(value=example_messages["IIT-JEE 2025 Mathematics"]), inputs=None, outputs=user_input)
|
226 |
+
example2_button.click(fn=lambda: gr.update(value=example_messages["IIT-JEE 2025 Physics"]), inputs=None, outputs=user_input)
|
227 |
+
example3_button.click(fn=lambda: gr.update(value=example_messages["Goldman Sachs Interview Puzzle"]), inputs=None, outputs=user_input)
|
228 |
+
example4_button.click(fn=lambda: gr.update(value=example_messages["IIT-JEE 2024 Mathematics"]), inputs=None, outputs=user_input)
|
229 |
+
|
230 |
+
#demo.launch(share=True, ssr_mode=False)
|
231 |
+
|
|
|
|
|
|
|
|
|
232 |
if __name__ == "__main__":
|
233 |
+
# first positional argument = concurrency_count
|
234 |
demo.queue().launch(share=True, ssr_mode=False)
|