Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
for idx, model_name in enumerate(attempts):
|
| 72 |
selected_model = model_name
|
| 73 |
attempt_used = idx + 1
|
|
|
|
| 74 |
try:
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
)
|
| 83 |
|
| 84 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
chat_histories[chat_id]
|
| 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, "
|
| 96 |
return
|
| 97 |
|
| 98 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
if thought_text:
|
| 102 |
-
send_bale_message(chat_id, f"π§ *Thinking
|
| 103 |
-
time.sleep(0.
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
|
| 109 |
def send_bale_message(chat_id, text):
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
inline_keyboard = {
|
| 113 |
-
"inline_keyboard": [
|
| 114 |
-
{"text": "ποΈ New Chat", "callback_data": "reset_chat"}
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
try:
|
| 119 |
-
|
| 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
|