Spaces:
Sleeping
Sleeping
ini commit
Browse files
app.py
CHANGED
|
@@ -1,195 +1,4 @@
|
|
| 1 |
|
| 2 |
-
# ==========================================================
|
| 3 |
-
# KC ROBOT AI - APP.PY (V2.0 MAX FINAL)
|
| 4 |
-
# Cloud AI Robot with Gemini 2.5 Flash + ESP32 + Telegram
|
| 5 |
-
# ==========================================================
|
| 6 |
-
|
| 7 |
-
from flask import Flask, request, jsonify, render_template_string
|
| 8 |
-
from google import genai
|
| 9 |
-
import requests
|
| 10 |
-
import os
|
| 11 |
-
import time
|
| 12 |
-
from gtts import gTTS
|
| 13 |
-
from langdetect import detect
|
| 14 |
-
import tempfile
|
| 15 |
-
import base64
|
| 16 |
-
|
| 17 |
-
# ==========================================================
|
| 18 |
-
# CONFIGURATION
|
| 19 |
-
# ==========================================================
|
| 20 |
-
|
| 21 |
-
# Load environment variables from secrets (Cloud Run or Hugging Face)
|
| 22 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 23 |
-
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-flash")
|
| 24 |
-
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 25 |
-
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
| 26 |
-
|
| 27 |
-
# Create Flask app
|
| 28 |
-
app = Flask(__name__)
|
| 29 |
-
|
| 30 |
-
# ==========================================================
|
| 31 |
-
# SETUP GEMINI CLIENT
|
| 32 |
-
# ==========================================================
|
| 33 |
-
if not GEMINI_API_KEY:
|
| 34 |
-
print("❌ ERROR: No Gemini API Key found. Please add GEMINI_API_KEY in Secrets.")
|
| 35 |
-
client = None
|
| 36 |
-
else:
|
| 37 |
-
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 38 |
-
|
| 39 |
-
# ==========================================================
|
| 40 |
-
# TELEGRAM UTILITIES
|
| 41 |
-
# ==========================================================
|
| 42 |
-
def send_telegram_message(text):
|
| 43 |
-
if not TELEGRAM_TOKEN or not TELEGRAM_CHAT_ID:
|
| 44 |
-
print("⚠️ Telegram not configured.")
|
| 45 |
-
return
|
| 46 |
-
url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
|
| 47 |
-
payload = {"chat_id": TELEGRAM_CHAT_ID, "text": text}
|
| 48 |
-
try:
|
| 49 |
-
requests.post(url, json=payload, timeout=5)
|
| 50 |
-
except Exception as e:
|
| 51 |
-
print("Telegram Error:", e)
|
| 52 |
-
|
| 53 |
-
# ==========================================================
|
| 54 |
-
# GEMINI AI RESPONSE
|
| 55 |
-
# ==========================================================
|
| 56 |
-
def ask_gemini(prompt: str):
|
| 57 |
-
if not client:
|
| 58 |
-
return "⚠️ Gemini API key missing. Please configure in Secrets."
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
response = client.models.generate_content(
|
| 62 |
-
model=GEMINI_MODEL,
|
| 63 |
-
contents=prompt
|
| 64 |
-
)
|
| 65 |
-
if hasattr(response, "text"):
|
| 66 |
-
return response.text.strip()
|
| 67 |
-
elif "text" in response:
|
| 68 |
-
return response["text"].strip()
|
| 69 |
-
else:
|
| 70 |
-
return "⚠️ No response text from Gemini."
|
| 71 |
-
except Exception as e:
|
| 72 |
-
print("Gemini Error:", e)
|
| 73 |
-
return f"⚠️ Gemini Error: {e}"
|
| 74 |
-
|
| 75 |
-
# ==========================================================
|
| 76 |
-
# LANGUAGE DETECTION & TTS
|
| 77 |
-
# ==========================================================
|
| 78 |
-
def text_to_speech(text):
|
| 79 |
-
try:
|
| 80 |
-
lang = detect(text)
|
| 81 |
-
if lang not in ["vi", "en"]:
|
| 82 |
-
lang = "en"
|
| 83 |
-
tts = gTTS(text=text, lang=lang)
|
| 84 |
-
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 85 |
-
tts.save(tmp.name)
|
| 86 |
-
with open(tmp.name, "rb") as f:
|
| 87 |
-
audio_b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 88 |
-
os.unlink(tmp.name)
|
| 89 |
-
return audio_b64
|
| 90 |
-
except Exception as e:
|
| 91 |
-
print("TTS Error:", e)
|
| 92 |
-
return None
|
| 93 |
-
|
| 94 |
-
# ==========================================================
|
| 95 |
-
# SIMPLE HTML INTERFACE (for testing)
|
| 96 |
-
# ==========================================================
|
| 97 |
-
HTML_PAGE = """
|
| 98 |
-
<!DOCTYPE html>
|
| 99 |
-
<html>
|
| 100 |
-
<head>
|
| 101 |
-
<title>KC Robot AI v2.0</title>
|
| 102 |
-
<style>
|
| 103 |
-
body { font-family: Arial; text-align: center; background-color: #101010; color: white; }
|
| 104 |
-
input, button { padding: 10px; font-size: 16px; margin: 5px; }
|
| 105 |
-
#chat { max-width: 700px; margin: auto; text-align: left; background: #202020; padding: 20px; border-radius: 10px; }
|
| 106 |
-
.msg-user { color: #4af; }
|
| 107 |
-
.msg-bot { color: #fa4; margin-left: 20px; }
|
| 108 |
-
audio { margin-top: 10px; }
|
| 109 |
-
</style>
|
| 110 |
-
</head>
|
| 111 |
-
<body>
|
| 112 |
-
<h1>🤖 KC Robot AI v2.0 MAX FINAL</h1>
|
| 113 |
-
<div id="chat"></div>
|
| 114 |
-
<br>
|
| 115 |
-
<input id="user_input" placeholder="Nói gì đó..." style="width:60%">
|
| 116 |
-
<button onclick="sendMessage()">Gửi</button>
|
| 117 |
-
|
| 118 |
-
<script>
|
| 119 |
-
async function sendMessage() {
|
| 120 |
-
const input = document.getElementById("user_input").value;
|
| 121 |
-
if (!input) return;
|
| 122 |
-
const chat = document.getElementById("chat");
|
| 123 |
-
chat.innerHTML += `<div class='msg-user'><b>Bạn:</b> ${input}</div>`;
|
| 124 |
-
document.getElementById("user_input").value = "";
|
| 125 |
-
const res = await fetch("/api/chat", {
|
| 126 |
-
method: "POST",
|
| 127 |
-
headers: {"Content-Type": "application/json"},
|
| 128 |
-
body: JSON.stringify({message: input})
|
| 129 |
-
});
|
| 130 |
-
const data = await res.json();
|
| 131 |
-
chat.innerHTML += `<div class='msg-bot'><b>Robot:</b> ${data.reply}</div>`;
|
| 132 |
-
if (data.audio) {
|
| 133 |
-
const audio = document.createElement("audio");
|
| 134 |
-
audio.src = "data:audio/mp3;base64," + data.audio;
|
| 135 |
-
audio.controls = true;
|
| 136 |
-
chat.appendChild(audio);
|
| 137 |
-
}
|
| 138 |
-
chat.scrollTop = chat.scrollHeight;
|
| 139 |
-
}
|
| 140 |
-
</script>
|
| 141 |
-
</body>
|
| 142 |
-
</html>
|
| 143 |
-
"""
|
| 144 |
-
|
| 145 |
-
@app.route("/")
|
| 146 |
-
def home():
|
| 147 |
-
return render_template_string(HTML_PAGE)
|
| 148 |
-
|
| 149 |
-
# ==========================================================
|
| 150 |
-
# API ENDPOINTS
|
| 151 |
-
# ==========================================================
|
| 152 |
-
|
| 153 |
-
@app.route("/api/chat", methods=["POST"])
|
| 154 |
-
def api_chat():
|
| 155 |
-
data = request.get_json()
|
| 156 |
-
if not data or "message" not in data:
|
| 157 |
-
return jsonify({"error": "Missing 'message'"}), 400
|
| 158 |
-
|
| 159 |
-
user_message = data["message"]
|
| 160 |
-
print(f"🧠 User said: {user_message}")
|
| 161 |
-
send_telegram_message(f"User: {user_message}")
|
| 162 |
-
|
| 163 |
-
ai_reply = ask_gemini(user_message)
|
| 164 |
-
send_telegram_message(f"Robot: {ai_reply}")
|
| 165 |
-
|
| 166 |
-
audio_b64 = text_to_speech(ai_reply)
|
| 167 |
-
return jsonify({"reply": ai_reply, "audio": audio_b64})
|
| 168 |
-
|
| 169 |
-
# ESP32 sensor endpoint
|
| 170 |
-
@app.route("/api/sensor", methods=["POST"])
|
| 171 |
-
def sensor_data():
|
| 172 |
-
data = request.get_json()
|
| 173 |
-
if not data:
|
| 174 |
-
return jsonify({"error": "No data"}), 400
|
| 175 |
-
msg = f"👁️ ESP32 Sensor update: {data}"
|
| 176 |
-
send_telegram_message(msg)
|
| 177 |
-
return jsonify({"status": "received"})
|
| 178 |
-
|
| 179 |
-
# Health check
|
| 180 |
-
@app.route("/ping")
|
| 181 |
-
def ping():
|
| 182 |
-
return jsonify({"status": "ok", "model": GEMINI_MODEL})
|
| 183 |
-
|
| 184 |
-
# ==========================================================
|
| 185 |
-
# MAIN ENTRY POINT
|
| 186 |
-
# ==========================================================
|
| 187 |
-
if __name__ == "__main__":
|
| 188 |
-
port = int(os.getenv("PORT", 8080))
|
| 189 |
-
print(f"🚀 KC Robot AI v2.0 running on port {port}")
|
| 190 |
-
app.run(host="0.0.0.0", port=port)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
|
| 194 |
# app.py — KC Robot AI v7.5 FINAL (auto-model-select, bilingual, TTS fallback, Telegram, ESP32 endpoints)
|
| 195 |
# Secrets expected (HF Space -> Settings -> Secrets):
|
|
@@ -232,8 +41,8 @@ logger = logging.getLogger("kcrobot.v7.5")
|
|
| 232 |
# ---------------- env / secrets ----------------
|
| 233 |
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 234 |
HF_MODEL = os.getenv("HF_MODEL", "").strip() # preferred model (may be empty)
|
| 235 |
-
HF_TTS_MODEL = os.getenv("HF_TTS_MODEL", "").strip() # optional HF TTS model
|
| 236 |
-
HF_STT_MODEL = os.getenv("HF_STT_MODEL", "openai/whisper-small").strip()
|
| 237 |
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN", "").strip()
|
| 238 |
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", "").strip()
|
| 239 |
PORT = int(os.getenv("PORT", 7860))
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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rets ----------------
|
| 42 |
HF_TOKEN = os.getenv("HF_TOKEN", "").strip()
|
| 43 |
HF_MODEL = os.getenv("HF_MODEL", "").strip() # preferred model (may be empty)
|
| 44 |
+
#HF_TTS_MODEL = os.getenv("HF_TTS_MODEL", "").strip() # optional HF TTS model
|
| 45 |
+
#HF_STT_MODEL = os.getenv("HF_STT_MODEL", "openai/whisper-small").strip()
|
| 46 |
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN", "").strip()
|
| 47 |
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", "").strip()
|
| 48 |
PORT = int(os.getenv("PORT", 7860))
|