Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 21 |
-
conversations = {}
|
| 22 |
-
|
| 23 |
-
def generate_conversation_id():
|
| 24 |
return str(uuid.uuid4())[:8]
|
| 25 |
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
enc = tiktoken.encoding_for_model("gpt-3.5-turbo") # any OpenAI encoding works
|
| 29 |
-
|
| 30 |
def generate_response(user_message,
|
| 31 |
max_tokens,
|
| 32 |
temperature,
|
|
@@ -45,7 +45,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,89 +76,68 @@ def generate_response(user_message,
|
|
| 76 |
|
| 77 |
token_text = chunk.choices[0].delta.content
|
| 78 |
assistant_response += token_text
|
| 79 |
-
# count how many tokens that piece is worth
|
| 80 |
tokens_seen += len(enc.encode(token_text))
|
| 81 |
|
| 82 |
new_history[-1]["content"] = assistant_response.strip()
|
| 83 |
yield new_history, new_history
|
| 84 |
|
| 85 |
if tokens_seen >= token_budget:
|
| 86 |
-
break
|
| 87 |
except Exception:
|
| 88 |
pass
|
| 89 |
|
| 90 |
yield new_history, new_history
|
| 91 |
|
| 92 |
-
|
| 93 |
example_messages = {
|
| 94 |
-
"IIT-JEE 2024 Mathematics": "
|
| 95 |
-
"IIT-JEE 2025 Physics": "
|
| 96 |
-
"Goldman Sachs Interview Puzzle": "
|
| 97 |
-
"IIT-JEE 2025 Mathematics": "
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 101 |
-
|
| 102 |
-
#
|
| 103 |
-
gr.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
</div>
|
| 109 |
-
|
| 110 |
-
</div>
|
| 111 |
-
"""
|
| 112 |
-
)
|
| 113 |
|
| 114 |
with gr.Sidebar():
|
| 115 |
gr.Markdown("## Conversations")
|
| 116 |
conversation_selector = gr.Radio(choices=[], label="Select Conversation", interactive=True)
|
| 117 |
new_convo_button = gr.Button("New Conversation ➕")
|
| 118 |
|
| 119 |
-
current_convo_id = gr.State(generate_conversation_id())
|
| 120 |
-
history_state = gr.State([])
|
| 121 |
-
|
| 122 |
with gr.Row():
|
| 123 |
with gr.Column(scale=1):
|
| 124 |
-
|
| 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(
|
| 143 |
with gr.Accordion("Advanced Settings", open=True):
|
| 144 |
-
temperature_slider = gr.Slider(
|
| 145 |
-
top_p_slider
|
| 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
|
| 159 |
with gr.Column():
|
| 160 |
submit_button = gr.Button("Send", variant="primary", scale=1)
|
| 161 |
-
clear_button
|
| 162 |
gr.Markdown("**Try these examples:**")
|
| 163 |
with gr.Row():
|
| 164 |
example1_button = gr.Button("IIT-JEE 2025 Mathematics")
|
|
@@ -166,36 +145,49 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 166 |
example3_button = gr.Button("Goldman Sachs Interview Puzzle")
|
| 167 |
example4_button = gr.Button("IIT-JEE 2024 Mathematics")
|
| 168 |
|
| 169 |
-
|
|
|
|
| 170 |
return [conversations[cid]["title"] for cid in conversations]
|
| 171 |
|
| 172 |
-
def start_new_conversation():
|
| 173 |
new_id = generate_conversation_id()
|
| 174 |
conversations[new_id] = {"title": f"New Conversation {new_id}", "messages": []}
|
| 175 |
-
return new_id, [],
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
def load_conversation(selected_title):
|
| 178 |
for cid, convo in conversations.items():
|
| 179 |
if convo["title"] == selected_title:
|
| 180 |
return cid, convo["messages"], convo["messages"]
|
| 181 |
return current_convo_id.value, history_state.value, history_state.value
|
| 182 |
|
| 183 |
-
def send_message(user_message, max_tokens, temperature, top_p,
|
|
|
|
|
|
|
| 184 |
if convo_id not in conversations:
|
| 185 |
-
#title = user_message.strip().split("\n")[0][:40]
|
| 186 |
title = " ".join(user_message.strip().split()[:5])
|
| 187 |
conversations[convo_id] = {"title": title, "messages": history}
|
|
|
|
| 188 |
if conversations[convo_id]["title"].startswith("New Conversation"):
|
| 189 |
-
#conversations[convo_id]["title"] = user_message.strip().split("\n")[0][:40]
|
| 190 |
conversations[convo_id]["title"] = " ".join(user_message.strip().split()[:5])
|
| 191 |
-
|
|
|
|
|
|
|
| 192 |
conversations[convo_id]["messages"] = new_history
|
| 193 |
-
yield updated_history,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
|
|
|
| 195 |
submit_button.click(
|
| 196 |
fn=send_message,
|
| 197 |
-
inputs=[user_input, max_tokens_slider, temperature_slider, top_p_slider,
|
| 198 |
-
|
|
|
|
| 199 |
concurrency_limit=16
|
| 200 |
).then(
|
| 201 |
fn=lambda: gr.update(value=""),
|
|
@@ -211,24 +203,26 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 211 |
|
| 212 |
new_convo_button.click(
|
| 213 |
fn=start_new_conversation,
|
| 214 |
-
inputs=
|
| 215 |
-
outputs=[current_convo_id, history_state, conversation_selector]
|
| 216 |
)
|
| 217 |
|
| 218 |
conversation_selector.change(
|
| 219 |
fn=load_conversation,
|
| 220 |
-
inputs=conversation_selector,
|
| 221 |
outputs=[current_convo_id, history_state, chatbot]
|
| 222 |
)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
if __name__ == "__main__":
|
| 232 |
-
|
| 233 |
-
demo.queue().launch(share=True, ssr_mode=False)
|
| 234 |
-
|
|
|
|
| 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, re, uuid
|
| 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" # <-- your key
|
| 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 |
+
def generate_conversation_id() -> str:
|
|
|
|
|
|
|
|
|
|
| 25 |
return str(uuid.uuid4())[:8]
|
| 26 |
|
| 27 |
+
enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
|
| 28 |
|
| 29 |
+
# ------------------ generation -----------------------------
|
|
|
|
|
|
|
| 30 |
def generate_response(user_message,
|
| 31 |
max_tokens,
|
| 32 |
temperature,
|
|
|
|
| 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 |
|
| 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 |
+
# ------------------ example prompts ------------------------
|
| 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 |
+
# -------- session-scoped states --------
|
| 105 |
+
conversations_state = gr.State({}) # <- one dict PER USER
|
| 106 |
+
current_convo_id = gr.State(generate_conversation_id())
|
| 107 |
+
history_state = gr.State([])
|
| 108 |
+
|
| 109 |
+
# ---------------- layout ---------------
|
| 110 |
+
gr.HTML("""
|
| 111 |
+
<div style="display:flex;align-items:center;gap:16px;margin-bottom:1em;">
|
| 112 |
+
<div style="background-color:black;padding:6px;border-radius:8px;">
|
| 113 |
+
<img src="https://framerusercontent.com/images/j0KjQQyrUfkFw4NwSaxQOLAoBU.png"
|
| 114 |
+
style="height:48px;">
|
| 115 |
+
</div>
|
| 116 |
+
<h1 style="margin:0;">Fathom R1 14B Chatbot</h1>
|
| 117 |
</div>
|
| 118 |
+
""")
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
with gr.Sidebar():
|
| 121 |
gr.Markdown("## Conversations")
|
| 122 |
conversation_selector = gr.Radio(choices=[], label="Select Conversation", interactive=True)
|
| 123 |
new_convo_button = gr.Button("New Conversation ➕")
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column(scale=1):
|
| 127 |
+
gr.Markdown("""Welcome to the Fathom R1 14B Chatbot, developed by Fractal AI Research! ...""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = gr.Slider(0.1, 1.0, value=0.95, label="Top-p")
|
| 133 |
+
gr.Markdown("""We sincerely acknowledge [VIDraft]...""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
with gr.Column(scale=4):
|
|
|
|
| 135 |
chatbot = gr.Chatbot(label="Chat", type="messages", height=520)
|
| 136 |
with gr.Row():
|
| 137 |
+
user_input = gr.Textbox(label="User Input", placeholder="Type your question here...", lines=3, scale=8)
|
| 138 |
with gr.Column():
|
| 139 |
submit_button = gr.Button("Send", variant="primary", scale=1)
|
| 140 |
+
clear_button = gr.Button("Clear", scale=1)
|
| 141 |
gr.Markdown("**Try these examples:**")
|
| 142 |
with gr.Row():
|
| 143 |
example1_button = gr.Button("IIT-JEE 2025 Mathematics")
|
|
|
|
| 145 |
example3_button = gr.Button("Goldman Sachs Interview Puzzle")
|
| 146 |
example4_button = gr.Button("IIT-JEE 2024 Mathematics")
|
| 147 |
|
| 148 |
+
# ------------- helper callbacks -----------------
|
| 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 (new_id, [], # current_convo_id, history_state
|
| 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():
|
| 162 |
if convo["title"] == selected_title:
|
| 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 (updated_history,
|
| 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(
|
| 193 |
fn=lambda: gr.update(value=""),
|
|
|
|
| 203 |
|
| 204 |
new_convo_button.click(
|
| 205 |
fn=start_new_conversation,
|
| 206 |
+
inputs=[conversations_state],
|
| 207 |
+
outputs=[current_convo_id, history_state, conversation_selector, conversations_state]
|
| 208 |
)
|
| 209 |
|
| 210 |
conversation_selector.change(
|
| 211 |
fn=load_conversation,
|
| 212 |
+
inputs=[conversation_selector, conversations_state],
|
| 213 |
outputs=[current_convo_id, history_state, chatbot]
|
| 214 |
)
|
| 215 |
|
| 216 |
+
# example buttons (unchanged)
|
| 217 |
+
example1_button.click(lambda: gr.update(value=example_messages["IIT-JEE 2025 Mathematics"]),
|
| 218 |
+
None, user_input)
|
| 219 |
+
example2_button.click(lambda: gr.update(value=example_messages["IIT-JEE 2025 Physics"]),
|
| 220 |
+
None, user_input)
|
| 221 |
+
example3_button.click(lambda: gr.update(value=example_messages["Goldman Sachs Interview Puzzle"]),
|
| 222 |
+
None, user_input)
|
| 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)
|
|
|
|
|
|