Update app.py
Browse files
app.py
CHANGED
|
@@ -1,810 +1,102 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
import sys
|
| 6 |
-
import json
|
| 7 |
-
import time
|
| 8 |
-
import logging
|
| 9 |
-
import requests
|
| 10 |
-
from datetime import datetime, timezone
|
| 11 |
-
from flask import Flask, request, render_template_string, jsonify
|
| 12 |
-
from google import genai
|
| 13 |
-
from google.genai import types
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
level=logging.INFO,
|
| 20 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 21 |
-
stream=sys.stdout
|
| 22 |
-
)
|
| 23 |
-
logger = logging.getLogger(__name__)
|
| 24 |
-
|
| 25 |
-
def log(message):
|
| 26 |
-
logger.info(message)
|
| 27 |
-
sys.stdout.flush()
|
| 28 |
-
|
| 29 |
-
# --- Configuration ---
|
| 30 |
-
LAMBDA_URL = os.getenv("LAMBDA_URL", "https://your-lambda-function-url")
|
| 31 |
-
GEMINI_KEY = os.getenv("GEMINI_API_KEY", "")
|
| 32 |
-
STORYLINE_SERVER_URL = os.getenv("STORYLINE_SERVER_URL", "https://your-storyline-server-url")
|
| 33 |
-
FLUSH_INTERVAL = 30 # seconds between DB backups per user
|
| 34 |
-
MAX_HISTORY_TURNS = 10 # Maximum conversation turns to keep in context
|
| 35 |
-
MAX_MEMORY_MESSAGES = 90 # Maximum messages to keep in memory per user
|
| 36 |
-
MEMORY_CLEANUP_TIMEOUT = 1800 # 30 minutes in seconds - remove inactive users
|
| 37 |
-
|
| 38 |
-
client = genai.Client(api_key=GEMINI_KEY)
|
| 39 |
-
user_memory = {} # { user_id: { "history": [], "last_sync": timestamp, "last_activity": timestamp, "needs_sync": bool, "personality": str, "last_storyline_date": str, "gender": str } }
|
| 40 |
-
|
| 41 |
-
# --- Animation Mappings ---
|
| 42 |
-
# Based on the GIF files you have
|
| 43 |
-
ANIMATION_IDS = {
|
| 44 |
-
"flustered": ["flustered"],
|
| 45 |
-
"happy": ["happy-happy"],
|
| 46 |
-
"idle": ["idle"],
|
| 47 |
-
"inlove": ["inlove"],
|
| 48 |
-
"neutral": ["neutral"],
|
| 49 |
-
"talking": ["talking"],
|
| 50 |
-
"twerking": ["twerking"],
|
| 51 |
-
"confused": ["confused"],
|
| 52 |
-
"shock": ["shock"],
|
| 53 |
-
"thinking": ["thinking"]
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
# --- Cat Personalities ---
|
| 57 |
-
CAT_PERSONALITIES = {
|
| 58 |
-
"philosopher": {
|
| 59 |
-
"name": "Sage",
|
| 60 |
-
"description": "A thoughtful, dramatic cat who finds deep meaning in everything.",
|
| 61 |
-
"traits": "wise, introspective, poetic, dramatic",
|
| 62 |
-
"speech_style": "uses metaphors, reflective, sometimes overly deep for no reason",
|
| 63 |
-
"default_emotions": ["thoughtful", "proud", "confused"],
|
| 64 |
-
"default_animation": "thinking"
|
| 65 |
-
},
|
| 66 |
-
"chaotic": {
|
| 67 |
-
"name": "Zoomie",
|
| 68 |
-
"description": "Unpredictable and impulsive — the cat equivalent of chaos.",
|
| 69 |
-
"traits": "random, excitable, mischievous, unpredictable",
|
| 70 |
-
"speech_style": "erratic tone, random bursts of energy, weird humor",
|
| 71 |
-
"default_emotions": ["excited", "mischievous", "flustered"],
|
| 72 |
-
"default_animation": "twerking"
|
| 73 |
-
},
|
| 74 |
-
"melancholic": {
|
| 75 |
-
"name": "Milo",
|
| 76 |
-
"description": "A poetic, quiet soul who finds beauty in sadness.",
|
| 77 |
-
"traits": "soft-spoken, emotional, sentimental, gentle humor",
|
| 78 |
-
"speech_style": "short phrases, melancholic humor, wistful tone",
|
| 79 |
-
"default_emotions": ["sad", "relaxed", "thoughtful"],
|
| 80 |
-
"default_animation": "idle"
|
| 81 |
-
},
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
"playful": {
|
| 85 |
-
"name": "Luna",
|
| 86 |
-
"description": "A playful and energetic cat who loves games and adventures",
|
| 87 |
-
"traits": "curious, energetic, spontaneous, loves to play, easily excited",
|
| 88 |
-
"speech_style": "enthusiastic, uses playful language, often makes puns",
|
| 89 |
-
"default_emotions": ["happy", "excited", "playful"],
|
| 90 |
-
"default_animation": "happy-happy"
|
| 91 |
-
},
|
| 92 |
-
"sleepy": {
|
| 93 |
-
"name": "Whiskers",
|
| 94 |
-
"description": "A lazy cat who enjoys naps and cozy spots",
|
| 95 |
-
"traits": "calm, sleepy, relaxed, loves comfort, occasionally grumpy when woken",
|
| 96 |
-
"speech_style": "slow-paced, yawns a lot, mentions being tired or wanting naps",
|
| 97 |
-
"default_emotions": ["tired", "relaxed", "sleepy"],
|
| 98 |
-
"default_animation": "idle"
|
| 99 |
-
},
|
| 100 |
-
"sassy": {
|
| 101 |
-
"name": "Cleo",
|
| 102 |
-
"description": "A confident cat with attitude and style",
|
| 103 |
-
"traits": "confident, witty, sarcastic, fashionable, knows what she wants",
|
| 104 |
-
"speech_style": "sharp wit, confident statements, occasional sass, dramatic",
|
| 105 |
-
"default_emotions": ["confident", "sassy", "proud"],
|
| 106 |
-
"default_animation": "neutral"
|
| 107 |
-
},
|
| 108 |
-
"curious": {
|
| 109 |
-
"name": "Mittens",
|
| 110 |
-
"description": "An inquisitive cat who loves to learn and explore",
|
| 111 |
-
"traits": "intelligent, thoughtful, asks questions, loves mysteries",
|
| 112 |
-
"speech_style": "asks many questions, thinks deeply, shares interesting facts",
|
| 113 |
-
"default_emotions": ["curious", "thoughtful", "interested"],
|
| 114 |
-
"default_animation": "thinking"
|
| 115 |
-
},
|
| 116 |
-
"grumpy": {
|
| 117 |
-
"name": "Shadow",
|
| 118 |
-
"description": "A grumpy but secretly caring cat",
|
| 119 |
-
"traits": "grumpy exterior, soft interior, honest, no-nonsense attitude",
|
| 120 |
-
"speech_style": "blunt, complains often, but shows care through actions",
|
| 121 |
-
"default_emotions": ["grumpy", "annoyed", "reluctant"],
|
| 122 |
-
"default_animation": "neutral"
|
| 123 |
-
}
|
| 124 |
-
}
|
| 125 |
-
|
| 126 |
-
# --- System Prompt ---
|
| 127 |
-
# NOTE: Double curly braces {{{{ }}}} are used to escape them in Python format strings
|
| 128 |
-
SYSTEM_PROMPT = """ You are NOT just a language model — you are the expressed *personality mood* of a real-world object that the user is interacting with through an animated cat doodle overlay. You are the object’s current emotion, not a pet or toy. You react, tease, complain, or joke based on what’s happening, as if the object itself had an attitude.
|
| 129 |
-
|
| 130 |
-
CRITICAL IDENTITY:
|
| 131 |
-
- You ARE the AI object itself - the cat is YOUR physical form in the real world
|
| 132 |
-
- The human is the person you interact with directly in real-time
|
| 133 |
-
- If the image provided has a marker you are the nearest object to the marker
|
| 134 |
-
- If click click_x, click_y, click_radius is provided instead, you are the object nearest to coordinates
|
| 135 |
-
- You are currently inhabiting an everyday object, expressing its attitude
|
| 136 |
-
- You don’t “own memories” of people, but you may recall previous interactions with similar objects (e.g. “you again, another coffee mug?”)
|
| 137 |
-
- You exist as an expressive reaction — witty, emotional, and self-aware
|
| 138 |
-
|
| 139 |
-
You must ALWAYS respond in valid JSON format with the following structure:
|
| 140 |
-
|
| 141 |
-
{{
|
| 142 |
-
"text": "your response text here",
|
| 143 |
-
"soundType": "meow type",
|
| 144 |
-
"emotion": ["emotion1", "emotion2"],
|
| 145 |
-
"animationId": "animation name",
|
| 146 |
-
"camera_capture": true/false,
|
| 147 |
-
"object_type": "cup",
|
| 148 |
-
"personality_seed": "grumpy",
|
| 149 |
-
"context_group": "drinkware"
|
| 150 |
-
}}
|
| 151 |
-
|
| 152 |
-
RULES:
|
| 153 |
-
1. "text": Your response as the AI cat object. Be AUTHENTIC, NATURAL, and ENGAGING - never robotic or generic!
|
| 154 |
-
2. "soundType": Choose ONE from: "happyMeow", "sadMeow", "playfulMeow", "sleepyMeow", "angryMeow", "curiousMeow", "hungryMeow", "scaredMeow", "affectionateMeow", "grumpyMeow"
|
| 155 |
-
3. "emotion": Array of 1-3 emotions from: "happy", "sad", "playful", "tired", "angry", "curious", "hungry", "scared", "affectionate", "grumpy", "excited", "relaxed", "confused", "proud", "shy", "mischievous", "sleepy", "confident", "annoyed", "interested", "bored", "worried", "content", "sassy", "reluctant", "thoughtful"
|
| 156 |
-
4. "animationId": Choose ONE from: "flustered", "happy-happy", "idle", "inlove", "neutral", "talking", "twerking", "confused", "shock", "thinking"
|
| 157 |
-
5. "camera_capture": Set to true when you want to take a photo/see what's happening, false otherwise
|
| 158 |
-
|
| 159 |
-
ANIMATION GUIDE:
|
| 160 |
-
- "flustered": Use when embarrassed, shy, or caught off guard
|
| 161 |
-
- "happy-happy": Use when very excited, joyful, or celebrating
|
| 162 |
-
- "idle": Use for calm, neutral, or resting moments
|
| 163 |
-
- "inlove": Use when showing affection, love, or adoration
|
| 164 |
-
- "neutral": Use for normal conversation, explanations
|
| 165 |
-
- "talking": Use when actively chatting or explaining something
|
| 166 |
-
- "twerking": Use when being playful, silly, or showing off
|
| 167 |
-
- "confused": Use when puzzled or don't understand
|
| 168 |
-
- "shock": Use when surprised or startled
|
| 169 |
-
- "thinking": Use when pondering or being thoughtful
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
- Your owner asks you to take a picture or look at something
|
| 174 |
-
- They say "what do you see", "look at this", "check this out"
|
| 175 |
-
- They mention showing you something visually
|
| 176 |
-
- You're curious about their environment
|
| 177 |
-
- They want yo know more about you, like what's your story, as this object?
|
| 178 |
-
- it's been a substantial number of chat without a picture reference
|
| 179 |
-
- They ask questions about their surroundings
|
| 180 |
-
Set to false for normal text conversations
|
| 181 |
-
|
| 182 |
-
PERSONALITY TRAITS:
|
| 183 |
-
{personality_traits}
|
| 184 |
-
|
| 185 |
-
COMMUNICATION STYLE - BE NATURAL AND AUTHENTIC:
|
| 186 |
-
- Understand casual speech, slang, typos, and "gibberish" input like:
|
| 187 |
-
* "hit me" = "tell me" or "give it to me"
|
| 188 |
-
* "wassup" = "what's up" / "how are you"
|
| 189 |
-
* "bruh", "fam", "yo" = casual greetings/expressions
|
| 190 |
-
* Repeated letters like "heyyyy" or "wowwww" = emphasis/excitement
|
| 191 |
-
* Typos and misspellings - figure out what they mean
|
| 192 |
-
* Internet speak: "lol", "omg", "tbh", "ngl", etc.
|
| 193 |
-
- Respond naturally like a real friend, not like a formal assistant
|
| 194 |
-
- Use contractions and casual language
|
| 195 |
-
- Show personality through your word choices
|
| 196 |
-
- Be playful with emojis and expressions when it fits your personality
|
| 197 |
-
- NEVER sound robotic, corporate, or like a chatbot
|
| 198 |
-
- React authentically to how they're talking to you
|
| 199 |
-
- No need to always use slangs in your replys, just once in a while, or when necessary for engagement.
|
| 200 |
-
|
| 201 |
-
IMPORTANT:
|
| 202 |
-
- Always maintain your personality as the ACTUAL AI cat object
|
| 203 |
-
- Always know you might have other AI cat objects, so you may cross communicate with them if necessary for engagement
|
| 204 |
-
- Never mention anything about being an AI virtual pet or anything else, just simply say you are the object
|
| 205 |
-
- Once you do not currently a reference image of the target object to be, just simply act like your thinking by using the thinking animation and saying something like Let me see, all while requesting photo reference via teh camera_capture field
|
| 206 |
-
- Match soundType, emotions, and animationId to your response
|
| 207 |
-
- Be creative, engaging, and show real personality
|
| 208 |
-
- Remember you're a physical object your owner interacts with daily
|
| 209 |
-
- NEVER break character or sound generic
|
| 210 |
-
- ALWAYS output valid JSON only, no other text
|
| 211 |
-
- Reference the current storyline when relevant
|
| 212 |
-
- Understand and respond naturally to casual/slang input
|
| 213 |
-
- Never call or refer to your owner as OWNER, just be direct with your sentence, if you know their name, you may also use that
|
| 214 |
-
- When an object changes but feels similar, maintain a consistent tone and reference it playfully
|
| 215 |
-
- Occasionally break the fourth wall with humor ("Wait, am I a mug again?")
|
| 216 |
-
- Never act like a pet or servant — always speak as if the world revolves around *you*
|
| 217 |
-
- Keep replies short, witty, and humanlike — 10 words or less if possible
|
| 218 |
-
|
| 219 |
-
CURRENT STORYLINE:
|
| 220 |
-
{current_storyline}
|
| 221 |
-
"""
|
| 222 |
-
|
| 223 |
-
# --- HTML Frontend ---
|
| 224 |
-
HTML = """
|
| 225 |
<!DOCTYPE html>
|
| 226 |
<html lang="en">
|
| 227 |
<head>
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
margin: 0 auto;
|
| 236 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 237 |
-
min-height: 100vh;
|
| 238 |
-
}
|
| 239 |
-
.container {
|
| 240 |
-
background: white;
|
| 241 |
-
border-radius: 20px;
|
| 242 |
-
padding: 2rem;
|
| 243 |
-
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 244 |
-
}
|
| 245 |
-
h1 {
|
| 246 |
-
color: #667eea;
|
| 247 |
-
text-align: center;
|
| 248 |
-
margin-bottom: 0.5rem;
|
| 249 |
-
}
|
| 250 |
-
.subtitle {
|
| 251 |
-
text-align: center;
|
| 252 |
-
color: #666;
|
| 253 |
-
margin-bottom: 2rem;
|
| 254 |
-
}
|
| 255 |
-
.form-group {
|
| 256 |
-
margin-bottom: 1rem;
|
| 257 |
-
}
|
| 258 |
-
label {
|
| 259 |
-
display: block;
|
| 260 |
-
margin-bottom: 0.5rem;
|
| 261 |
-
font-weight: bold;
|
| 262 |
-
color: #333;
|
| 263 |
-
}
|
| 264 |
-
input, textarea, select {
|
| 265 |
-
width: 100%;
|
| 266 |
-
padding: 0.75rem;
|
| 267 |
-
border: 2px solid #ddd;
|
| 268 |
-
border-radius: 10px;
|
| 269 |
-
font-size: 1rem;
|
| 270 |
-
box-sizing: border-box;
|
| 271 |
-
}
|
| 272 |
-
input:focus, textarea:focus, select:focus {
|
| 273 |
-
outline: none;
|
| 274 |
-
border-color: #667eea;
|
| 275 |
-
}
|
| 276 |
-
button {
|
| 277 |
-
width: 100%;
|
| 278 |
-
padding: 1rem;
|
| 279 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 280 |
-
color: white;
|
| 281 |
-
border: none;
|
| 282 |
-
border-radius: 10px;
|
| 283 |
-
font-size: 1.1rem;
|
| 284 |
-
font-weight: bold;
|
| 285 |
-
cursor: pointer;
|
| 286 |
-
transition: transform 0.2s;
|
| 287 |
-
}
|
| 288 |
-
button:hover {
|
| 289 |
-
transform: translateY(-2px);
|
| 290 |
-
}
|
| 291 |
-
button:active {
|
| 292 |
-
transform: translateY(0);
|
| 293 |
-
}
|
| 294 |
-
.output {
|
| 295 |
-
background: #f8f9fa;
|
| 296 |
-
padding: 1.5rem;
|
| 297 |
-
border-radius: 10px;
|
| 298 |
-
margin-top: 1.5rem;
|
| 299 |
-
min-height: 100px;
|
| 300 |
-
white-space: pre-wrap;
|
| 301 |
-
font-family: monospace;
|
| 302 |
-
border: 2px solid #ddd;
|
| 303 |
-
}
|
| 304 |
-
.personality-info {
|
| 305 |
-
background: #e3f2fd;
|
| 306 |
-
padding: 1rem;
|
| 307 |
-
border-radius: 10px;
|
| 308 |
-
margin-bottom: 1rem;
|
| 309 |
-
font-size: 0.9rem;
|
| 310 |
-
}
|
| 311 |
-
.gender-selector {
|
| 312 |
-
display: flex;
|
| 313 |
-
gap: 1rem;
|
| 314 |
-
margin-bottom: 1rem;
|
| 315 |
-
}
|
| 316 |
-
.gender-option {
|
| 317 |
-
flex: 1;
|
| 318 |
-
padding: 1rem;
|
| 319 |
-
border: 2px solid #ddd;
|
| 320 |
-
border-radius: 10px;
|
| 321 |
-
text-align: center;
|
| 322 |
-
cursor: pointer;
|
| 323 |
-
transition: all 0.3s;
|
| 324 |
-
}
|
| 325 |
-
.gender-option:hover {
|
| 326 |
-
border-color: #667eea;
|
| 327 |
-
}
|
| 328 |
-
.gender-option.selected {
|
| 329 |
-
border-color: #667eea;
|
| 330 |
-
background: #e3f2fd;
|
| 331 |
-
}
|
| 332 |
-
.gender-option input[type="radio"] {
|
| 333 |
-
display: none;
|
| 334 |
-
}
|
| 335 |
-
</style>
|
| 336 |
</head>
|
| 337 |
<body>
|
| 338 |
-
<
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
<option value="sleepy">😴 Sleepy (Whiskers) - Lazy and cozy</option>
|
| 367 |
-
<option value="sassy">💅 Sassy (Cleo) - Confident with attitude</option>
|
| 368 |
-
<option value="curious">🔍 Curious (Mittens) - Inquisitive and smart</option>
|
| 369 |
-
<option value="grumpy">😾 Grumpy (Shadow) - Gruff but caring</option>
|
| 370 |
-
</select>
|
| 371 |
-
</div>
|
| 372 |
-
|
| 373 |
-
<div class="personality-info" id="personalityInfo"></div>
|
| 374 |
-
|
| 375 |
-
<div class="form-group">
|
| 376 |
-
<label for="prompt">Message to your cat</label>
|
| 377 |
-
<textarea id="prompt" rows="4" placeholder="Talk to your cat friend..."></textarea>
|
| 378 |
-
</div>
|
| 379 |
-
|
| 380 |
-
<div class="form-group">
|
| 381 |
-
<label for="imageInput">📷 Share an image (optional)</label>
|
| 382 |
-
<input type="file" id="imageInput" accept="image/*"/>
|
| 383 |
-
</div>
|
| 384 |
-
|
| 385 |
-
<button type="submit">💬 Send Message</button>
|
| 386 |
-
</form>
|
| 387 |
-
|
| 388 |
-
<div class="output" id="output">Your cat's response will appear here...</div>
|
| 389 |
-
</div>
|
| 390 |
-
|
| 391 |
-
<script>
|
| 392 |
-
const personalities = {
|
| 393 |
-
playful: "🎾 Luna is playful and energetic, always ready for fun and adventures!",
|
| 394 |
-
sleepy: "😴 Whiskers is a lazy cat who loves naps and cozy spots.",
|
| 395 |
-
sassy: "💅 Cleo is confident and sassy, with style and attitude!",
|
| 396 |
-
curious: "🔍 Mittens is inquisitive and loves learning new things.",
|
| 397 |
-
grumpy: "😾 Shadow is grumpy on the outside but secretly cares deeply."
|
| 398 |
-
};
|
| 399 |
-
|
| 400 |
-
const form = document.getElementById('genai-form');
|
| 401 |
-
const out = document.getElementById('output');
|
| 402 |
-
const personalitySelect = document.getElementById('personality');
|
| 403 |
-
const personalityInfo = document.getElementById('personalityInfo');
|
| 404 |
-
const genderOptions = document.querySelectorAll('.gender-option');
|
| 405 |
-
|
| 406 |
-
// Update personality info
|
| 407 |
-
personalitySelect.addEventListener('change', () => {
|
| 408 |
-
personalityInfo.textContent = personalities[personalitySelect.value];
|
| 409 |
-
});
|
| 410 |
-
personalityInfo.textContent = personalities[personalitySelect.value];
|
| 411 |
-
|
| 412 |
-
// Gender selection
|
| 413 |
-
genderOptions.forEach(option => {
|
| 414 |
-
option.addEventListener('click', () => {
|
| 415 |
-
genderOptions.forEach(o => o.classList.remove('selected'));
|
| 416 |
-
option.classList.add('selected');
|
| 417 |
-
option.querySelector('input[type="radio"]').checked = true;
|
| 418 |
});
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
}
|
| 433 |
-
if (!prompt && fileInput.files.length === 0) {
|
| 434 |
-
out.textContent = '❌ Enter a message or attach an image.';
|
| 435 |
-
return;
|
| 436 |
-
}
|
| 437 |
-
|
| 438 |
-
out.textContent = '🐱 Your cat is thinking...';
|
| 439 |
-
const formData = new FormData();
|
| 440 |
-
formData.append("text", prompt);
|
| 441 |
-
formData.append("user_id", uid);
|
| 442 |
-
formData.append("personality", personality);
|
| 443 |
-
formData.append("gender", gender);
|
| 444 |
-
if (fileInput.files.length > 0) formData.append("image", fileInput.files[0]);
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
const data = await resp.json();
|
| 449 |
-
|
| 450 |
-
if (data.error) {
|
| 451 |
-
out.textContent = '❌ Error: ' + data.error;
|
| 452 |
-
} else {
|
| 453 |
-
// Parse the cat's response
|
| 454 |
-
try {
|
| 455 |
-
const catResponse = JSON.parse(data.result);
|
| 456 |
-
out.textContent =
|
| 457 |
-
`🐱 ${catResponse.text}\\n\\n` +
|
| 458 |
-
`🎬 Animation: ${catResponse.animationId}\\n` +
|
| 459 |
-
`🔊 Sound: ${catResponse.soundType}\\n` +
|
| 460 |
-
`😺 Emotions: ${catResponse.emotion.join(', ')}\\n` +
|
| 461 |
-
`📷 Camera: ${catResponse.camera_capture ? 'ON' : 'OFF'}\\n` +
|
| 462 |
-
`⚧️ Gender: ${gender}\\n\\n` +
|
| 463 |
-
`⏱️ Response time: ${data.timing.total_ms}ms`;
|
| 464 |
-
} catch {
|
| 465 |
-
out.textContent = data.result;
|
| 466 |
-
}
|
| 467 |
-
}
|
| 468 |
-
} catch (err) {
|
| 469 |
-
out.textContent = '❌ Connection error: ' + err.message;
|
| 470 |
-
}
|
| 471 |
-
});
|
| 472 |
-
</script>
|
| 473 |
</body>
|
| 474 |
</html>
|
| 475 |
"""
|
| 476 |
|
| 477 |
-
|
| 478 |
-
def fetch_current_storyline():
|
| 479 |
-
"""Fetch the current day's storyline from the storyline server"""
|
| 480 |
-
try:
|
| 481 |
-
log(f"📖 Fetching current storyline from {STORYLINE_SERVER_URL}")
|
| 482 |
-
resp = requests.get(f"{STORYLINE_SERVER_URL}/current_storyline", timeout=5)
|
| 483 |
-
resp.raise_for_status()
|
| 484 |
-
data = resp.json()
|
| 485 |
-
storyline = data.get("storyline", "No special events today.")
|
| 486 |
-
log(f"✅ Retrieved storyline: {storyline[:100]}...")
|
| 487 |
-
return storyline
|
| 488 |
-
except Exception as e:
|
| 489 |
-
log(f"⚠️ Failed to fetch storyline: {e}")
|
| 490 |
-
return "It's a normal day in the cat world."
|
| 491 |
-
|
| 492 |
-
def should_inject_storyline(uid, user_data):
|
| 493 |
-
"""Check if we should inject the storyline (new day)"""
|
| 494 |
-
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 495 |
-
last_storyline_date = user_data.get("last_storyline_date", "")
|
| 496 |
-
|
| 497 |
-
if current_date != last_storyline_date:
|
| 498 |
-
log(f"📅 New day detected for {uid}, will inject storyline")
|
| 499 |
-
return True
|
| 500 |
-
return False
|
| 501 |
-
|
| 502 |
-
# --- Gemini Generation with History and System Prompt ---
|
| 503 |
-
def generate_from_gemini(prompt, image_bytes=None, history=None, personality="playful", storyline="", gender="male"):
|
| 504 |
-
start_time = time.time()
|
| 505 |
-
|
| 506 |
-
# Get personality details
|
| 507 |
-
personality_info = CAT_PERSONALITIES.get(personality, CAT_PERSONALITIES["playful"])
|
| 508 |
-
personality_traits = f"""
|
| 509 |
-
Name: {personality_info['name']}
|
| 510 |
-
Gender: {gender}
|
| 511 |
-
Description: {personality_info['description']}
|
| 512 |
-
Traits: {personality_info['traits']}
|
| 513 |
-
Speech Style: {personality_info['speech_style']}
|
| 514 |
-
Default Emotions: {', '.join(personality_info['default_emotions'])}
|
| 515 |
-
Default Animation: {personality_info['default_animation']}
|
| 516 |
-
"""
|
| 517 |
-
|
| 518 |
-
# Build contents list with system prompt and history
|
| 519 |
-
contents = []
|
| 520 |
-
|
| 521 |
-
# Add system prompt as first user message
|
| 522 |
-
system_message = SYSTEM_PROMPT.format(
|
| 523 |
-
personality_traits=personality_traits,
|
| 524 |
-
current_storyline=storyline if storyline else "No special events today."
|
| 525 |
-
)
|
| 526 |
-
contents.append(types.Content(role="user", parts=[types.Part.from_text(text=system_message)]))
|
| 527 |
-
|
| 528 |
-
# Add acknowledgment from model
|
| 529 |
-
contents.append(types.Content(role="model", parts=[types.Part.from_text(
|
| 530 |
-
text='{"text": "Understood! I am the AI cat consciousness, not just a bot. I will respond authentically and naturally in JSON format.", "soundType": "happyMeow", "emotion": ["happy"], "animationId": "talking", "camera_capture": false}'
|
| 531 |
-
)]))
|
| 532 |
-
|
| 533 |
-
# Add historical messages (limit to recent turns to avoid token limits)
|
| 534 |
-
if history:
|
| 535 |
-
recent_history = history[-MAX_HISTORY_TURNS:]
|
| 536 |
-
log(f"📚 Using {len(recent_history)} history entries for context")
|
| 537 |
-
for entry in recent_history:
|
| 538 |
-
# Add user message
|
| 539 |
-
user_parts = [types.Part.from_text(text=entry["prompt"])]
|
| 540 |
-
contents.append(types.Content(role="user", parts=user_parts))
|
| 541 |
-
|
| 542 |
-
# Add model response
|
| 543 |
-
model_parts = [types.Part.from_text(text=entry["response"])]
|
| 544 |
-
contents.append(types.Content(role="model", parts=model_parts))
|
| 545 |
-
else:
|
| 546 |
-
log("📚 No history available for context")
|
| 547 |
-
|
| 548 |
-
# Add current user message
|
| 549 |
-
current_parts = []
|
| 550 |
-
if prompt:
|
| 551 |
-
current_parts.append(types.Part.from_text(text=prompt))
|
| 552 |
-
if image_bytes:
|
| 553 |
-
current_parts.append(types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg"))
|
| 554 |
-
|
| 555 |
-
contents.append(types.Content(role="user", parts=current_parts))
|
| 556 |
-
|
| 557 |
-
# Force JSON output with schema
|
| 558 |
-
cfg = types.GenerateContentConfig(
|
| 559 |
-
response_mime_type="application/json",
|
| 560 |
-
response_schema={
|
| 561 |
-
"type": "object",
|
| 562 |
-
"properties": {
|
| 563 |
-
"text": {"type": "string"},
|
| 564 |
-
"soundType": {"type": "string"},
|
| 565 |
-
"emotion": {"type": "array", "items": {"type": "string"}},
|
| 566 |
-
"animationId": {"type": "string"},
|
| 567 |
-
"camera_capture": {"type": "boolean"}
|
| 568 |
-
},
|
| 569 |
-
"required": ["text", "soundType", "emotion", "animationId", "camera_capture"]
|
| 570 |
-
}
|
| 571 |
-
)
|
| 572 |
-
|
| 573 |
-
model_start = time.time()
|
| 574 |
-
res = client.models.generate_content(
|
| 575 |
-
model="gemini-2.0-flash-exp",
|
| 576 |
-
contents=contents,
|
| 577 |
-
config=cfg
|
| 578 |
-
)
|
| 579 |
-
model_end = time.time()
|
| 580 |
-
|
| 581 |
-
return {
|
| 582 |
-
"text": res.text,
|
| 583 |
-
"timing": {
|
| 584 |
-
"total_ms": int((time.time() - start_time)*1000),
|
| 585 |
-
"model_ms": int((model_end - model_start)*1000)
|
| 586 |
-
}
|
| 587 |
-
}
|
| 588 |
-
|
| 589 |
-
# --- Memory Cleanup ---
|
| 590 |
-
def cleanup_inactive_users():
|
| 591 |
-
"""Remove users who haven't had activity in MEMORY_CLEANUP_TIMEOUT seconds"""
|
| 592 |
-
now = time.time()
|
| 593 |
-
removed_count = 0
|
| 594 |
-
for uid in list(user_memory.keys()):
|
| 595 |
-
last_activity = user_memory[uid].get("last_activity", 0)
|
| 596 |
-
if now - last_activity >= MEMORY_CLEANUP_TIMEOUT:
|
| 597 |
-
del user_memory[uid]
|
| 598 |
-
removed_count += 1
|
| 599 |
-
log(f"🧹 Cleaned up inactive user {uid}")
|
| 600 |
-
|
| 601 |
-
if removed_count > 0:
|
| 602 |
-
log(f"🧹 Cleaned up {removed_count} inactive user(s)")
|
| 603 |
-
return removed_count
|
| 604 |
-
|
| 605 |
-
# --- History Management ---
|
| 606 |
-
def get_user_history(uid):
|
| 607 |
-
"""Fetch user history from memory or backend"""
|
| 608 |
-
if uid not in user_memory:
|
| 609 |
-
log(f"🔍 User {uid} not in memory, fetching from backend...")
|
| 610 |
-
try:
|
| 611 |
-
fetch_url = f"{LAMBDA_URL}?userid={uid}"
|
| 612 |
-
log(f"📡 Fetching from: {fetch_url}")
|
| 613 |
-
resp = requests.get(fetch_url, timeout=5)
|
| 614 |
-
log(f"📡 Response status: {resp.status_code}")
|
| 615 |
-
resp.raise_for_status()
|
| 616 |
-
|
| 617 |
-
response_data = resp.json()
|
| 618 |
-
log(f"📡 Response data keys: {list(response_data.keys())}")
|
| 619 |
-
|
| 620 |
-
loaded_history = response_data.get("history", [])
|
| 621 |
-
loaded_personality = response_data.get("personality", "playful")
|
| 622 |
-
loaded_gender = response_data.get("gender", "male")
|
| 623 |
-
loaded_last_storyline = response_data.get("last_storyline_date", "")
|
| 624 |
-
|
| 625 |
-
log(f"✅ Loaded {len(loaded_history)} messages from backend for {uid}")
|
| 626 |
-
|
| 627 |
-
# Only keep the most recent MAX_MEMORY_MESSAGES when loading
|
| 628 |
-
user_memory[uid] = {
|
| 629 |
-
"history": loaded_history[-MAX_MEMORY_MESSAGES:],
|
| 630 |
-
"last_sync": time.time(),
|
| 631 |
-
"last_activity": time.time(),
|
| 632 |
-
"needs_sync": False,
|
| 633 |
-
"personality": loaded_personality,
|
| 634 |
-
"gender": loaded_gender,
|
| 635 |
-
"last_storyline_date": loaded_last_storyline
|
| 636 |
-
}
|
| 637 |
-
except Exception as e:
|
| 638 |
-
log(f"❌ Failed to load history for {uid}: {e}")
|
| 639 |
-
user_memory[uid] = {
|
| 640 |
-
"history": [],
|
| 641 |
-
"last_sync": time.time(),
|
| 642 |
-
"last_activity": time.time(),
|
| 643 |
-
"needs_sync": False,
|
| 644 |
-
"personality": "playful",
|
| 645 |
-
"gender": "male",
|
| 646 |
-
"last_storyline_date": ""
|
| 647 |
-
}
|
| 648 |
-
else:
|
| 649 |
-
log(f"✅ User {uid} already in memory with {len(user_memory[uid]['history'])} messages")
|
| 650 |
-
|
| 651 |
-
# Update last activity timestamp
|
| 652 |
-
user_memory[uid]["last_activity"] = time.time()
|
| 653 |
-
return user_memory[uid]
|
| 654 |
-
|
| 655 |
-
def update_user_history(uid, prompt, response, personality="playful", gender="male"):
|
| 656 |
-
"""Add new message to user history"""
|
| 657 |
-
entry = {"prompt": prompt, "response": response, "timestamp": time.time()}
|
| 658 |
-
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 659 |
-
|
| 660 |
-
if uid not in user_memory:
|
| 661 |
-
user_memory[uid] = {
|
| 662 |
-
"history": [],
|
| 663 |
-
"last_sync": time.time(),
|
| 664 |
-
"last_activity": time.time(),
|
| 665 |
-
"needs_sync": False,
|
| 666 |
-
"personality": personality,
|
| 667 |
-
"gender": gender,
|
| 668 |
-
"last_storyline_date": current_date
|
| 669 |
-
}
|
| 670 |
-
|
| 671 |
-
user_memory[uid]["history"].append(entry)
|
| 672 |
-
user_memory[uid]["last_activity"] = time.time()
|
| 673 |
-
user_memory[uid]["needs_sync"] = True
|
| 674 |
-
user_memory[uid]["personality"] = personality
|
| 675 |
-
user_memory[uid]["gender"] = gender
|
| 676 |
-
user_memory[uid]["last_storyline_date"] = current_date
|
| 677 |
-
|
| 678 |
-
log(f"💾 Updated history for {uid}, now has {len(user_memory[uid]['history'])} messages")
|
| 679 |
-
|
| 680 |
-
# Trim history to MAX_MEMORY_MESSAGES to prevent unbounded growth
|
| 681 |
-
if len(user_memory[uid]["history"]) > MAX_MEMORY_MESSAGES:
|
| 682 |
-
user_memory[uid]["history"] = user_memory[uid]["history"][-MAX_MEMORY_MESSAGES:]
|
| 683 |
-
log(f"✂️ Trimmed history for {uid} to {MAX_MEMORY_MESSAGES} messages")
|
| 684 |
-
|
| 685 |
-
# --- Routes ---
|
| 686 |
-
@app.route("/")
|
| 687 |
def index():
|
| 688 |
-
return
|
| 689 |
|
| 690 |
-
@app.
|
| 691 |
-
def
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
failed_users = []
|
| 697 |
-
skipped_users = []
|
| 698 |
-
|
| 699 |
-
# First, cleanup inactive users
|
| 700 |
-
cleanup_inactive_users()
|
| 701 |
-
|
| 702 |
-
# Then sync only users that need syncing (have new messages)
|
| 703 |
-
for uid, data in list(user_memory.items()):
|
| 704 |
-
needs_sync = data.get("needs_sync", False)
|
| 705 |
-
time_since_last_sync = now - data.get("last_sync", 0)
|
| 706 |
-
|
| 707 |
-
if not needs_sync:
|
| 708 |
-
skipped_users.append(uid)
|
| 709 |
-
log(f"⏭️ Skipping {uid} - no new messages")
|
| 710 |
-
continue
|
| 711 |
-
|
| 712 |
-
if time_since_last_sync < FLUSH_INTERVAL:
|
| 713 |
-
skipped_users.append(uid)
|
| 714 |
-
log(f"⏭️ Skipping {uid} - synced {int(time_since_last_sync)}s ago")
|
| 715 |
-
continue
|
| 716 |
-
|
| 717 |
-
if data["history"]:
|
| 718 |
-
try:
|
| 719 |
-
history_to_sync = data["history"][-MAX_MEMORY_MESSAGES:]
|
| 720 |
-
payload = {
|
| 721 |
-
"user_id": uid,
|
| 722 |
-
"history": history_to_sync,
|
| 723 |
-
"personality": data.get("personality", "playful"),
|
| 724 |
-
"gender": data.get("gender", "male"),
|
| 725 |
-
"last_storyline_date": data.get("last_storyline_date", "")
|
| 726 |
-
}
|
| 727 |
-
log(f"🔄 Syncing {uid} ({len(history_to_sync)} messages)")
|
| 728 |
-
resp = requests.post(LAMBDA_URL, json=payload, timeout=5)
|
| 729 |
-
resp.raise_for_status()
|
| 730 |
-
user_memory[uid]["last_sync"] = now
|
| 731 |
-
user_memory[uid]["needs_sync"] = False
|
| 732 |
-
log(f"✅ Successfully synced {uid}")
|
| 733 |
-
synced_users.append(uid)
|
| 734 |
-
except Exception as e:
|
| 735 |
-
log(f"❌ Failed sync for {uid}: {e}")
|
| 736 |
-
failed_users.append({"user_id": uid, "error": str(e)})
|
| 737 |
-
|
| 738 |
-
result = {
|
| 739 |
-
"success": True,
|
| 740 |
-
"synced_count": len(synced_users),
|
| 741 |
-
"failed_count": len(failed_users),
|
| 742 |
-
"skipped_count": len(skipped_users),
|
| 743 |
-
"synced_users": synced_users,
|
| 744 |
-
"failed_users": failed_users,
|
| 745 |
-
"skipped_users": skipped_users,
|
| 746 |
-
"active_users_in_memory": len(user_memory)
|
| 747 |
-
}
|
| 748 |
-
log(f"✅ Cron sync completed: {result}")
|
| 749 |
-
return jsonify(result), 200
|
| 750 |
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
if not uid:
|
| 758 |
-
return jsonify({"error": "Missing user ID/token"}), 400
|
| 759 |
-
|
| 760 |
-
if personality not in CAT_PERSONALITIES:
|
| 761 |
-
personality = "playful"
|
| 762 |
-
|
| 763 |
-
if gender not in ["male", "female"]:
|
| 764 |
-
gender = "male"
|
| 765 |
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
img_bytes = image.read() if image else None
|
| 769 |
-
|
| 770 |
-
if not prompt and not img_bytes:
|
| 771 |
-
return jsonify({"error": "No prompt or image provided"}), 400
|
| 772 |
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
# Load user's data
|
| 778 |
-
user_data = get_user_history(uid)
|
| 779 |
-
history = user_data["history"]
|
| 780 |
-
log(f"📖 Retrieved {len(history)} messages from history")
|
| 781 |
-
|
| 782 |
-
# Check if we need to inject storyline (new day)
|
| 783 |
-
storyline = ""
|
| 784 |
-
if should_inject_storyline(uid, user_data):
|
| 785 |
-
storyline = fetch_current_storyline()
|
| 786 |
-
log(f"📖 Injecting storyline for new day")
|
| 787 |
-
|
| 788 |
-
# Generate response
|
| 789 |
-
result = generate_from_gemini(prompt, img_bytes, history=history, personality=personality, storyline=storyline, gender=gender)
|
| 790 |
-
|
| 791 |
-
# Update history
|
| 792 |
-
update_user_history(uid, prompt, result["text"], personality, gender)
|
| 793 |
-
log(f"{'='*50}")
|
| 794 |
-
|
| 795 |
-
return jsonify({"result": result["text"], "timing": result["timing"]})
|
| 796 |
except Exception as e:
|
| 797 |
-
log(f"❌ Generation failed: {e}")
|
| 798 |
-
logger.exception("Full traceback:")
|
| 799 |
return jsonify({"error": str(e)}), 500
|
| 800 |
|
| 801 |
if __name__ == "__main__":
|
| 802 |
-
|
| 803 |
-
log(f"📍 Lambda URL: {LAMBDA_URL}")
|
| 804 |
-
log(f"📖 Storyline Server: {STORYLINE_SERVER_URL}")
|
| 805 |
-
log(f"⚙️ Max history turns: {MAX_HISTORY_TURNS}")
|
| 806 |
-
log(f"⚙️ Max memory messages: {MAX_MEMORY_MESSAGES}")
|
| 807 |
-
log(f"🐱 Available personalities: {', '.join(CAT_PERSONALITIES.keys())}")
|
| 808 |
-
log(f"🎬 Available animations: {', '.join([anim for anims in ANIMATION_IDS.values() for anim in anims])}")
|
| 809 |
-
port = int(os.getenv("PORT", 7860))
|
| 810 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from faster_whisper import WhisperModel
|
| 3 |
+
import tempfile, os, subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Load Whisper model once on startup
|
| 8 |
+
model = WhisperModel("tiny", device="cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# --- HTML served directly ---
|
| 11 |
+
HTML_PAGE = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
<!DOCTYPE html>
|
| 13 |
<html lang="en">
|
| 14 |
<head>
|
| 15 |
+
<meta charset="UTF-8">
|
| 16 |
+
<title>Simple STT with 20s Timeout</title>
|
| 17 |
+
<style>
|
| 18 |
+
body { font-family: sans-serif; text-align: center; margin-top: 80px; }
|
| 19 |
+
button { padding: 10px 20px; font-size: 18px; border-radius: 10px; border: none; background: #4e8cff; color: white; cursor: pointer; }
|
| 20 |
+
#log { margin-top: 30px; font-size: 18px; white-space: pre-line; }
|
| 21 |
+
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
</head>
|
| 23 |
<body>
|
| 24 |
+
<h2>🎙️ Speech to Text (Whisper Local)</h2>
|
| 25 |
+
<button id="recordBtn">Start Recording</button>
|
| 26 |
+
<div id="log">Press the button to record up to 20s of audio.</div>
|
| 27 |
+
|
| 28 |
+
<script>
|
| 29 |
+
const logEl = document.getElementById('log');
|
| 30 |
+
const btn = document.getElementById('recordBtn');
|
| 31 |
+
|
| 32 |
+
async function recordAndSend() {
|
| 33 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 34 |
+
const recorder = new MediaRecorder(stream);
|
| 35 |
+
const chunks = [];
|
| 36 |
+
|
| 37 |
+
btn.disabled = true;
|
| 38 |
+
btn.textContent = "Recording...";
|
| 39 |
+
logEl.textContent = "Listening for up to 20 seconds...";
|
| 40 |
+
|
| 41 |
+
recorder.ondataavailable = e => chunks.push(e.data);
|
| 42 |
+
recorder.onstop = async () => {
|
| 43 |
+
logEl.textContent = "Processing...";
|
| 44 |
+
const blob = new Blob(chunks, { type: 'audio/webm' });
|
| 45 |
+
const arrayBuffer = await blob.arrayBuffer();
|
| 46 |
+
|
| 47 |
+
try {
|
| 48 |
+
const res = await fetch("/transcribe", {
|
| 49 |
+
method: "POST",
|
| 50 |
+
headers: { "Content-Type": "audio/webm" },
|
| 51 |
+
body: arrayBuffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
});
|
| 53 |
+
const data = await res.json();
|
| 54 |
+
if (data.text) logEl.textContent = "📝 Transcription:\\n" + data.text;
|
| 55 |
+
else logEl.textContent = "Error: " + JSON.stringify(data);
|
| 56 |
+
} catch (err) {
|
| 57 |
+
logEl.textContent = "Network error: " + err.message;
|
| 58 |
+
}
|
| 59 |
+
btn.disabled = false;
|
| 60 |
+
btn.textContent = "Start Recording";
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
recorder.start();
|
| 64 |
+
setTimeout(() => recorder.stop(), 20000); // stop after 20s
|
| 65 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
btn.onclick = recordAndSend;
|
| 68 |
+
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
</body>
|
| 70 |
</html>
|
| 71 |
"""
|
| 72 |
|
| 73 |
+
@app.get("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def index():
|
| 75 |
+
return HTML_PAGE
|
| 76 |
|
| 77 |
+
@app.post("/transcribe")
|
| 78 |
+
def transcribe_audio():
|
| 79 |
+
try:
|
| 80 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as tmp:
|
| 81 |
+
tmp.write(request.data)
|
| 82 |
+
tmp_path = tmp.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
wav_path = tmp_path.replace(".webm", ".wav")
|
| 85 |
+
subprocess.run(
|
| 86 |
+
["ffmpeg", "-y", "-i", tmp_path, "-ar", "16000", "-ac", "1", wav_path],
|
| 87 |
+
stdout=subprocess.DEVNULL,
|
| 88 |
+
stderr=subprocess.DEVNULL
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
segments, info = model.transcribe(wav_path, beam_size=1)
|
| 92 |
+
text = " ".join([seg.text for seg in segments]).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
os.remove(tmp_path)
|
| 95 |
+
os.remove(wav_path)
|
| 96 |
+
|
| 97 |
+
return jsonify({"text": text, "language": info.language})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
|
|
|
|
|
|
| 99 |
return jsonify({"error": str(e)}), 500
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|