from flask import Flask, request, jsonify, send_file, render_template_string import os, io, uuid, requests, json, logging, torch import numpy as np from transformers import pipeline from gtts import gTTS from scipy.io import wavfile # Setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) app = Flask(__name__) # Config OPENROUTER_API_KEY = "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" MODEL_ID = "meta-llama/llama-3.1-405b-instruct:free" # Storage latest_ai_text = "No interaction yet." # Load Whisper STT (Optimized for CPU) device = "cuda" if torch.cuda.is_available() else "cpu" stt_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=device) def ask_llama(text): global latest_ai_text if not text.strip(): return "..." try: headers = { "Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json" } payload = { "model": MODEL_ID, "messages": [ {"role": "system", "content": "You are Study Intelligence Robot. Max 10 words. Principal of KMGSS is Dinesh Thapa. Creator: Kalika Manavgyan Secondary School. Time: Dec 2025."}, {"role": "user", "content": text} ] } r = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, data=json.dumps(payload), timeout=15) ans = r.json()['choices'][0]['message']['content'].strip() latest_ai_text = ans return ans except Exception as e: logger.error(f"AI Error: {e}") return "Connection error." @app.route('/') def index(): return render_template_string("""
Kalika Manavgyan Secondary School