o7c1 commited on
Commit
649b73c
Β·
verified Β·
1 Parent(s): 09d4a10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -29
app.py CHANGED
@@ -12,6 +12,14 @@ from flask import Flask
12
 
13
  # Stores conversation history: { chat_id: [history_parts] }
14
  chat_histories = {}
 
 
 
 
 
 
 
 
15
  # ---------- Configuration ----------
16
  BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
17
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
@@ -58,77 +66,113 @@ from google.genai import types
58
 
59
  def process_single_message(chat_id, text):
60
  logger.info(f"Processing message from {chat_id}")
 
61
 
62
- # Initialize history for new users
63
  if chat_id not in chat_histories:
64
  chat_histories[chat_id] = []
65
 
66
  attempts = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
67
  response = None
68
  selected_model = None
69
- attempt_used = 1
 
 
 
 
70
 
71
  for idx, model_name in enumerate(attempts):
72
  selected_model = model_name
73
  attempt_used = idx + 1
 
74
  try:
75
- # We use start_chat to maintain context
76
- chat_session = ai_client.chats.create(
77
- model=selected_model,
78
- history=chat_histories[chat_id], # Pass current history
79
- config=types.GenerateContentConfig(
80
- thinking_config=types.ThinkingConfig(thinking_level="high")
 
 
 
 
 
 
 
 
 
 
81
  )
82
  )
83
 
84
- response = chat_session.send_message(text)
 
 
 
 
85
 
86
- # After success, update the local history with the new exchange
87
- # (The SDK automatically updates the session, but we save it for retries/persistence)
88
- chat_histories[chat_id] = chat_session.history
89
  break
90
 
91
  except Exception as e:
92
  logger.warning(f"Attempt {attempt_used} failed: {e}")
93
  if attempt_used < len(attempts): time.sleep(2)
94
  else:
95
- send_bale_message(chat_id, "AI Error. Try again later.")
96
  return
97
 
98
- # ... [Same Parsing Logic for Thoughts and Answers as previous code] ...
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- # Final delivery
101
- if thought_text:
102
- send_bale_message(chat_id, f"🧠 *Thinking Process:*\n_{thought_text}_")
103
- time.sleep(0.5)
104
 
105
- metadata = f"\n\nβš™οΈ _{selected_model} (Attempt {attempt_used}/3)_"
106
- send_bale_message(chat_id, answer_text + metadata)
 
107
 
108
 
109
  def send_bale_message(chat_id, text):
110
- """Sends a message with an Inline Button inside the chat bubble area."""
111
- # Define the Inline Keyboard (inside the chat area)
 
 
 
 
112
  inline_keyboard = {
113
- "inline_keyboard": [[
114
- {"text": "πŸ—‘οΈ New Chat", "callback_data": "reset_chat"}
115
- ]]
 
 
116
  }
117
 
118
  try:
119
- send_resp = requests.post(
120
  f"{BALE_API_URL}/sendMessage",
121
  json={
122
  "chat_id": chat_id,
123
  "text": text,
124
- "reply_markup": inline_keyboard
 
125
  },
126
  timeout=10
127
  )
128
- return send_resp
129
  except Exception as e:
130
  logger.error(f"Network error in send_bale_message: {e}")
131
- return None
132
 
133
  # ---------- Manual polling loop ----------
134
  def poll_updates():
@@ -172,7 +216,27 @@ def poll_updates():
172
  # Send confirmation
173
  send_bale_message(chat_id, "βœ… Memory cleared! How can I help you now?")
174
  continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
 
176
  # --- HANDLE NORMAL MESSAGES ---
177
  msg = upd.get("message")
178
  if not msg: continue
 
12
 
13
  # Stores conversation history: { chat_id: [history_parts] }
14
  chat_histories = {}
15
+ # Stores user preferences: { chat_id: {"thinking": "medium", "afc": False} }
16
+ user_settings = {}
17
+
18
+ def get_settings(chat_id):
19
+ """Retrieve settings or return defaults."""
20
+ if chat_id not in user_settings:
21
+ user_settings[chat_id] = {"thinking": "medium", "afc": False}
22
+ return user_settings[chat_id]
23
  # ---------- Configuration ----------
24
  BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
25
  GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
 
66
 
67
  def process_single_message(chat_id, text):
68
  logger.info(f"Processing message from {chat_id}")
69
+ settings = get_settings(chat_id)
70
 
 
71
  if chat_id not in chat_histories:
72
  chat_histories[chat_id] = []
73
 
74
  attempts = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
75
  response = None
76
  selected_model = None
77
+
78
+ # Build content with history
79
+ current_request_content = chat_histories[chat_id] + [
80
+ types.Content(role="user", parts=[types.Part(text=text)])
81
+ ]
82
 
83
  for idx, model_name in enumerate(attempts):
84
  selected_model = model_name
85
  attempt_used = idx + 1
86
+
87
  try:
