Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,38 +2,75 @@ from dotenv import load_dotenv
|
|
| 2 |
load_dotenv()
|
| 3 |
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from core import CoreDaimon
|
| 6 |
from db import DBModule
|
| 7 |
from ui import crear_interfaz
|
| 8 |
from router import brain_router
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
core = CoreDaimon()
|
| 11 |
db = DBModule()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
if not prompt:
|
| 20 |
return None
|
|
|
|
|
|
|
|
|
|
| 21 |
return None
|
| 22 |
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
if
|
| 26 |
-
return "❌
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
if not
|
| 32 |
-
return "
|
| 33 |
-
return
|
| 34 |
|
| 35 |
|
| 36 |
-
demo = crear_interfaz(chat,
|
|
|
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
demo.launch()
|
|
|
|
| 2 |
load_dotenv()
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import asyncio
|
| 7 |
+
import speech_recognition as sr
|
| 8 |
+
import edge_tts
|
| 9 |
+
|
| 10 |
from core import CoreDaimon
|
| 11 |
from db import DBModule
|
| 12 |
from ui import crear_interfaz
|
| 13 |
from router import brain_router
|
| 14 |
+
from auth import verificar_limite, login_usuario, registro_usuario, do_logout
|
| 15 |
+
from files import procesar_archivo
|
| 16 |
+
from image_gen import ImageGen
|
| 17 |
|
| 18 |
core = CoreDaimon()
|
| 19 |
db = DBModule()
|
| 20 |
+
image_gen = ImageGen()
|
| 21 |
+
recognizer = sr.Recognizer()
|
| 22 |
+
uso_diario = {}
|
| 23 |
+
LIMITE_DIARIO = 200
|
| 24 |
+
|
| 25 |
|
| 26 |
+
async def texto_a_voz_async(texto, archivo="respuesta.mp3"):
|
| 27 |
+
try:
|
| 28 |
+
if len(texto) > 2000: texto = texto[:2000]
|
| 29 |
+
communicate = edge_tts.Communicate(texto, "es-MX-DaliaNeural")
|
| 30 |
+
await communicate.save(archivo)
|
| 31 |
+
return archivo
|
| 32 |
+
except:
|
| 33 |
+
return None
|
| 34 |
|
| 35 |
+
|
| 36 |
+
def texto_a_voz(texto, archivo="respuesta.mp3"):
|
| 37 |
+
try:
|
| 38 |
+
return asyncio.run(texto_a_voz_async(texto, archivo))
|
| 39 |
+
except:
|
| 40 |
+
return None
|
| 41 |
|
| 42 |
|
| 43 |
+
def procesar_imagen(prompt):
|
| 44 |
+
if not prompt or not prompt.strip():
|
| 45 |
return None
|
| 46 |
+
ruta = image_gen.generar_con_fallback(prompt)
|
| 47 |
+
if ruta and ruta.exists():
|
| 48 |
+
return str(ruta)
|
| 49 |
return None
|
| 50 |
|
| 51 |
|
| 52 |
+
def procesar_voz(audio_path):
|
| 53 |
+
if audio_path is None:
|
| 54 |
+
return "❌ No se detecto audio.", None
|
| 55 |
+
try:
|
| 56 |
+
with sr.AudioFile(audio_path) as source:
|
| 57 |
+
audio = recognizer.record(source)
|
| 58 |
+
texto = recognizer.recognize_google(audio, language="es-ES")
|
| 59 |
+
respuesta = core.chat(texto)
|
| 60 |
+
audio_respuesta = texto_a_voz(respuesta, "respuesta_voz.mp3")
|
| 61 |
+
return f"🎙️ **Usted:** {texto}\n\n💬 **Daimon:** {respuesta}", audio_respuesta
|
| 62 |
+
except:
|
| 63 |
+
return "❌ No se entendio el audio.", None
|
| 64 |
|
| 65 |
|
| 66 |
+
def chat(mensaje, history, user_id=None, is_admin=0, ip=""):
|
| 67 |
+
if not is_admin and not verificar_limite(ip, uso_diario, LIMITE_DIARIO, lambda: None):
|
| 68 |
+
return "⚠️ Límite diario alcanzado."
|
| 69 |
+
return core.chat(mensaje)
|
| 70 |
|
| 71 |
|
| 72 |
+
demo = crear_interfaz(chat, texto_a_voz, procesar_imagen, procesar_archivo, procesar_voz,
|
| 73 |
+
login_usuario, registro_usuario, do_logout, db)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
demo.launch()
|