Update app.py
Browse files
app.py
CHANGED
|
@@ -52,20 +52,41 @@ def show_logs():
|
|
| 52 |
executor = ThreadPoolExecutor(max_workers=5)
|
| 53 |
|
| 54 |
def process_single_message(chat_id, text):
|
| 55 |
-
"""Processes a
|
| 56 |
logger.info(f"Processing message from {chat_id}: {text}")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
send_resp = requests.post(
|
| 71 |
f"{BALE_API_URL}/sendMessage",
|
|
|
|
| 52 |
executor = ThreadPoolExecutor(max_workers=5)
|
| 53 |
|
| 54 |
def process_single_message(chat_id, text):
|
| 55 |
+
"""Processes a message with up to 3 attempts, falling back to the 26B model on the last try."""
|
| 56 |
logger.info(f"Processing message from {chat_id}: {text}")
|
| 57 |
+
|
| 58 |
+
# Execution plan: Try 31B twice, then fall back to the 26B model.
|
| 59 |
+
attempts = [
|
| 60 |
+
'gemma-4-31b-it', # Attempt 1: Primary
|
| 61 |
+
'gemma-4-31b-it', # Attempt 2: Retry primary
|
| 62 |
+
'gemma-4-26b-a4b-it' # Attempt 3: Fallback MoE model
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
reply_text = None
|
| 66 |
+
|
| 67 |
+
for idx, model_name in enumerate(attempts):
|
| 68 |
+
attempt_num = idx + 1
|
| 69 |
+
logger.info(f"Attempt {attempt_num}/3 using model '{model_name}'")
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
response = ai_client.models.generate_content(
|
| 73 |
+
model=model_name,
|
| 74 |
+
contents=text
|
| 75 |
+
)
|
| 76 |
+
reply_text = response.text
|
| 77 |
+
break # Success! Exit the retry loop
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.warning(f"Attempt {attempt_num} failed with model '{model_name}': {e}")
|
| 81 |
+
|
| 82 |
+
# If we still have attempts left, pause before trying again
|
| 83 |
+
if attempt_num < len(attempts):
|
| 84 |
+
time.sleep(2) # Pause 2 seconds to let transient API issues clear
|
| 85 |
+
else:
|
| 86 |
+
logger.error("All 3 attempts (including fallback model) failed.")
|
| 87 |
+
reply_text = "Sorry, the AI services are temporarily unavailable. Please try again shortly."
|
| 88 |
+
|
| 89 |
+
# Send the reply back to Bale
|
| 90 |
try:
|
| 91 |
send_resp = requests.post(
|
| 92 |
f"{BALE_API_URL}/sendMessage",
|