gmtobl1 / app.py
o7c1's picture
Update app.py
2701cf1 verified
Raw History Blame Contribute Delete
8.21 kB
import os
import sys
import logging
import time
import json
import traceback
import threading
from concurrent.futures import ThreadPoolExecutor
import requests
from google import genai
from flask import Flask
from google.genai import types
# --- Global Storage ---
chat_histories = {}
user_settings = {}
def get_settings(chat_id):
"""Retrieve user settings or set defaults."""
if chat_id not in user_settings:
user_settings[chat_id] = {"thinking": "medium", "afc": False}
return user_settings[chat_id]
# --- Configuration ---
BALE_BOT_TOKEN = os.environ.get("BALE_BOT_TOKEN")
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
BALE_API_URL = f"https://tapi.bale.ai/bot{BALE_BOT_TOKEN}"
if not BALE_BOT_TOKEN or not GOOGLE_API_KEY:
sys.exit("Error: Please set BALE_BOT_TOKEN and GOOGLE_API_KEY environment variables.")
# --- Logging ---
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[logging.StreamHandler(sys.stdout)]
)
logger = logging.getLogger(__name__)
# --- AI Client Setup ---
ai_client = genai.Client(api_key=GOOGLE_API_KEY)
executor = ThreadPoolExecutor(max_workers=5)
app = Flask(__name__)
@app.route('/')
def health(): return "Bot is alive", 200
# --- Helper: Generate Keyboard ---
def get_keyboard(chat_id):
"""Generates the inline keyboard based on current user settings."""
settings = get_settings(chat_id)
think_label = f"🧠 Thinking: {settings['thinking'].upper()}"
afc_label = f"πŸ€– AFC: {'ON' if settings['afc'] else 'OFF'}"
return {
"inline_keyboard": [
[{"text": "πŸ—‘οΈ New Chat (Reset Memory)", "callback_data": "reset_chat"}],
[{"text": think_label, "callback_data": "toggle_think"}],
[{"text": afc_label, "callback_data": "toggle_afc"}]
]
}
# --- Helper: Communication ---
def send_bale_message(chat_id, text):
"""Sends a new message to the user with the settings keyboard."""
try:
payload = {
"chat_id": chat_id,
"text": text,
"reply_markup": get_keyboard(chat_id),
"parse_mode": "Markdown"
}
requests.post(f"{BALE_API_URL}/sendMessage", json=payload, timeout=15)
except Exception as e:
logger.error(f"Error sending message: {e}")
def edit_bale_buttons(chat_id, message_id, original_text):
"""Updates only the buttons on an existing message to show status changes."""
try:
payload = {
"chat_id": chat_id,
"message_id": message_id,
"text": original_text,
"reply_markup": get_keyboard(chat_id),
"parse_mode": "Markdown"
}
requests.post(f"{BALE_API_URL}/editMessageText", json=payload, timeout=10)
except Exception as e:
logger.error(f"Error editing buttons: {e}")
# --- AI Core Logic ---
def process_single_message(chat_id, text):
settings = get_settings(chat_id)
if chat_id not in chat_histories:
chat_histories[chat_id] = []
# Retry Strategy: 1 & 2 on 31B, 3 on 26B fallback
attempts_list = ['gemma-4-31b-it', 'gemma-4-31b-it', 'gemma-4-26b-a4b-it']
response = None
selected_model = None
final_attempt_num = 1 # Track which attempt actually worked
# Build prompt with history context
current_req_content = chat_histories[chat_id] + [
types.Content(role="user", parts=[types.Part(text=text)])
]
for idx, model_name in enumerate(attempts_list):
selected_model = model_name
final_attempt_num = idx + 1
try:
# Configure Thinking level
t_cfg = types.ThinkingConfig(thinking_level=settings["thinking"]) if settings["thinking"] != "off" else None
# Configure Internet Search (AFC)
tools_cfg = [types.Tool(google_search=types.GoogleSearch())] if settings["afc"] else []
config = types.GenerateContentConfig(
thinking_config=t_cfg,
tools=tools_cfg,
automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=not settings["afc"])
)
response = ai_client.models.generate_content(model=selected_model, contents=current_req_content, config=config)
# Save success to history
chat_histories[chat_id].append(types.Content(role="user", parts=[types.Part(text=text)]))
chat_histories[chat_id].append(response.candidates[0].content)
break # Exit loop on success
except Exception as e:
logger.warning(f"Attempt {final_attempt_num}/3 failed for {selected_model}: {e}")
if idx < 2:
time.sleep(2) # Wait before retry
else:
send_bale_message(chat_id, "❌ All AI attempts failed (Google Server Overload). Please try again in a few seconds.")
return
# Parse Thought vs Answer
parts = response.candidates[0].content.parts
thought_text = "".join([p.text for p in parts if getattr(p, 'thought', False)]).strip()
answer_text = "".join([p.text for p in parts if not getattr(p, 'thought', False)]).strip()
if not answer_text:
answer_text = response.text or "Model produced an empty result."
# 1. Send Thinking Block (if applicable)
if thought_text and settings["thinking"] != "off":
send_bale_message(chat_id, f"🧠 *Thinking ({settings['thinking']}):*\n_{thought_text[:1500]}_")
time.sleep(0.6)
# 2. Send Final Answer with Attempt Metadata
status_footer = (
f"\n\nβš™οΈ _{selected_model} | **Attempt {final_attempt_num}/3** | "
f"Think: {settings['thinking']} | AFC: {'On' if settings['afc'] else 'Off'}_"
)
send_bale_message(chat_id, answer_text + status_footer)
# --- Polling Loop ---
def poll_updates():
offset = None
logger.info("Bot is polling...")
while True:
try:
resp = requests.post(f"{BALE_API_URL}/getUpdates", json={"timeout": 30, "offset": offset}, timeout=40)
if resp.status_code != 200:
time.sleep(5)
continue
data = resp.json()
for upd in data.get("result", []):
offset = upd["update_id"] + 1
# A. Handle Buttons (Callback Queries)
if "callback_query" in upd:
cb = upd["callback_query"]
chat_id = cb["message"]["chat"]["id"]
msg_id = cb["message"]["message_id"]
orig_text = cb["message"].get("text", "")
cmd = cb.get("data")
s = get_settings(chat_id)
if cmd == "reset_chat":
chat_histories[chat_id] = []
send_bale_message(chat_id, "βœ… *Memory Cleared!* Starting fresh.")
elif cmd == "toggle_think":
modes = ["off", "low", "medium", "high"]
s["thinking"] = modes[(modes.index(s["thinking"]) + 1) % 4]
edit_bale_buttons(chat_id, msg_id, orig_text)
elif cmd == "toggle_afc":
s["afc"] = not s["afc"]
edit_bale_buttons(chat_id, msg_id, orig_text)
continue
# B. Handle Text Messages
msg = upd.get("message")
if not msg or "text" not in msg: continue
chat_id = msg["chat"]["id"]
text = msg["text"]
if text in ["/start", "/reset"]:
chat_histories[chat_id] = []
send_bale_message(chat_id, "πŸ‘‹ Welcome! How can I help you today?")
else:
executor.submit(process_single_message, chat_id, text)
except Exception:
logger.error(traceback.format_exc())
time.sleep(5)
if __name__ == "__main__":
threading.Thread(target=poll_updates, daemon=True).start()
app.run(host="0.0.0.0", port=7860)