FractalAIR commited on
Commit
08d1d51
·
verified ·
1 Parent(s): 872ba85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -18,21 +18,11 @@ def format_math(text):
18
  text = text.replace(r"\(", "$").replace(r"\)", "$")
19
  return text
20
 
21
- # Global dictionary to store all conversations
22
  conversations = {}
23
 
24
- # Function to generate a user-friendly conversation title
25
- def generate_conversation_name(user_message):
26
- words = user_message.strip().split()
27
- if not words:
28
- return "Conversation-" + str(uuid.uuid4())[:8]
29
- name = " ".join(words[:5])
30
- base_name = name
31
- suffix = 1
32
- while name in conversations:
33
- name = f"{base_name} ({suffix})"
34
- suffix += 1
35
- return name
36
 
37
  @spaces.GPU(duration=60)
38
  def generate_response(user_message, max_tokens, temperature, top_p, history_state):
@@ -104,23 +94,33 @@ example_messages = {
104
  }
105
 
106
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
107
- gr.Markdown("# Ramanujan Ganit R1 14B V1 Chatbot\n\nWelcome to the Ramanujan Ganit R1 14B V1 Chatbot, developed by Fractal AI Research!\n\nOur model excels at reasoning tasks in mathematics and science.\n\nTry the example problems below from JEE Main 2025 or type in your own problems to see how our model breaks down complex reasoning problems.")
 
 
 
 
 
 
 
 
 
 
108
 
109
  with gr.Sidebar():
110
  gr.Markdown("## Conversations")
111
  conversation_selector = gr.Radio(choices=[], label="Select Conversation", interactive=True)
112
  new_convo_button = gr.Button("New Conversation")
113
 
114
- current_convo_id = gr.State("")
115
  history_state = gr.State([])
116
 
117
  with gr.Row():
118
  with gr.Column(scale=1):
119
  gr.Markdown("### Settings")
120
- max_tokens_slider = gr.Slider(6144, 32768, step=1024, value=16384, label="Max Tokens")
121
  with gr.Accordion("Advanced Settings", open=False):
122
- temperature_slider = gr.Slider(0.1, 2.0, value=0.6, label="Temperature")
123
- top_p_slider = gr.Slider(0.1, 1.0, value=0.95, label="Top-p")
124
 
125
  with gr.Column(scale=4):
126
  chatbot = gr.Chatbot(label="Chat", type="messages")
@@ -137,33 +137,33 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
137
  example4_button = gr.Button("JEE Main 2025 Physics")
138
 
139
  def update_conversation_list():
140
- return list(conversations.keys())
141
 
142
  def start_new_conversation():
143
- return "", [], gr.update(choices=update_conversation_list(), value=None)
 
 
144
 
145
- def load_conversation(selected_id):
146
- if selected_id in conversations:
147
- return selected_id, conversations[selected_id], conversations[selected_id]
148
- else:
149
- return current_convo_id.value, history_state.value, history_state.value
150
 
151
  def send_message(user_message, max_tokens, temperature, top_p, convo_id, history):
152
- new_convo = False
153
- if not convo_id:
154
- new_convo = True
155
- convo_id = generate_conversation_name(user_message)
156
- conversations[convo_id] = []
157
-
158
  for updated_history, new_history in generate_response(user_message, max_tokens, temperature, top_p, history):
159
- conversations[convo_id] = new_history
160
- updates = [gr.update(choices=update_conversation_list(), value=convo_id)] if new_convo else []
161
- yield updated_history, new_history, convo_id, *updates
162
 
163
  submit_button.click(
164
  fn=send_message,
165
  inputs=[user_input, max_tokens_slider, temperature_slider, top_p_slider, current_convo_id, history_state],
166
- outputs=[chatbot, history_state, current_convo_id, conversation_selector]
167
  ).then(
168
  fn=lambda: gr.update(value=""),
169
  inputs=None,
@@ -189,8 +189,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
189
  )
190
 
191
  example1_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Combinatorics"]), inputs=None, outputs=user_input)
192
- example2_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Co-ordinate Geometry"]), inputs=None, outputs=user_input)
193
- example3_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Prob-Stats"]), inputs=None, outputs=user_input)
194
- example4_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Physics"]), inputs=None, outputs=user_input)
195
 
