o7c1 commited on
Commit
2701cf1
Β·
verified Β·
1 Parent(s): 2660e3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -70,7 +70,7 @@ def send_bale_message(chat_id, text):
70
  "reply_markup": get_keyboard(chat_id),
71
  "parse_mode": "Markdown"
72
  }
73
- requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=10)
74
  except Exception as e:
75
  logger.error(f"Error sending message: {e}")
76
 
@@ -80,7 +80,7 @@ def edit_bale_buttons(chat_id, message_id, original_text):
80
  payload = {
81
  "chat_id": chat_id,
82
  "message_id": message_id,
83
- "text": original_text, # Keep original text so it doesn't disappear
84
  "reply_markup": get_keyboard(chat_id),
85
  "parse_mode": "Markdown"
86
  }
@@ -94,20 +94,21 @@ def process_single_message(chat_id, text):
94
  if chat_id not in chat_histories:
95
  chat_histories[chat_id] = []
96
 
97
- # Retry Strategy: 2 tries on 31B, final try on 26B fallback
98
- attempts = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
99
  response = None
100
  selected_model = None
101
- attempt_num = 0
102
 
103
  # Build prompt with history context
104
  current_req_content = chat_histories[chat_id] + [
105
  types.Content(role="user", parts=[types.Part(text=text)])
106
  ]
107
 
108
- for idx, model_name in enumerate(attempts):
109
  selected_model = model_name
110
- attempt_num = idx + 1
 
111
  try:
112
  # Configure Thinking level
113
  t_cfg = types.ThinkingConfig(thinking_level=settings["thinking"]) if settings["thinking"] != "off" else None
@@ -125,12 +126,14 @@ def process_single_message(chat_id, text):
125
  # Save success to history
126
  chat_histories[chat_id].append(types.Content(role="user", parts=[types.Part(text=text)]))
127
  chat_histories[chat_id].append(response.candidates[0].content)
128
- break
 
129
  except Exception as e:
130
- logger.warning(f"Attempt {attempt_num} failed for {selected_model}: {e}")
131
- if idx < 2: time.sleep(2)
 
132
  else:
133
- send_bale_message(chat_id, "❌ Server Error. Try toggling Thinking to 'LOW' or 'OFF'.")
134
  return
135
 
136
  # Parse Thought vs Answer
@@ -138,24 +141,28 @@ def process_single_message(chat_id, text):
138
  thought_text = "".join([p.text for p in parts if getattr(p, 'thought', False)]).strip()
139
  answer_text = "".join([p.text for p in parts if not getattr(p, 'thought', False)]).strip()
140
 
141
- if not answer_text: answer_text = response.text or "Model produced an empty result."
 
142
 
143
  # 1. Send Thinking Block (if applicable)
144
  if thought_text and settings["thinking"] != "off":
145
- send_bale_message(chat_id, f"🧠 *Thinking ({settings['thinking']}):*\n_{thought_text[:1200]}_")
146
  time.sleep(0.6)
147
 
148
- # 2. Send Final Answer
149
- status_footer = f"\n\nβš™οΈ _{selected_model} | Think: {settings['thinking']} | AFC: {'On' if settings['afc'] else 'Off'}_"
 
 
 
150
  send_bale_message(chat_id, answer_text + status_footer)
151
 
152
  # --- Polling Loop ---
153
  def poll_updates():
154
  offset = None
155
- logger.info("Bot is polling for updates...")
156
  while True:
157
  try:
158
- resp = requests.post(f"{BALE_API_URL}/getUpdates", json={"timeout": 30, "offset": offset}, timeout=35)
159
  if resp.status_code != 200:
160
  time.sleep(5)
161
  continue
@@ -175,18 +182,15 @@ def poll_updates():
175
 
176
  if cmd == "reset_chat":
177
  chat_histories[chat_id] = []
178
- # We send a NEW message instead of editing the old one
179
- send_bale_message(chat_id, "βœ… *Memory Cleared!* History is reset. What's next?")
180
 
181
  elif cmd == "toggle_think":
182
  modes = ["off", "low", "medium", "high"]
183
  s["thinking"] = modes[(modes.index(s["thinking"]) + 1) % 4]
184
- # Realtime update of the buttons on current message
185
  edit_bale_buttons(chat_id, msg_id, orig_text)
186
 
187
  elif cmd == "toggle_afc":
188
  s["afc"] = not s["afc"]
189
- # Realtime update of the buttons on current message
190
  edit_bale_buttons(chat_id, msg_id, orig_text)
191
  continue
192
 
@@ -197,9 +201,9 @@ def poll_updates():
197
  chat_id = msg["chat"]["id"]
198
  text = msg["text"]
199
 
200
- if text in ["/start", "/reset", "New Chat"]:
201
  chat_histories[chat_id] = []
202
- send_bale_message(chat_id, "πŸ‘‹ Hello! Memory is fresh. How can I help you today?")
203
  else:
204
  executor.submit(process_single_message, chat_id, text)
205
 
