feat: upgrade interface web e suporte a áudio completo
This commit is contained in:
42
main.py
42
main.py
@@ -3,11 +3,12 @@ import psutil
|
||||
import subprocess
|
||||
import time
|
||||
import json
|
||||
from fastapi import FastAPI, Request, Header, Depends, HTTPException, status
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi import FastAPI, Request, Header, Depends, HTTPException, status, File, UploadFile
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from dotenv import load_dotenv
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
import audio_handler
|
||||
|
||||
from ai_agent import query_agent
|
||||
from config import get_config, save_config
|
||||
@@ -111,6 +112,43 @@ async def web_chat(message: dict, is_auth: bool = Depends(verify_password)):
|
||||
reply = await run_in_threadpool(query_agent, prompt=user_text)
|
||||
return JSONResponse(content={"reply": reply})
|
||||
|
||||
@app.post("/api/chat-audio")
|
||||
async def web_chat_audio(audio: UploadFile = File(...), is_auth: bool = Depends(verify_password)):
|
||||
"""Recebe áudio, transcreve, processa na IA e devolve texto + áudio de resposta."""
|
||||
temp_path = f"/tmp/{audio.filename}"
|
||||
with open(temp_path, "wb") as buffer:
|
||||
buffer.write(await audio.read())
|
||||
|
||||
try:
|
||||
# Transcrição (STT)
|
||||
user_text = await run_in_threadpool(audio_handler.transcribe_audio, temp_path)
|
||||
|
||||
# IA (Processamento)
|
||||
reply = await run_in_threadpool(query_agent, prompt=user_text)
|
||||
|
||||
# Síntese (TTS)
|
||||
audio_filename = await run_in_threadpool(audio_handler.text_to_speech, reply)
|
||||
audio_url = f"/api/audio-file/{audio_filename}"
|
||||
|
||||
return JSONResponse(content={
|
||||
"text": user_text,
|
||||
"reply": reply,
|
||||
"audio_url": audio_url
|
||||
})
|
||||
except Exception as e:
|
||||
return JSONResponse(content={"status": "error", "message": str(e)}, status_code=500)
|
||||
finally:
|
||||
if os.path.exists(temp_path):
|
||||
os.remove(temp_path)
|
||||
|
||||
@app.get("/api/audio-file/{filename}")
|
||||
async def get_audio_file(filename: str):
|
||||
"""Serve os arquivos de áudio temporários gerados pelo TTS."""
|
||||
filepath = os.path.join("/tmp", filename)
|
||||
if os.path.exists(filepath):
|
||||
return FileResponse(filepath, media_type="audio/mpeg")
|
||||
raise HTTPException(status_code=404, detail="Arquivo de áudio não encontrado.")
|
||||
|
||||
@app.get("/api/test_llm")
|
||||
async def test_llm_speed(is_auth: bool = Depends(verify_password)):
|
||||
"""Mede a velocidade de resposta da IA ativa."""
|
||||
|
||||
Reference in New Issue
Block a user