196
  demo.launch(share=True, ssr_mode=False)
 
18
  text = text.replace(r"\(", "$").replace(r"\)", "$")
19
  return text
20
 
21
+ # Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
22
  conversations = {}
23
 
24
+ def generate_conversation_id():
25
+ return str(uuid.uuid4())[:8]
 
 
 
 
 
 
 
 
 
 
26
 
27
  @spaces.GPU(duration=60)
28
  def generate_response(user_message, max_tokens, temperature, top_p, history_state):
 
94
  }
95
 
96
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
97
+ gr.Markdown(
98
+ """
99
+ # Ramanujan Ganit R1 14B V1 Chatbot
100
+
101
+ Welcome to the Ramanujan Ganit R1 14B V1 Chatbot, developed by Fractal AI Research!
102
+
103
+ Our model excels at reasoning tasks in mathematics and science.
104
+
105
+ Try the example problems below from JEE Main 2025 or type in your own problems to see how our model breaks down complex reasoning problems.
106
+ """
107
+ )
108
 
109
  with gr.Sidebar():
110
  gr.Markdown("## Conversations")
111
  conversation_selector = gr.Radio(choices=[], label="Select Conversation", interactive=True)
112
  new_convo_button = gr.Button("New Conversation")
113
 
114
+ current_convo_id = gr.State(generate_conversation_id())
115
  history_state = gr.State([])
116
 
117
  with gr.Row():
118
  with gr.Column(scale=1):
119
  gr.Markdown("### Settings")
120
+ max_tokens_slider = gr.Slider(minimum=6144, maximum=32768, step=1024, value=16384, label="Max Tokens")
121
  with gr.Accordion("Advanced Settings", open=False):
122
+ temperature_slider = gr.Slider(minimum=0.1, maximum=2.0, value=0.6, label="Temperature")
123
+ top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p")
124
 
125
  with gr.Column(scale=4):
126
  chatbot = gr.Chatbot(label="Chat", type="messages")
 
137
  example4_button = gr.Button("JEE Main 2025 Physics")
138
 
139
  def update_conversation_list():
140
+ return [conversations[cid]["title"] for cid in conversations]
141
 
142
  def start_new_conversation():
143
+ new_id = generate_conversation_id()
144
+ conversations[new_id] = {"title": f"New Conversation {new_id}", "messages": []}
145
+ return new_id, [], gr.update(choices=update_conversation_list(), value=conversations[new_id]["title"])
146
 
147
+ def load_conversation(selected_title):
148
+ for cid, convo in conversations.items():
149
+ if convo["title"] == selected_title:
150
+ return cid, convo["messages"], convo["messages"]
151
+ return current_convo_id.value, history_state.value, history_state.value
152
 
153
  def send_message(user_message, max_tokens, temperature, top_p, convo_id, history):
154
+ if convo_id not in conversations:
155
+ title = user_message.strip().split("\n")[0][:40]
156
+ conversations[convo_id] = {"title": title, "messages": history}
157
+ if conversations[convo_id]["title"].startswith("New Conversation"):
158
+ conversations[convo_id]["title"] = user_message.strip().split("\n")[0][:40]
 
159
  for updated_history, new_history in generate_response(user_message, max_tokens, temperature, top_p, history):
160
+ conversations[convo_id]["messages"] = new_history
161
+ yield updated_history, new_history, gr.update(choices=update_conversation_list(), value=conversations[convo_id]["title"])
 
162
 
163
  submit_button.click(
164
  fn=send_message,
165
  inputs=[user_input, max_tokens_slider, temperature_slider, top_p_slider, current_convo_id, history_state],
166
+ outputs=[chatbot, history_state, conversation_selector]
167
  ).then(
168
  fn=lambda: gr.update(value=""),
169
  inputs=None,
 
189
  )
190
 
191
  example1_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Combinatorics"]), inputs=None, outputs=user_input)
192
+ example2_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Coordinate Geometry"]), inputs=None, outputs=user_input)
193
+ example3_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Probability & Statistics"]), inputs=None, outputs=user_input)
194
+ example4_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Laws of Motion"]), inputs=None, outputs=user_input)
195
 
196
  demo.launch(share=True, ssr_mode=False)