@@ -208,7 +212,5 @@ def poll_updates():
208
  time.sleep(5)
209
 
210
  if __name__ == "__main__":
211
- # Start polling in background
212
  threading.Thread(target=poll_updates, daemon=True).start()
213
- # Keep Flask alive for health checks (port 7860 for HF Spaces)
214
  app.run(host="0.0.0.0", port=7860)
 
70
  "reply_markup": get_keyboard(chat_id),
71
  "parse_mode": "Markdown"
72
  }
73
+ requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=15)
74
  except Exception as e:
75
  logger.error(f"Error sending message: {e}")
76
 
 
80
  payload = {
81
  "chat_id": chat_id,
82
  "message_id": message_id,
83
+ "text": original_text,
84
  "reply_markup": get_keyboard(chat_id),
85
  "parse_mode": "Markdown"
86
  }
 
94
  if chat_id not in chat_histories:
95
  chat_histories[chat_id] = []
96
 
97
+ # Retry Strategy: 1 & 2 on 31B, 3 on 26B fallback
98
+ attempts_list = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
99
  response = None
100
  selected_model = None
101
+ final_attempt_num = 1 # Track which attempt actually worked
102
 
103
  # Build prompt with history context
104
  current_req_content = chat_histories[chat_id] + [
105
  types.Content(role="user", parts=[types.Part(text=text)])
106
  ]
107
 
108
+ for idx, model_name in enumerate(attempts_list):
109
  selected_model = model_name
110
+ final_attempt_num = idx + 1
111
+
112
  try:
113
  # Configure Thinking level
114
  t_cfg = types.ThinkingConfig(thinking_level=settings["thinking"]) if settings["thinking"] != "off" else None
 
126
  # Save success to history
127
  chat_histories[chat_id].append(types.Content(role="user", parts=[types.Part(text=text)]))
128
  chat_histories[chat_id].append(response.candidates[0].content)
129
+ break # Exit loop on success
130
+
131
  except Exception as e:
132
+ logger.warning(f"Attempt {final_attempt_num}/3 failed for {selected_model}: {e}")
133
+ if idx < 2:
134
+ time.sleep(2) # Wait before retry
135
  else:
136
+ send_bale_message(chat_id, "❌ All AI attempts failed (Google Server Overload). Please try again in a few seconds.")
137
  return
138
 
139
  # Parse Thought vs Answer
 
141
  thought_text = "".join([p.text for p in parts if getattr(p, 'thought', False)]).strip()
142
  answer_text = "".join([p.text for p in parts if not getattr(p, 'thought', False)]).strip()
143
 
144
+ if not answer_text:
145
+ answer_text = response.text or "Model produced an empty result."
146
 
147
  # 1. Send Thinking Block (if applicable)
148
  if thought_text and settings["thinking"] != "off":
149
+ send_bale_message(chat_id, f"🧠 *Thinking ({settings['thinking']}):*\n_{thought_text[:1500]}_")
150
  time.sleep(0.6)
151
 
152
+ # 2. Send Final Answer with Attempt Metadata
153
+ status_footer = (
154
+ f"\n\nβš™οΈ _{selected_model} | **Attempt {final_attempt_num}/3** | "
155
+ f"Think: {settings['thinking']} | AFC: {'On' if settings['afc'] else 'Off'}_"
156
+ )
157
  send_bale_message(chat_id, answer_text + status_footer)
158
 
159
  # --- Polling Loop ---
160
  def poll_updates():
161
  offset = None
162
+ logger.info("Bot is polling...")
163
  while True:
164
  try:
165
+ resp = requests.post(f"{BALE_API_URL}/getUpdates", json={"timeout": 30, "offset": offset}, timeout=40)
166
  if resp.status_code != 200:
167
  time.sleep(5)
168
  continue
 
182
 
183
  if cmd == "reset_chat":
184
  chat_histories[chat_id] = []
185
+ send_bale_message(chat_id, "βœ… *Memory Cleared!* Starting fresh.")
 
186
 
187
  elif cmd == "toggle_think":
188
  modes = ["off", "low", "medium", "high"]
189
  s["thinking"] = modes[(modes.index(s["thinking"]) + 1) % 4]
 
190
  edit_bale_buttons(chat_id, msg_id, orig_text)
191
 
192
  elif cmd == "toggle_afc":
193
  s["afc"] = not s["afc"]
 
194
  edit_bale_buttons(chat_id, msg_id, orig_text)
195
  continue
196
 
 
201
  chat_id = msg["chat"]["id"]
202
  text = msg["text"]
203
 
204
+ if text in ["/start", "/reset"]:
205
  chat_histories[chat_id] = []
206
+ send_bale_message(chat_id, "πŸ‘‹ Welcome! How can I help you today?")
207
  else:
208
  executor.submit(process_single_message, chat_id, text)
209
 
 
212
  time.sleep(5)
213
 
214
  if __name__ == "__main__":
 
215
  threading.Thread(target=poll_updates, daemon=True).start()
 
216
  app.run(host="0.0.0.0", port=7860)