Spaces:
Runtime error
Runtime error
Download app.py from jorgeas6402/mariela-api: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/jorgeas6402/mariela-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/jorgeas6402/mariela-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jorgeas6402/mariela-api/resolve/main/app.py
1.39 kB
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| import requests | |
| app = FastAPI() | |
| # Permisos globales CORS para que tu frontend se conecte sin bloqueos | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class ChatRequest(BaseModel): | |
| message: str | |
| # Tu clave de API activa de Google Gemini | |
| API_KEY = "AIzaSyD8UXzsNh470H_6lzCyzJq8qnhowsitH_s" | |
| def read_root(): | |
| return {"status": "MARIELA PRIME CORE OPERATIVO"} | |
| async def procesar_chat(request: ChatRequest): | |
| if not request.message: | |
| raise HTTPException(status_code=400, detail="El mensaje está vacío") | |
| url = f"https://googleapis.com{API_KEY}" | |
| try: | |
| respuesta = requests.post( | |
| url, | |
| json={"contents": [{"parts": [{"text": request.message}]}]}, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| datos = respuesta.json() | |
| # Desempaquetado seguro del texto en el servidor | |
| texto_ia = datos['candidates'][0]['content']['parts'][0]['text'] | |
| return {"salida": texto_ia} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail="Error en el procesamiento del núcleo central") | |