88
+ # 1. Configure Thinking
89
+ t_cfg = None
90
+ if settings["thinking"] != "off":
91
+ t_cfg = types.ThinkingConfig(thinking_level=settings["thinking"])
92
+
93
+ # 2. Configure AFC & Tools
94
+ # If AFC is ON, we enable Google Search as the default tool
95
+ tools_cfg = []
96
+ if settings["afc"]:
97
+ tools_cfg = [types.Tool(google_search=types.GoogleSearch())]
98
+
99
+ config = types.GenerateContentConfig(
100
+ thinking_config=t_cfg,
101
+ tools=tools_cfg,
102
+ automatic_function_calling=types.AutomaticFunctionCallingConfig(
103
+ disable=not settings["afc"]
104
  )
105
  )
106
 
107
+ response = ai_client.models.generate_content(
108
+ model=selected_model,
109
+ contents=current_request_content,
110
+ config=config
111
+ )
112
 
113
+ # Update history on success
114
+ chat_histories[chat_id].append(types.Content(role="user", parts=[types.Part(text=text)]))
115
+ chat_histories[chat_id].append(response.candidates[0].content)
116
  break
117
 
118
  except Exception as e:
119
  logger.warning(f"Attempt {attempt_used} failed: {e}")
120
  if attempt_used < len(attempts): time.sleep(2)
121
  else:
122
+ send_bale_message(chat_id, "❌ Server Error. Try changing Thinking to 'LOW' or 'OFF'.")
123
  return
124
 
125
+ # --- PARSE AND SEND ---
126
+ thought_text = ""
127
+ answer_text = ""
128
+
129
+ if response and response.candidates:
130
+ for part in response.candidates[0].content.parts:
131
+ if getattr(part, 'thought', False):
132
+ thought_text += part.text
133
+ else:
134
+ answer_text += part.text
135
+
136
+ if not answer_text: answer_text = response.text or "No response generated."
137
 
138
+ # Send Thought (if any and not turned off)
139
+ if thought_text and settings["thinking"] != "off":
140
+ send_bale_message(chat_id, f"🧠 *Thinking ({settings['thinking']}):*\n_{thought_text[:1000]}_")
141
+ time.sleep(0.8)
142
 
143
+ # Final Answer
144
+ status_line = f"βš™οΈ _{selected_model} | Think: {settings['thinking']} | AFC: {'On' if settings['afc'] else 'Off'}_"
145
+ send_bale_message(chat_id, f"{answer_text}\n\n{status_line}")
146
 
147
 
148
  def send_bale_message(chat_id, text):
149
+ settings = get_settings(chat_id)
150
+
151
+ # Text labels for the buttons
152
+ think_label = f"🧠 Thinking: {settings['thinking'].upper()}"
153
+ afc_label = f"πŸ€– AFC: {'ON' if settings['afc'] else 'OFF'}"
154
+
155
  inline_keyboard = {
156
+ "inline_keyboard": [
157
+ [{"text": "πŸ—‘οΈ New Chat", "callback_data": "reset_chat"}],
158
+ [{"text": think_label, "callback_data": "toggle_think"}],
159
+ [{"text": afc_label, "callback_data": "toggle_afc"}]
160
+ ]
161
  }
162
 
163
  try:
164
+ requests.post(
165
  f"{BALE_API_URL}/sendMessage",
166
  json={
167
  "chat_id": chat_id,
168
  "text": text,
169
+ "reply_markup": inline_keyboard,
170
+ "parse_mode": "Markdown" # Enables bold/italics
171
  },
172
  timeout=10
173
  )
 
174
  except Exception as e:
175
  logger.error(f"Network error in send_bale_message: {e}")
 
176
 
177
  # ---------- Manual polling loop ----------
178
  def poll_updates():
 
216
  # Send confirmation
217
  send_bale_message(chat_id, "βœ… Memory cleared! How can I help you now?")
218
  continue
219
+ # --- INSIDE THE poll_updates loop, under 'if "callback_query" in upd:' ---
220
+ data = callback.get("data")
221
+ settings = get_settings(chat_id)
222
+
223
+ if data == "reset_chat":
224
+ chat_histories[chat_id] = []
225
+ send_bale_message(chat_id, "βœ… Memory cleared!")
226
+
227
+ elif data == "toggle_think":
228
+ # Rotate: off -> low -> medium -> high -> off
229
+ modes = ["off", "low", "medium", "high"]
230
+ current_idx = modes.index(settings["thinking"])
231
+ settings["thinking"] = modes[(current_idx + 1) % len(modes)]
232
+ send_bale_message(chat_id, f"βš™οΈ Thinking set to: *{settings['thinking'].upper()}*\n(Applied on next message)")
233
+
234
+ elif data == "toggle_afc":
235
+ settings["afc"] = not settings["afc"]
236
+ status = "ON" if settings["afc"] else "OFF"
237
+ send_bale_message(chat_id, f"βš™οΈ AFC set to: *{status}*\n(Applied on next message)")
238
 
239
+ continue
240
  # --- HANDLE NORMAL MESSAGES ---
241
  msg = upd.get("message")
242
  if not msg: continue