Download main.py from akra35567/brain: direct link, hf CLI and curl.
- Browser
- Download file 2.63 kB
-
https://huggingface.co/spaces/akra35567/brain/resolve/main/main.py
- Command line
-
hf download hf://spaces/akra35567/brain/main.py
-
curl -L -o main.py https://huggingface.co/spaces/akra35567/brain/resolve/main/main.py
2.63 kB
| import os | |
| from fastapi import Depends, FastAPI, Header, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import spaces | |
| from ai_engine import AIEngine | |
| from config import Config | |
| from models import EscutarPayload, EscutarResponse, FeedbackPayload, FeedbackResponse | |
| app = FastAPI( | |
| title="Plenitude — Cerebro Negra Plena", | |
| description="API de atendimento da pastelaria Negra Plena com suporte a GPU.", | |
| version="1.0.1", | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| engine = AIEngine() | |
| def require_token(authorization: str = Header(default="")) -> None: | |
| if not Config.API_AUTH_TOKEN: | |
| return | |
| expected = f"Bearer {Config.API_AUTH_TOKEN}" | |
| if authorization != expected: | |
| raise HTTPException(status_code=401, detail="Token invalido") | |
| # A Hugging Face exige que haja uma funcao com @spaces.GPU se o Space tiver GPU! | |
| def run_ml_task(texto: str): | |
| return f"GPU Processed: {texto}" | |
| def root(): | |
| return { | |
| "assistente": Config.ASSISTANT_NAME, | |
| "empresa": Config.COMPANY_NAME, | |
| "status": "ok", | |
| "provedor": Config.get_active_provider(), | |
| "gpu_enabled": True | |
| } | |
| def health(): | |
| return {"status": "ok"} | |
| def escutar(payload: EscutarPayload, _: None = Depends(require_token)): | |
| cliente = payload.numero_crm or payload.numero or "unknown" | |
| texto = payload.texto.strip() | |
| if not texto and not payload.audio_base64: | |
| return EscutarResponse( | |
| texto="Nao consegui ler a sua mensagem. Pode escrever de novo, por favor?" | |
| ) | |
| if not texto and payload.audio_base64: | |
| texto = "(o cliente enviou um audio; transcricao na GPU ainda em preparacao)" | |
| resposta, mensagem_id = engine.responder( | |
| cliente, texto, payload.nome or "Cliente", | |
| fora_expediente=payload.fora_expediente, | |
| ) | |
| return EscutarResponse(texto=resposta, mensagem_id=mensagem_id) | |
| def feedback(payload: FeedbackPayload, _: None = Depends(require_token)): | |
| ok = engine.learning.rate( | |
| payload.mensagem_id, payload.nota, payload.comentario or "" | |
| ) | |
| if ok: | |
| return FeedbackResponse(ok=True, detalhe="Avaliação registada. Obrigado!") | |
| return FeedbackResponse(ok=False, detalhe="mensagem_id não encontrado.") | |