Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import requests
|
|
| 10 |
from google import genai
|
| 11 |
from flask import Flask
|
| 12 |
|
|
|
|
|
|
|
| 13 |
# ---------- Configuration ----------
|
| 14 |
BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
|
| 15 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
@@ -55,119 +57,77 @@ import re
|
|
| 55 |
from google.genai import types
|
| 56 |
|
| 57 |
def process_single_message(chat_id, text):
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
separate thought delivery, and attempt tracking.
|
| 61 |
-
"""
|
| 62 |
-
logger.info(f"Processing message from {chat_id}: {text}")
|
| 63 |
-
|
| 64 |
-
attempts = [
|
| 65 |
-
'gemma-4-31b-it', # Attempt 1: Primary
|
| 66 |
-
'gemma-4-31b-it', # Attempt 2: Retry primary
|
| 67 |
-
'gemma-4-26b-a4b-it' # Attempt 3: Fallback MoE model
|
| 68 |
-
]
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
response = None
|
| 71 |
selected_model = None
|
| 72 |
attempt_used = 1
|
| 73 |
-
|
| 74 |
-
# 1. Run the generation loop with retries
|
| 75 |
for idx, model_name in enumerate(attempts):
|
| 76 |
selected_model = model_name
|
| 77 |
attempt_used = idx + 1
|
| 78 |
-
logger.info(f"Attempt {attempt_used}/3 using model '{selected_model}'")
|
| 79 |
-
|
| 80 |
try:
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
thinking_config=types.ThinkingConfig(
|
| 84 |
-
thinking_level="high"
|
| 85 |
-
),
|
| 86 |
-
# UNCOMMENT the line below if you want to enable internet search grounding:
|
| 87 |
-
# tools=[types.Tool(google_search=types.GoogleSearch())]
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
response = ai_client.models.generate_content(
|
| 91 |
model=selected_model,
|
| 92 |
-
|
| 93 |
-
config=
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
-
logger.warning(f"Attempt {attempt_used} failed
|
| 99 |
-
if attempt_used < len(attempts):
|
| 100 |
-
time.sleep(2)
|
| 101 |
else:
|
| 102 |
-
|
| 103 |
-
send_bale_message(chat_id, "Sorry, the AI services are temporarily unavailable. Please try again.")
|
| 104 |
return
|
| 105 |
|
| 106 |
-
|
| 107 |
-
return
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
thought_text = ""
|
| 111 |
-
answer_text = ""
|
| 112 |
-
|
| 113 |
-
# Try parsing via SDK objects first
|
| 114 |
-
thoughts_list = []
|
| 115 |
-
answers_list = []
|
| 116 |
-
if response.candidates and response.candidates[0].content.parts:
|
| 117 |
-
for part in response.candidates[0].content.parts:
|
| 118 |
-
if getattr(part, 'thought', False):
|
| 119 |
-
thoughts_list.append(part.text)
|
| 120 |
-
else:
|
| 121 |
-
answers_list.append(part.text)
|
| 122 |
-
thought_text = "\n".join(thoughts_list).strip()
|
| 123 |
-
answer_text = "\n".join(answers_list).strip()
|
| 124 |
-
|
| 125 |
-
# Fallback parsing: if the SDK returns raw text containing delimiters
|
| 126 |
-
if not thought_text:
|
| 127 |
-
raw_text = response.text or ""
|
| 128 |
-
|
| 129 |
-
# Check for <thought>...</thought>
|
| 130 |
-
thought_match = re.search(r'<thought>(.*?)</thought>', raw_text, re.DOTALL)
|
| 131 |
-
if thought_match:
|
| 132 |
-
thought_text = thought_match.group(1).strip()
|
| 133 |
-
answer_text = re.sub(r'<thought>.*?</thought>', '', raw_text, flags=re.DOTALL).strip()
|
| 134 |
-
else:
|
| 135 |
-
# Check for <|channel>thought ... <channel|>
|
| 136 |
-
channel_match = re.search(r'<\|channel>thought\s*(.*?)\s*<channel\|>', raw_text, re.DOTALL)
|
| 137 |
-
if channel_match:
|
| 138 |
-
thought_text = channel_match.group(1).strip()
|
| 139 |
-
answer_text = re.sub(r'<\|channel>thought.*?<channel\|>', '', raw_text, flags=re.DOTALL).strip()
|
| 140 |
-
else:
|
| 141 |
-
answer_text = raw_text.strip()
|
| 142 |
-
|
| 143 |
-
# 3. Send the Thought Chain as a separate message (if it exists)
|
| 144 |
if thought_text:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# 4. Format and send the Final Answer with attempt metadata
|
| 151 |
-
metadata_footer = f"\n\n⚙️ _Generated by {selected_model} (Attempt {attempt_used}/3)_"
|
| 152 |
-
final_payload = answer_text + metadata_footer
|
| 153 |
-
|
| 154 |
-
send_bale_message(chat_id, final_payload)
|
| 155 |
|
| 156 |
|
| 157 |
def send_bale_message(chat_id, text):
|
| 158 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
try:
|
| 160 |
send_resp = requests.post(
|
| 161 |
f"{BALE_API_URL}/sendMessage",
|
| 162 |
-
json={
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
timeout=10
|
| 164 |
)
|
| 165 |
-
if send_resp.status_code =
|
| 166 |
-
logger.
|
| 167 |
-
else:
|
| 168 |
-
logger.error(f"sendMessage failed: {send_resp.status_code} {send_resp.text}")
|
| 169 |
except Exception as e:
|
| 170 |
-
logger.error(f"Network error
|
| 171 |
|
| 172 |
# ---------- Manual polling loop ----------
|
| 173 |
def poll_updates():
|
|
@@ -208,7 +168,16 @@ def poll_updates():
|
|
| 208 |
|
| 209 |
# Offload AI generation to the executor so the polling loop remains non-blocking
|
| 210 |
executor.submit(process_single_message, chat_id, text)
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
except Exception as e:
|
| 213 |
logger.error(f"Polling loop error: {traceback.format_exc()}")
|
| 214 |
time.sleep(5)
|
|
|
|
| 10 |
from google import genai
|
| 11 |
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")
|
|
|
|
| 57 |
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 a persistent 'New Chat' button."""
|
| 111 |
+
# Define the keyboard markup
|
| 112 |
+
keyboard = {
|
| 113 |
+
"keyboard": [[{"text": "🗑️ New Chat"}]],
|
| 114 |
+
"resize_keyboard": True
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
try:
|
| 118 |
send_resp = requests.post(
|
| 119 |
f"{BALE_API_URL}/sendMessage",
|
| 120 |
+
json={
|
| 121 |
+
"chat_id": chat_id,
|
| 122 |
+
"text": text,
|
| 123 |
+
"reply_markup": keyboard # <--- This adds the button
|
| 124 |
+
},
|
| 125 |
timeout=10
|
| 126 |
)
|
| 127 |
+
if send_resp.status_code != 200:
|
| 128 |
+
logger.error(f"sendMessage failed: {send_resp.text}")
|
|
|
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
+
logger.error(f"Network error: {e}")
|
| 131 |
|
| 132 |
# ---------- Manual polling loop ----------
|
| 133 |
def poll_updates():
|
|
|
|
| 168 |
|
| 169 |
# Offload AI generation to the executor so the polling loop remains non-blocking
|
| 170 |
executor.submit(process_single_message, chat_id, text)
|
| 171 |
+
if text == "🗑️ New Chat":
|
| 172 |
+
# Clear the history for this user
|
| 173 |
+
chat_histories[chat_id] = []
|
| 174 |
+
logger.info(f"Chat history cleared for {chat_id}")
|
| 175 |
+
send_bale_message(chat_id, "Memory cleared! Starting a fresh conversation. How can I help you?")
|
| 176 |
+
continue # Skip AI processing for this specific command
|
| 177 |
+
|
| 178 |
+
# Otherwise, process as a normal AI request
|
| 179 |
+
executor.submit(process_single_message, chat_id, text)
|
| 180 |
+
|
| 181 |
except Exception as e:
|
| 182 |
logger.error(f"Polling loop error: {traceback.format_exc()}")
|
| 183 |
time.sleep(5)
|