FractalAIR commited on
Commit
917d1cb
·
verified ·
1 Parent(s): d074d0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -4
app.py CHANGED
@@ -24,7 +24,7 @@ def generate_conversation_id():
24
  return str(uuid.uuid4())[:8]
25
 
26
 
27
- def generate_response(user_message, max_tokens, temperature, top_p, history_state):
28
  if not user_message.strip():
29
  return history_state, history_state
30
 
@@ -72,9 +72,75 @@ def generate_response(user_message, max_tokens, temperature, top_p, history_stat
72
  except Exception:
73
  pass
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  yield new_history, new_history
76
 
77
-
78
  example_messages = {
79
  "JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
80
  "JEE Main 2025 Coordinate Geometry": "A circle \\(C\\) of radius 2 lies in the second quadrant and touches both the coordinate axes. Let \\(r\\) be the radius of a circle that has centre at the point \\((2, 5)\\) and intersects the circle \\(C\\) at exactly two points. If the set of all possible values of \\(r\\) is the interval \\((\\alpha, \\beta)\\), then \\(3\\beta - 2\\alpha\\) is?",
@@ -91,7 +157,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
91
  <div style="background-color: black; padding: 6px; border-radius: 8px;">
92
  <img src="https://framerusercontent.com/images/j0KjQQyrUfkFw4NwSaxQOLAoBU.png" alt="Fractal AI Logo" style="height: 48px;">
93
  </div>
94
- <h1 style="margin: 0;">Ramanujan Ganit R1 14B V1 Chatbot</h1>
95
  </div>
96
  """
97
  )
@@ -109,7 +175,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
109
  # INTRO TEXT MOVED HERE
110
  gr.Markdown(
111
  """
112
- Welcome to the Ramanujan Ganit R1 14B V1 Chatbot, developed by Fractal AI Research!
113
 
114
  Our model excels at reasoning tasks in mathematics and science.
115
 
 
24
  return str(uuid.uuid4())[:8]
25
 
26
 
27
+ '''def generate_response(user_message, max_tokens, temperature, top_p, history_state):
28
  if not user_message.strip():
29
  return history_state, history_state
30
 
 
72
  except Exception:
73
  pass
74
 
75
+ yield new_history, new_history'''
76
+
77
+
78
+ import tiktoken
79
+ enc = tiktoken.encoding_for_model("gpt-3.5-turbo") # any OpenAI encoding works
80
+
81
+ def generate_response(user_message,
82
+ max_tokens,
83
+ temperature,
84
+ top_p,
85
+ history_state):
86
+ if not user_message.strip():
87
+ return history_state, history_state
88
+
89
+ system_message = "Your role as an assistant..."
90
+ messages = [{"role": "system", "content": system_message}]
91
+ for m in history_state:
92
+ messages.append({"role": m["role"], "content": m["content"]})
93
+ messages.append({"role": "user", "content": user_message})
94
+
95
+ try:
96
+ response = client.chat.completions.create(
97
+ model="tgi",
98
+ messages=messages,
99
+ max_tokens=int(max_tokens), # server-side limit
100
+ temperature=temperature,
101
+ top_p=top_p,
102
+ stream=True
103
+ )
104
+ except Exception as e:
105
+ print(f"[ERROR] OpenAI API call failed: {e}")
106
+ yield history_state + [
107
+ {"role": "user", "content": user_message},
108
+ {"role": "assistant", "content": "⚠️ Generation failed."}
109
+ ], history_state
110
+ return
111
+
112
+ assistant_response = ""
113
+ new_history = history_state + [
114
+ {"role": "user", "content": user_message},
115
+ {"role": "assistant", "content": ""}
116
+ ]
117
+
118
+ token_budget = int(max_tokens)
119
+ tokens_seen = 0
120
+
121
+ try:
122
+ for chunk in response:
123
+ if (not chunk.choices
124
+ or not chunk.choices[0].delta
125
+ or not chunk.choices[0].delta.content):
126
+ continue
127
+
128
+ token_text = chunk.choices[0].delta.content
129
+ assistant_response += token_text
130
+ # count how many tokens that piece is worth
131
+ tokens_seen += len(enc.encode(token_text))
132
+
133
+ new_history[-1]["content"] = assistant_response.strip()
134
+ yield new_history, new_history
135
+
136
+ if tokens_seen >= token_budget:
137
+ break # stop the local loop
138
+ except Exception:
139
+ pass
140
+
141
  yield new_history, new_history
142
 
143
+
144
  example_messages = {
145
  "JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
146
  "JEE Main 2025 Coordinate Geometry": "A circle \\(C\\) of radius 2 lies in the second quadrant and touches both the coordinate axes. Let \\(r\\) be the radius of a circle that has centre at the point \\((2, 5)\\) and intersects the circle \\(C\\) at exactly two points. If the set of all possible values of \\(r\\) is the interval \\((\\alpha, \\beta)\\), then \\(3\\beta - 2\\alpha\\) is?",
 
157
  <div style="background-color: black; padding: 6px; border-radius: 8px;">
158
  <img src="https://framerusercontent.com/images/j0KjQQyrUfkFw4NwSaxQOLAoBU.png" alt="Fractal AI Logo" style="height: 48px;">
159
  </div>
160
+ <h1 style="margin: 0;">Ramanujan Ganit R1 14B Chatbot</h1>
161
  </div>
162
  """
163
  )
 
175
  # INTRO TEXT MOVED HERE
176
  gr.Markdown(
177
  """
178
+ Welcome to the Ramanujan Ganit R1 14B Chatbot, developed by Fractal AI Research!
179
 
180
  Our model excels at reasoning tasks in mathematics and science.
181