Spaces:
Running
Running
Upload 6 files
Browse files- .dockerignore +5 -0
- Dockerfile +22 -0
- database.py +141 -0
- main.py +494 -0
- model_manager.py +159 -0
- requirements.txt +12 -0
.dockerignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
__pycache__
|
| 3 |
+
*.db
|
| 4 |
+
.git
|
| 5 |
+
.ipynb_checkpoints
|
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight Python image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory inside the container
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file first to leverage Docker caching
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the rest of your application code
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Hugging Face Spaces specifically listens on port 7860
|
| 17 |
+
ENV PORT=7860
|
| 18 |
+
EXPOSE 7860
|
| 19 |
+
|
| 20 |
+
# Start the FastAPI app using uvicorn
|
| 21 |
+
# We use 0.0.0.0 so it's accessible outside the container
|
| 22 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
database.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
from datetime import datetime, date, timedelta
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# βββ Init ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
def init_db():
|
| 7 |
+
conn = sqlite3.connect('tasks.db')
|
| 8 |
+
cursor = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# Create table with new date_context column
|
| 11 |
+
cursor.execute('''
|
| 12 |
+
CREATE TABLE IF NOT EXISTS tasks (
|
| 13 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
title TEXT NOT NULL,
|
| 15 |
+
time_context TEXT NOT NULL,
|
| 16 |
+
date_context TEXT NOT NULL DEFAULT 'today',
|
| 17 |
+
status TEXT DEFAULT 'pending'
|
| 18 |
+
)
|
| 19 |
+
''')
|
| 20 |
+
|
| 21 |
+
# Migrate existing DB: add date_context if it doesn't exist yet
|
| 22 |
+
try:
|
| 23 |
+
cursor.execute("ALTER TABLE tasks ADD COLUMN date_context TEXT NOT NULL DEFAULT 'today'")
|
| 24 |
+
print("Migration: added date_context column.")
|
| 25 |
+
except sqlite3.OperationalError:
|
| 26 |
+
pass # Column already exists β safe to ignore
|
| 27 |
+
|
| 28 |
+
conn.commit()
|
| 29 |
+
conn.close()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# βββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
+
def get_db_connection():
|
| 34 |
+
conn = sqlite3.connect('tasks.db')
|
| 35 |
+
conn.row_factory = sqlite3.Row
|
| 36 |
+
return conn
|
| 37 |
+
|
| 38 |
+
def resolve_date(date_context: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Converts natural language date strings into ISO format (YYYY-MM-DD).
|
| 41 |
+
Accepts: 'today', 'tomorrow', 'YYYY-MM-DD', or any existing value.
|
| 42 |
+
Returns the resolved ISO date string, or the raw value if unrecognised.
|
| 43 |
+
"""
|
| 44 |
+
if not date_context:
|
| 45 |
+
return date.today().isoformat()
|
| 46 |
+
|
| 47 |
+
normalised = date_context.strip().lower()
|
| 48 |
+
|
| 49 |
+
if normalised == "today":
|
| 50 |
+
return date.today().isoformat()
|
| 51 |
+
elif normalised == "tomorrow":
|
| 52 |
+
return (date.today() + timedelta(days=1)).isoformat()
|
| 53 |
+
elif normalised == "yesterday":
|
| 54 |
+
return (date.today() - timedelta(days=1)).isoformat()
|
| 55 |
+
|
| 56 |
+
# Already an ISO date β return as-is
|
| 57 |
+
try:
|
| 58 |
+
datetime.strptime(date_context.strip(), "%Y-%m-%d")
|
| 59 |
+
return date_context.strip()
|
| 60 |
+
except ValueError:
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
# Unrecognised β store raw so AI-generated strings like "next Monday" are kept
|
| 64 |
+
return date_context.strip()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# βββ CRUD ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
+
def get_all_tasks():
|
| 69 |
+
conn = get_db_connection()
|
| 70 |
+
cursor = conn.cursor()
|
| 71 |
+
cursor.execute("SELECT * FROM tasks WHERE status = 'pending' ORDER BY date_context, time_context")
|
| 72 |
+
rows = cursor.fetchall()
|
| 73 |
+
conn.close()
|
| 74 |
+
return [dict(row) for row in rows]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def get_tasks_by_date(date_context: str):
|
| 78 |
+
"""Fetch pending tasks for a specific date (accepts 'today', 'tomorrow', or ISO date)."""
|
| 79 |
+
resolved = resolve_date(date_context)
|
| 80 |
+
conn = get_db_connection()
|
| 81 |
+
cursor = conn.cursor()
|
| 82 |
+
cursor.execute(
|
| 83 |
+
"SELECT * FROM tasks WHERE status = 'pending' AND date_context = ? ORDER BY time_context",
|
| 84 |
+
(resolved,)
|
| 85 |
+
)
|
| 86 |
+
rows = cursor.fetchall()
|
| 87 |
+
conn.close()
|
| 88 |
+
return [dict(row) for row in rows]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def create_task(title: str, time_context: str, date_context: str = "today"):
|
| 92 |
+
resolved_date = resolve_date(date_context)
|
| 93 |
+
conn = get_db_connection()
|
| 94 |
+
cursor = conn.cursor()
|
| 95 |
+
cursor.execute(
|
| 96 |
+
"INSERT INTO tasks (title, time_context, date_context) VALUES (?, ?, ?)",
|
| 97 |
+
(title, time_context, resolved_date)
|
| 98 |
+
)
|
| 99 |
+
conn.commit()
|
| 100 |
+
new_id = cursor.lastrowid
|
| 101 |
+
conn.close()
|
| 102 |
+
# Return the created task so main.py can track last_task_id
|
| 103 |
+
return {"id": new_id, "title": title, "time_context": time_context, "date_context": resolved_date}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def delete_task(task_id: int):
|
| 107 |
+
conn = get_db_connection()
|
| 108 |
+
cursor = conn.cursor()
|
| 109 |
+
cursor.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
|
| 110 |
+
conn.commit()
|
| 111 |
+
conn.close()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def update_task(task_id: int, new_time: str = None, new_date: str = None, new_title: str = None):
|
| 115 |
+
conn = get_db_connection()
|
| 116 |
+
cursor = conn.cursor()
|
| 117 |
+
|
| 118 |
+
if new_time:
|
| 119 |
+
cursor.execute("UPDATE tasks SET time_context = ? WHERE id = ?", (new_time, task_id))
|
| 120 |
+
if new_date:
|
| 121 |
+
resolved = resolve_date(new_date)
|
| 122 |
+
cursor.execute("UPDATE tasks SET date_context = ? WHERE id = ?", (resolved, task_id))
|
| 123 |
+
if new_title:
|
| 124 |
+
cursor.execute("UPDATE tasks SET title = ? WHERE id = ?", (new_title, task_id))
|
| 125 |
+
|
| 126 |
+
conn.commit()
|
| 127 |
+
conn.close()
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def complete_task(task_id: int):
|
| 131 |
+
"""Mark a task as done without deleting it."""
|
| 132 |
+
conn = get_db_connection()
|
| 133 |
+
cursor = conn.cursor()
|
| 134 |
+
cursor.execute("UPDATE tasks SET status = 'done' WHERE id = ?", (task_id,))
|
| 135 |
+
conn.commit()
|
| 136 |
+
conn.close()
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
init_db()
|
| 141 |
+
print("Database initialised successfully.")
|
main.py
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from fastapi import FastAPI, Header
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
import google.generativeai as genai
|
| 10 |
+
from typing import Dict, Optional, List
|
| 11 |
+
|
| 12 |
+
from database import get_all_tasks, create_task, delete_task, update_task
|
| 13 |
+
from model_manager import model_manager
|
| 14 |
+
|
| 15 |
+
load_dotenv()
|
| 16 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 17 |
+
|
| 18 |
+
app = FastAPI()
|
| 19 |
+
app.add_middleware(
|
| 20 |
+
CORSMiddleware,
|
| 21 |
+
allow_origins=["*"],
|
| 22 |
+
allow_credentials=True,
|
| 23 |
+
allow_methods=["*"],
|
| 24 |
+
allow_headers=["*"],
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# βββ Session Store βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
sessions: Dict[str, Dict] = {}
|
| 29 |
+
|
| 30 |
+
def get_or_create_session(session_id: str) -> Dict:
|
| 31 |
+
if session_id not in sessions:
|
| 32 |
+
sessions[session_id] = {
|
| 33 |
+
"history": [],
|
| 34 |
+
"last_task_id": None,
|
| 35 |
+
"last_task_title": None,
|
| 36 |
+
"last_read_tasks": [],
|
| 37 |
+
"pending_delete": None, # task_id awaiting confirmation
|
| 38 |
+
}
|
| 39 |
+
return sessions[session_id]
|
| 40 |
+
|
| 41 |
+
# βββ Request / Response Models βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
+
class ChatRequest(BaseModel):
|
| 43 |
+
text: str
|
| 44 |
+
|
| 45 |
+
class ChatResponse(BaseModel):
|
| 46 |
+
intent: str
|
| 47 |
+
tts_response: str
|
| 48 |
+
session_id: str
|
| 49 |
+
model_used: str
|
| 50 |
+
|
| 51 |
+
# βββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
+
def get_current_datetime_context() -> str:
|
| 53 |
+
now = datetime.now()
|
| 54 |
+
return (
|
| 55 |
+
f"Current date : {now.strftime('%A, %B %d, %Y')}\n"
|
| 56 |
+
f"Current time : {now.strftime('%I:%M %p')}\n"
|
| 57 |
+
f"Time periods : morning = before 12 PM | afternoon = 12β5 PM | "
|
| 58 |
+
f"evening = 5β9 PM | night = after 9 PM"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def build_last_task_hint(session: Dict) -> str:
|
| 62 |
+
parts = []
|
| 63 |
+
|
| 64 |
+
if session["last_task_id"] is not None:
|
| 65 |
+
lid = session["last_task_id"]
|
| 66 |
+
ltitle = session.get("last_task_title") or f"ID {lid}"
|
| 67 |
+
parts.append(
|
| 68 |
+
f"*** CRITICAL CONTEXT ***\n"
|
| 69 |
+
f"The LAST task the user explicitly referenced was: '{ltitle}' (ID: {lid}).\n"
|
| 70 |
+
f"If the user says ANYTHING vague β 'the previous one', 'that one', 'it',\n"
|
| 71 |
+
f"'actually', 'change that', 'change it', 'move it' β you MUST use "
|
| 72 |
+
f"target_task_id: {lid} in that action.\n"
|
| 73 |
+
f"Do NOT pick a different task unless the user explicitly names one by title.\n"
|
| 74 |
+
f"*** END CRITICAL CONTEXT ***"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
last_read = session.get("last_read_tasks", [])
|
| 78 |
+
if last_read:
|
| 79 |
+
ordered = "\n".join(
|
| 80 |
+
f" Position {i+1}: '{t['title']}' at {t['time_context']} (ID: {t['id']})"
|
| 81 |
+
for i, t in enumerate(last_read)
|
| 82 |
+
)
|
| 83 |
+
parts.append(
|
| 84 |
+
f"*** LAST READ LIST ***\n"
|
| 85 |
+
f"The assistant just listed these tasks in this order:\n{ordered}\n"
|
| 86 |
+
f"If the user says 'the first one', 'the second one', 'the last one', etc.,\n"
|
| 87 |
+
f"resolve from this list and use that task's ID in the relevant action.\n"
|
| 88 |
+
f"*** END LAST READ LIST ***"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
return "\n\n".join(parts)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# βββ Semantic category map βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 95 |
+
# Maps common spoken concepts β keywords likely found in task titles
|
| 96 |
+
SEMANTIC_CATEGORIES = {
|
| 97 |
+
"workout": ["workout", "gym", "exercise", "run", "running", "training", "fitness",
|
| 98 |
+
"yoga", "pilates", "crossfit", "lift", "weights", "jog", "swim", "cycling", "bike"],
|
| 99 |
+
"meeting": ["meeting", "meet", "sync", "call", "standup", "stand-up", "catch-up",
|
| 100 |
+
"catchup", "1:1", "one on one", "interview", "review", "session"],
|
| 101 |
+
"linkedin": ["linkedin", "post", "social", "content", "publish", "share"],
|
| 102 |
+
"email": ["email", "mail", "inbox", "reply", "respond", "message"],
|
| 103 |
+
"lunch": ["lunch", "eat", "food", "meal", "dinner", "breakfast", "coffee", "cafe"],
|
| 104 |
+
"doctor": ["doctor", "dentist", "appointment", "checkup", "clinic", "hospital", "physio"],
|
| 105 |
+
"study": ["study", "read", "reading", "course", "class", "lecture", "homework", "revision"],
|
| 106 |
+
"errand": ["errand", "shop", "shopping", "grocery", "groceries", "bank", "pickup"],
|
| 107 |
+
"travel": ["travel", "flight", "commute", "drive", "uber", "taxi", "train", "bus"],
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
def build_semantic_hint(user_text: str, tasks: list) -> str:
|
| 111 |
+
"""
|
| 112 |
+
Detects semantic concepts in the user utterance and finds tasks
|
| 113 |
+
whose titles match those concepts. Injects a targeted hint so
|
| 114 |
+
Gemini can resolve vague references like 'my evening workout'.
|
| 115 |
+
"""
|
| 116 |
+
text_lower = user_text.lower()
|
| 117 |
+
matched_tasks = {} # task_id β task
|
| 118 |
+
|
| 119 |
+
for concept, keywords in SEMANTIC_CATEGORIES.items():
|
| 120 |
+
if any(kw in text_lower for kw in keywords):
|
| 121 |
+
# Find tasks whose title contains any keyword from this category
|
| 122 |
+
for task in tasks:
|
| 123 |
+
title_lower = task["title"].lower()
|
| 124 |
+
if any(kw in title_lower for kw in keywords):
|
| 125 |
+
matched_tasks[task["id"]] = task
|
| 126 |
+
|
| 127 |
+
# Also apply time-period narrowing from the utterance
|
| 128 |
+
time_filters = {
|
| 129 |
+
"morning": lambda t: (parse_minutes(t) or 9999) < 720, # before 12:00
|
| 130 |
+
"afternoon": lambda t: 720 <= (parse_minutes(t) or 0) < 1020,
|
| 131 |
+
"evening": lambda t: 1020 <= (parse_minutes(t) or 0) < 1260,
|
| 132 |
+
"night": lambda t: (parse_minutes(t) or 0) >= 1260,
|
| 133 |
+
}
|
| 134 |
+
active_filter = None
|
| 135 |
+
for period, fn in time_filters.items():
|
| 136 |
+
if period in text_lower:
|
| 137 |
+
active_filter = fn
|
| 138 |
+
break
|
| 139 |
+
|
| 140 |
+
if active_filter and matched_tasks:
|
| 141 |
+
narrowed = {
|
| 142 |
+
tid: t for tid, t in matched_tasks.items()
|
| 143 |
+
if active_filter(t.get("time_context", ""))
|
| 144 |
+
}
|
| 145 |
+
if narrowed:
|
| 146 |
+
matched_tasks = narrowed
|
| 147 |
+
|
| 148 |
+
if not matched_tasks:
|
| 149 |
+
return ""
|
| 150 |
+
|
| 151 |
+
task_list = "\n".join(
|
| 152 |
+
f" - '{t['title']}' at {t['time_context']} on {t.get('date_context','today')} (ID: {t['id']})"
|
| 153 |
+
for t in matched_tasks.values()
|
| 154 |
+
)
|
| 155 |
+
return (
|
| 156 |
+
f"\n\n*** SEMANTIC MATCH ***"
|
| 157 |
+
f"\nThe user said '{user_text}'. Based on semantic analysis, the most likely "
|
| 158 |
+
f"task(s) they are referring to:\n{task_list}"
|
| 159 |
+
f"\nUse the ID from this list as target_task_id. If only one match, use it directly."
|
| 160 |
+
f"\nIf multiple matches exist, pick the one that best fits the time period mentioned."
|
| 161 |
+
f"\n*** END SEMANTIC MATCH ***"
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
def resolve_confirmation(text: str) -> Optional[bool]:
|
| 165 |
+
"""
|
| 166 |
+
Returns True = confirmed, False = cancelled, None = unrelated input.
|
| 167 |
+
Detects the LAST matching word so 'actually wait no' correctly cancels.
|
| 168 |
+
"""
|
| 169 |
+
cleaned = text.lower()
|
| 170 |
+
for p in ".,!?;:'\"": cleaned = cleaned.replace(p, "")
|
| 171 |
+
padded = f" {cleaned} "
|
| 172 |
+
|
| 173 |
+
confirms = ["yes","yeah","yep","sure","ok","okay","confirm","please","do it","go ahead","delete it"]
|
| 174 |
+
cancels = ["no","nope","cancel","stop","nevermind","never mind","dont","wait","keep it"]
|
| 175 |
+
|
| 176 |
+
last_confirm = max([padded.rfind(f" {w} ") for w in confirms] + [-1])
|
| 177 |
+
last_cancel = max([padded.rfind(f" {w} ") for w in cancels] + [-1])
|
| 178 |
+
|
| 179 |
+
if last_confirm == -1 and last_cancel == -1:
|
| 180 |
+
return None
|
| 181 |
+
return last_confirm > last_cancel
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def parse_minutes(time_str: str) -> Optional[int]:
|
| 185 |
+
"""Convert a time string like '11:05 AM', '9 PM', '14:30' to total minutes since midnight."""
|
| 186 |
+
import re
|
| 187 |
+
if not time_str:
|
| 188 |
+
return None
|
| 189 |
+
s = time_str.strip().upper()
|
| 190 |
+
# Try HH:MM AM/PM
|
| 191 |
+
m = re.match(r"(\d{1,2}):(\d{2})\s*(AM|PM)?", s)
|
| 192 |
+
if m:
|
| 193 |
+
h, mn, period = int(m.group(1)), int(m.group(2)), m.group(3)
|
| 194 |
+
if period == "PM" and h != 12: h += 12
|
| 195 |
+
if period == "AM" and h == 12: h = 0
|
| 196 |
+
return h * 60 + mn
|
| 197 |
+
# Try H AM/PM (no minutes)
|
| 198 |
+
m = re.match(r"(\d{1,2})\s*(AM|PM)", s)
|
| 199 |
+
if m:
|
| 200 |
+
h, period = int(m.group(1)), m.group(2)
|
| 201 |
+
if period == "PM" and h != 12: h += 12
|
| 202 |
+
if period == "AM" and h == 12: h = 0
|
| 203 |
+
return h * 60
|
| 204 |
+
return None
|
| 205 |
+
|
| 206 |
+
def find_closest_task(requested_time: str, tasks: list, threshold_minutes: int = 60) -> Optional[dict]:
|
| 207 |
+
"""
|
| 208 |
+
Returns the task whose time_context is closest to requested_time,
|
| 209 |
+
only if within threshold_minutes. Returns None if no close match.
|
| 210 |
+
"""
|
| 211 |
+
req_mins = parse_minutes(requested_time)
|
| 212 |
+
if req_mins is None:
|
| 213 |
+
return None
|
| 214 |
+
|
| 215 |
+
best_task = None
|
| 216 |
+
best_delta = threshold_minutes + 1
|
| 217 |
+
|
| 218 |
+
for task in tasks:
|
| 219 |
+
task_mins = parse_minutes(task.get("time_context", ""))
|
| 220 |
+
if task_mins is None:
|
| 221 |
+
continue
|
| 222 |
+
delta = abs(task_mins - req_mins)
|
| 223 |
+
if delta < best_delta:
|
| 224 |
+
best_delta = delta
|
| 225 |
+
best_task = task
|
| 226 |
+
|
| 227 |
+
return best_task if best_task else None
|
| 228 |
+
|
| 229 |
+
# βββ Endpoints βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 230 |
+
@app.get("/api/tasks")
|
| 231 |
+
async def get_tasks_endpoint():
|
| 232 |
+
return get_all_tasks()
|
| 233 |
+
|
| 234 |
+
@app.get("/api/models")
|
| 235 |
+
async def list_models_endpoint():
|
| 236 |
+
return {"models": model_manager.status()}
|
| 237 |
+
|
| 238 |
+
@app.post("/api/chat", response_model=ChatResponse)
|
| 239 |
+
async def chat_endpoint(
|
| 240 |
+
request: ChatRequest,
|
| 241 |
+
x_session_id: Optional[str] = Header(default=None),
|
| 242 |
+
):
|
| 243 |
+
session_id = x_session_id or str(uuid.uuid4())
|
| 244 |
+
session = get_or_create_session(session_id)
|
| 245 |
+
|
| 246 |
+
session["history"].append({"role": "user", "text": request.text})
|
| 247 |
+
print(f"[{session_id}] User: {request.text}")
|
| 248 |
+
|
| 249 |
+
# ββ Pending delete confirmation check ββββββββββββββββββββββββββββββββββββββ
|
| 250 |
+
if session["pending_delete"] is not None:
|
| 251 |
+
confirmed = resolve_confirmation(request.text)
|
| 252 |
+
pending_id = session["pending_delete"]
|
| 253 |
+
|
| 254 |
+
if confirmed is True:
|
| 255 |
+
matched = next((t for t in get_all_tasks() if t["id"] == pending_id), None)
|
| 256 |
+
session["pending_delete"] = None
|
| 257 |
+
if matched:
|
| 258 |
+
delete_task(pending_id)
|
| 259 |
+
if session["last_task_id"] == pending_id:
|
| 260 |
+
session["last_task_id"] = None
|
| 261 |
+
session["last_task_title"] = None
|
| 262 |
+
msg = f"Done, I've deleted '{matched['title']}' scheduled at {matched['time_context']}."
|
| 263 |
+
else:
|
| 264 |
+
msg = "That task no longer exists."
|
| 265 |
+
session["history"].append({"role": "agent", "text": msg})
|
| 266 |
+
return ChatResponse(intent="DELETE", tts_response=msg, session_id=session_id, model_used="confirmation-handler")
|
| 267 |
+
|
| 268 |
+
elif confirmed is False:
|
| 269 |
+
session["pending_delete"] = None
|
| 270 |
+
msg = "Got it, I'll keep the task. Anything else?"
|
| 271 |
+
session["history"].append({"role": "agent", "text": msg})
|
| 272 |
+
return ChatResponse(intent="CHAT", tts_response=msg, session_id=session_id, model_used="confirmation-handler")
|
| 273 |
+
|
| 274 |
+
else:
|
| 275 |
+
# User changed subject β clear pending and fall through to normal AI flow
|
| 276 |
+
session["pending_delete"] = None
|
| 277 |
+
|
| 278 |
+
# ββ Build prompt βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 279 |
+
current_tasks = get_all_tasks()
|
| 280 |
+
datetime_context = get_current_datetime_context()
|
| 281 |
+
formatted_history = "\n".join(f"{m['role'].upper()}: {m['text']}" for m in session["history"])
|
| 282 |
+
hint_block = build_last_task_hint(session)
|
| 283 |
+
|
| 284 |
+
# ββ Pre-resolve 1: fuzzy time match ββββββββββββββββββββββββββββββββββββββ
|
| 285 |
+
import re as _re
|
| 286 |
+
_time_pat = _re.search(
|
| 287 |
+
r"\b(\d{1,2}(?::\d{2})?\s*(?:AM|PM|am|pm))\b", request.text
|
| 288 |
+
)
|
| 289 |
+
_fuzzy_hint = ""
|
| 290 |
+
if _time_pat:
|
| 291 |
+
_req_time = _time_pat.group(1)
|
| 292 |
+
_req_mins = parse_minutes(_req_time)
|
| 293 |
+
_exact = any(
|
| 294 |
+
parse_minutes(t.get("time_context","")) == _req_mins
|
| 295 |
+
for t in current_tasks
|
| 296 |
+
)
|
| 297 |
+
if not _exact and _req_mins is not None:
|
| 298 |
+
_closest = find_closest_task(_req_time, current_tasks, threshold_minutes=90)
|
| 299 |
+
if _closest:
|
| 300 |
+
_fuzzy_hint = (
|
| 301 |
+
f"\n*** FUZZY TIME MATCH ***"
|
| 302 |
+
f"\nThe user asked about a task at {_req_time} but NO task exists at that exact time."
|
| 303 |
+
f"\nThe CLOSEST task is: '{_closest['title']}' at {_closest['time_context']} (ID: {_closest['id']})."
|
| 304 |
+
f"\nIf the user intent is DELETE or UPDATE, use ID {_closest['id']} as target_task_id."
|
| 305 |
+
f"\nDo NOT say the task was not found. Instead use this closest match."
|
| 306 |
+
f"\n*** END FUZZY TIME MATCH ***"
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
# ββ Pre-resolve 2: semantic concept match βββββββββββββββββββββββββββββββββ
|
| 310 |
+
_semantic_hint = build_semantic_hint(request.text, current_tasks)
|
| 311 |
+
|
| 312 |
+
system_prompt = f"""
|
| 313 |
+
You are an intelligent Voice Task Manager. You MUST handle multiple actions in a single response when the user asks for them.
|
| 314 |
+
|
| 315 |
+
{datetime_context}
|
| 316 |
+
|
| 317 |
+
{hint_block}{_fuzzy_hint}{_semantic_hint}
|
| 318 |
+
|
| 319 |
+
Current tasks in the database:
|
| 320 |
+
{json.dumps(current_tasks, indent=2)}
|
| 321 |
+
|
| 322 |
+
Conversation history (oldest β newest):
|
| 323 |
+
{formatted_history}
|
| 324 |
+
|
| 325 |
+
Output a strict JSON object with NO markdown. Each action in the "actions" array is independent.
|
| 326 |
+
|
| 327 |
+
Schema:
|
| 328 |
+
{{
|
| 329 |
+
"actions": [
|
| 330 |
+
{{
|
| 331 |
+
"intent": "CREATE" | "UPDATE" | "DELETE" | "READ" | "CHAT",
|
| 332 |
+
"target_task_id": <integer task ID for UPDATE/DELETE, or null>,
|
| 333 |
+
"entities": {{
|
| 334 |
+
"title": "Task title β required for CREATE, optional for UPDATE (if renaming)",
|
| 335 |
+
"time_context": "e.g. '7:00 AM' β required for CREATE, optional for UPDATE",
|
| 336 |
+
"date_context": "e.g. 'today', 'tomorrow', 'YYYY-MM-DD' β required for CREATE, optional for UPDATE",
|
| 337 |
+
"time_filter": "morning|afternoon|evening|night|today|tomorrow|all β READ only"
|
| 338 |
+
}},
|
| 339 |
+
"read_task_ids": [ordered list of task IDs mentioned β READ only, else omit]
|
| 340 |
+
}}
|
| 341 |
+
],
|
| 342 |
+
"tts_response": "A single natural spoken reply covering ALL actions together."
|
| 343 |
+
}}
|
| 344 |
+
|
| 345 |
+
Rules β READ CAREFULLY:
|
| 346 |
+
1. MULTI-ACTION: If the user requests N things (e.g. 3 tasks, or create + delete), produce N action objects.
|
| 347 |
+
Example: "Gym at 7, sync at 9, LinkedIn at 11 tomorrow" β 3 CREATE actions.
|
| 348 |
+
Example: "Delete LinkedIn and add a call at 4 PM" β 1 DELETE + 1 CREATE action.
|
| 349 |
+
|
| 350 |
+
2. CREATE: Every CREATE action needs its own title, time_context, date_context (default 'today').
|
| 351 |
+
|
| 352 |
+
3. UPDATE: target_task_id goes INSIDE the action object. Only fill changed entity fields.
|
| 353 |
+
|
| 354 |
+
4. DELETE: target_task_id goes INSIDE the action object. Set entities to {{}}.
|
| 355 |
+
Only use IDs that exist in the database list. Never invent IDs.
|
| 356 |
+
|
| 357 |
+
5. READ: Use time_filter to select which tasks to mention. Speak naturally, not as a list.
|
| 358 |
+
Fill read_task_ids in the order you mention them.
|
| 359 |
+
|
| 360 |
+
6. tts_response is ONE combined reply for everything, e.g.:
|
| 361 |
+
"Done! I've added Gym at 7 AM, Team sync at 9 AM, and LinkedIn post at 11 AM β all for tomorrow morning."
|
| 362 |
+
|
| 363 |
+
7. Vague references ('the previous one', 'it', 'that', 'the second one'):
|
| 364 |
+
Resolve using the CRITICAL CONTEXT and LAST READ LIST hints above.
|
| 365 |
+
Never invent task IDs.
|
| 366 |
+
|
| 367 |
+
8. Semantic references ('my workout', 'the meeting', 'evening run', 'the LinkedIn thing'):
|
| 368 |
+
Resolve using the SEMANTIC MATCH hint above when present.
|
| 369 |
+
Match by concept, not exact wording β 'gym session' matches a task called 'Morning Workout'.
|
| 370 |
+
If a time period is mentioned ('evening workout'), use it to narrow among multiple matches.
|
| 371 |
+
Always prefer the SEMANTIC MATCH hint ID over guessing from the task title alone.
|
| 372 |
+
|
| 373 |
+
Time-filter reference:
|
| 374 |
+
- morning β before 12 PM
|
| 375 |
+
- afternoon β 12 PM β 5 PM
|
| 376 |
+
- evening β 5 PM β 9 PM
|
| 377 |
+
- night β after 9 PM
|
| 378 |
+
- today / tomorrow β by date
|
| 379 |
+
- all β no filter
|
| 380 |
+
"""
|
| 381 |
+
|
| 382 |
+
try:
|
| 383 |
+
response_text, model_used = model_manager.call_with_fallback(system_prompt)
|
| 384 |
+
ai_decision = json.loads(response_text)
|
| 385 |
+
actions = ai_decision.get("actions", [])
|
| 386 |
+
tts_response = ai_decision.get("tts_response", "Done.")
|
| 387 |
+
|
| 388 |
+
print(f"[{session_id}] Decision ({model_used}) β {len(actions)} action(s):", ai_decision)
|
| 389 |
+
|
| 390 |
+
last_intent = "CHAT"
|
| 391 |
+
|
| 392 |
+
for action in actions:
|
| 393 |
+
intent = action.get("intent", "CHAT")
|
| 394 |
+
tid = action.get("target_task_id")
|
| 395 |
+
entities = action.get("entities", {})
|
| 396 |
+
last_intent = intent
|
| 397 |
+
|
| 398 |
+
if intent == "CREATE":
|
| 399 |
+
task_title = entities.get("title", "Untitled")
|
| 400 |
+
new_task = create_task(
|
| 401 |
+
task_title,
|
| 402 |
+
entities.get("time_context", ""),
|
| 403 |
+
entities.get("date_context", "today"),
|
| 404 |
+
)
|
| 405 |
+
if isinstance(new_task, dict) and "id" in new_task:
|
| 406 |
+
session["last_task_id"] = new_task["id"]
|
| 407 |
+
session["last_task_title"] = task_title
|
| 408 |
+
|
| 409 |
+
elif intent == "UPDATE":
|
| 410 |
+
if tid:
|
| 411 |
+
update_task(
|
| 412 |
+
tid,
|
| 413 |
+
new_time=entities.get("time_context"),
|
| 414 |
+
new_date=entities.get("date_context"),
|
| 415 |
+
new_title=entities.get("title"), # <-- ADD THIS LINE
|
| 416 |
+
)
|
| 417 |
+
session["last_task_id"] = tid
|
| 418 |
+
matched = next((t for t in current_tasks if t.get("id") == tid), None)
|
| 419 |
+
session["last_task_title"] = matched["title"] if matched else None
|
| 420 |
+
|
| 421 |
+
elif intent == "DELETE":
|
| 422 |
+
import re as _re2
|
| 423 |
+
# ββ Step 1: exact match by ID Gemini provided ββββββββββββββββββ
|
| 424 |
+
matched = next((t for t in current_tasks if t.get("id") == tid), None) if tid else None
|
| 425 |
+
|
| 426 |
+
# ββ Step 2: fallback β fuzzy match from raw utterance ββββββββββ
|
| 427 |
+
if not matched:
|
| 428 |
+
_tp = _re2.search(r"\b(\d{1,2}(?::\d{2})?\s*(?:AM|PM|am|pm))\b", request.text)
|
| 429 |
+
_rts = _tp.group(1) if _tp else ""
|
| 430 |
+
matched = find_closest_task(_rts, current_tasks, threshold_minutes=90) if _rts else None
|
| 431 |
+
|
| 432 |
+
if matched:
|
| 433 |
+
# ββ Step 3: always confirm before deleting βββββββββββββββββ
|
| 434 |
+
req_time_str = ""
|
| 435 |
+
_tp2 = _re2.search(r"\b(\d{1,2}(?::\d{2})?\s*(?:AM|PM|am|pm))\b", request.text)
|
| 436 |
+
if _tp2:
|
| 437 |
+
req_time_str = _tp2.group(1)
|
| 438 |
+
|
| 439 |
+
exact_match = parse_minutes(req_time_str) == parse_minutes(matched["time_context"]) if req_time_str else True
|
| 440 |
+
|
| 441 |
+
if exact_match:
|
| 442 |
+
confirm_msg = (
|
| 443 |
+
f"Just to confirm β delete '{matched['title']}' "
|
| 444 |
+
f"at {matched['time_context']}? Say yes to confirm or no to cancel."
|
| 445 |
+
)
|
| 446 |
+
else:
|
| 447 |
+
confirm_msg = (
|
| 448 |
+
f"I couldn't find a task at {req_time_str}. "
|
| 449 |
+
f"Did you mean '{matched['title']}' at {matched['time_context']}? "
|
| 450 |
+
f"Say yes to delete it or no to cancel."
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
session["pending_delete"] = matched["id"]
|
| 454 |
+
session["history"].append({"role": "agent", "text": confirm_msg})
|
| 455 |
+
return ChatResponse(
|
| 456 |
+
intent="CLARIFICATION",
|
| 457 |
+
tts_response=confirm_msg,
|
| 458 |
+
session_id=session_id,
|
| 459 |
+
model_used=model_used,
|
| 460 |
+
)
|
| 461 |
+
# else: nothing found at all β fall through, AI tts_response handles it
|
| 462 |
+
|
| 463 |
+
elif intent == "READ":
|
| 464 |
+
read_ids = action.get("read_task_ids", [])
|
| 465 |
+
id_to_task = {t["id"]: t for t in current_tasks}
|
| 466 |
+
if read_ids:
|
| 467 |
+
session["last_read_tasks"] = [
|
| 468 |
+
id_to_task[rid] for rid in read_ids if rid in id_to_task
|
| 469 |
+
]
|
| 470 |
+
if session["last_read_tasks"]:
|
| 471 |
+
last = session["last_read_tasks"][-1]
|
| 472 |
+
session["last_task_id"] = last["id"]
|
| 473 |
+
session["last_task_title"] = last["title"]
|
| 474 |
+
|
| 475 |
+
session["history"].append({"role": "agent", "text": tts_response})
|
| 476 |
+
|
| 477 |
+
return ChatResponse(
|
| 478 |
+
intent=last_intent,
|
| 479 |
+
tts_response=tts_response,
|
| 480 |
+
session_id=session_id,
|
| 481 |
+
model_used=model_used,
|
| 482 |
+
)
|
| 483 |
+
|
| 484 |
+
except RuntimeError as e:
|
| 485 |
+
msg = "All AI models are currently rate-limited. Please wait a moment and try again."
|
| 486 |
+
print(f"[{session_id}] {e}")
|
| 487 |
+
session["history"].append({"role": "agent", "text": msg})
|
| 488 |
+
return ChatResponse(intent="ERROR", tts_response=msg, session_id=session_id, model_used="none")
|
| 489 |
+
|
| 490 |
+
except Exception as e:
|
| 491 |
+
msg = "Sorry, I had trouble processing that request."
|
| 492 |
+
print(f"[{session_id}] Error: {e}")
|
| 493 |
+
session["history"].append({"role": "agent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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import threading
|
| 3 |
+
from collections import deque
|
| 4 |
+
from typing import Optional
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
|
| 7 |
+
# βββ Model Pool (only models with actual quota) βββββββββββββββββββββββββββββββ
|
| 8 |
+
# Ordered by preference: most quota first
|
| 9 |
+
MODEL_POOL = [
|
| 10 |
+
{
|
| 11 |
+
"key": "gemini-3.1-flash-lite",
|
| 12 |
+
"name": "Gemini 3.1 Flash Lite",
|
| 13 |
+
"rpm": 15,
|
| 14 |
+
"rpd": 500,
|
| 15 |
+
"tpm": 250_000,
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"key": "gemini-2.5-flash-lite", # gemini-2.5-flash-lite-preview-06-17 if needed
|
| 19 |
+
"name": "Gemini 2.5 Flash Lite",
|
| 20 |
+
"rpm": 10,
|
| 21 |
+
"rpd": 20,
|
| 22 |
+
"tpm": 250_000,
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"key": "gemini-2.5-flash",
|
| 26 |
+
"name": "Gemini 2.5 Flash",
|
| 27 |
+
"rpm": 5,
|
| 28 |
+
"rpd": 20,
|
| 29 |
+
"tpm": 250_000,
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"key": "gemini-2.0-flash", # "Gemini 3 Flash" in the UI
|
| 33 |
+
"name": "Gemini 3 Flash",
|
| 34 |
+
"rpm": 5,
|
| 35 |
+
"rpd": 20,
|
| 36 |
+
"tpm": 250_000,
|
| 37 |
+
},
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
class ModelManager:
|
| 41 |
+
"""
|
| 42 |
+
Tracks per-model rate limits (RPM + RPD) and automatically shuffles
|
| 43 |
+
to the next available model when a limit is reached.
|
| 44 |
+
Resets minute/day windows with a sliding window approach.
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
def __init__(self):
|
| 48 |
+
self._lock = threading.Lock()
|
| 49 |
+
# For each model key: deque of UTC timestamps for recent calls
|
| 50 |
+
self._minute_calls: dict[str, deque] = {m["key"]: deque() for m in MODEL_POOL}
|
| 51 |
+
self._day_calls: dict[str, deque] = {m["key"]: deque() for m in MODEL_POOL}
|
| 52 |
+
# Track which models are in a cooldown (rate-limited by the API itself)
|
| 53 |
+
self._cooldown_until: dict[str, float] = {m["key"]: 0.0 for m in MODEL_POOL}
|
| 54 |
+
|
| 55 |
+
def _prune(self, dq: deque, window_seconds: int) -> None:
|
| 56 |
+
"""Remove timestamps outside the rolling window."""
|
| 57 |
+
cutoff = time.time() - window_seconds
|
| 58 |
+
while dq and dq[0] < cutoff:
|
| 59 |
+
dq.popleft()
|
| 60 |
+
|
| 61 |
+
def _is_available(self, model: dict) -> bool:
|
| 62 |
+
key = model["key"]
|
| 63 |
+
now = time.time()
|
| 64 |
+
|
| 65 |
+
# Hard cooldown (e.g. after a 429)
|
| 66 |
+
if now < self._cooldown_until[key]:
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
+
self._prune(self._minute_calls[key], 60)
|
| 70 |
+
self._prune(self._day_calls[key], 86_400)
|
| 71 |
+
|
| 72 |
+
rpm_ok = len(self._minute_calls[key]) < model["rpm"]
|
| 73 |
+
rpd_ok = len(self._day_calls[key]) < model["rpd"]
|
| 74 |
+
return rpm_ok and rpd_ok
|
| 75 |
+
|
| 76 |
+
def _record_call(self, key: str) -> None:
|
| 77 |
+
now = time.time()
|
| 78 |
+
self._minute_calls[key].append(now)
|
| 79 |
+
self._day_calls[key].append(now)
|
| 80 |
+
|
| 81 |
+
def _set_cooldown(self, key: str, seconds: int = 65) -> None:
|
| 82 |
+
"""Call this after receiving a 429 to pause that model."""
|
| 83 |
+
self._cooldown_until[key] = time.time() + seconds
|
| 84 |
+
print(f"[ModelManager] {key} in cooldown for {seconds}s")
|
| 85 |
+
|
| 86 |
+
def get_available_model(self) -> Optional[dict]:
|
| 87 |
+
"""Return the first model that has remaining quota, or None."""
|
| 88 |
+
with self._lock:
|
| 89 |
+
for model in MODEL_POOL:
|
| 90 |
+
if self._is_available(model):
|
| 91 |
+
return model
|
| 92 |
+
return None
|
| 93 |
+
|
| 94 |
+
def call_with_fallback(self, system_prompt: str) -> tuple[str, str]:
|
| 95 |
+
"""
|
| 96 |
+
Try each model in order. On success return (response_text, model_key).
|
| 97 |
+
On 429 / quota error, mark the model as cooled down and try the next.
|
| 98 |
+
Raises RuntimeError if all models are exhausted.
|
| 99 |
+
"""
|
| 100 |
+
import google.api_core.exceptions as gex
|
| 101 |
+
|
| 102 |
+
with self._lock:
|
| 103 |
+
candidates = [m for m in MODEL_POOL if self._is_available(m)]
|
| 104 |
+
|
| 105 |
+
if not candidates:
|
| 106 |
+
raise RuntimeError("All models are rate-limited. Try again later.")
|
| 107 |
+
|
| 108 |
+
for model_info in candidates:
|
| 109 |
+
key = model_info["key"]
|
| 110 |
+
try:
|
| 111 |
+
genai_model = genai.GenerativeModel(
|
| 112 |
+
key,
|
| 113 |
+
generation_config={"response_mime_type": "application/json"},
|
| 114 |
+
)
|
| 115 |
+
response = genai_model.generate_content(system_prompt)
|
| 116 |
+
|
| 117 |
+
with self._lock:
|
| 118 |
+
self._record_call(key)
|
| 119 |
+
|
| 120 |
+
print(f"[ModelManager] Used: {key}")
|
| 121 |
+
return response.text, key
|
| 122 |
+
|
| 123 |
+
except gex.ResourceExhausted as e:
|
| 124 |
+
print(f"[ModelManager] 429 on {key}: {e}")
|
| 125 |
+
with self._lock:
|
| 126 |
+
self._set_cooldown(key, seconds=65)
|
| 127 |
+
continue # try next model
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"[ModelManager] Error on {key}: {e}")
|
| 131 |
+
continue # skip broken model, try next
|
| 132 |
+
|
| 133 |
+
raise RuntimeError("All models failed or are rate-limited.")
|
| 134 |
+
|
| 135 |
+
def status(self) -> list[dict]:
|
| 136 |
+
"""Return current usage snapshot for all models (useful for /api/models endpoint)."""
|
| 137 |
+
now = time.time()
|
| 138 |
+
result = []
|
| 139 |
+
with self._lock:
|
| 140 |
+
for m in MODEL_POOL:
|
| 141 |
+
key = m["key"]
|
| 142 |
+
self._prune(self._minute_calls[key], 60)
|
| 143 |
+
self._prune(self._day_calls[key], 86_400)
|
| 144 |
+
cooldown_remaining = max(0, self._cooldown_until[key] - now)
|
| 145 |
+
result.append({
|
| 146 |
+
"key": key,
|
| 147 |
+
"name": m["name"],
|
| 148 |
+
"rpm_limit": m["rpm"],
|
| 149 |
+
"rpd_limit": m["rpd"],
|
| 150 |
+
"rpm_used": len(self._minute_calls[key]),
|
| 151 |
+
"rpd_used": len(self._day_calls[key]),
|
| 152 |
+
"available": self._is_available(m),
|
| 153 |
+
"cooldown_seconds": round(cooldown_remaining),
|
| 154 |
+
})
|
| 155 |
+
return result
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# Singleton β import this in main.py
|
| 159 |
+
model_manager = ModelManager()
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
google-generativeai
|
| 4 |
+
python-dotenv
|
| 5 |
+
google-genai
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
transformers
|
| 10 |
+
torch
|
| 11 |
+
torchvision
|
| 12 |
+
torchaudio
|