DeepRat commited on
Commit
29384ce
·
verified ·
1 Parent(s): 8633425

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +65 -0
main.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # main.py
2
+ import os
3
+ from fastapi import FastAPI
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.responses import FileResponse
6
+ from fastapi.staticfiles import StaticFiles
7
+ from pydantic import BaseModel
8
+ import requests
9
+
10
+ # -------------------------------
11
+ # CARGA DEL FLOW_API_URL DESDE SECRETS
12
+ # -------------------------------
13
+ FLOW_API_URL = os.getenv("FLOW_API_URL")
14
+ if FLOW_API_URL is None:
15
+ raise RuntimeError("❌ FLOW_API_URL no está definido. Agregalo en los Secrets de Hugging Face.")
16
+
17
+ # -------------------------------
18
+ # INICIALIZACIÓN DE LA APP
19
+ # -------------------------------
20
+ app = FastAPI()
21
+
22
+ # Middleware CORS
23
+ app.add_middleware(
24
+ CORSMiddleware,
25
+ allow_origins=["*"], # Podés restringir esto si querés
26
+ allow_credentials=True,
27
+ allow_methods=["*"],
28
+ allow_headers=["*"],
29
+ )
30
+
31
+ # Montar carpeta estática
32
+ app.mount("/static", StaticFiles(directory="static"), name="static")
33
+
34
+ # Ruta principal: servir index.html
35
+ @app.get("/")
36
+ async def serve_index():
37
+ return FileResponse("static/index.html")
38
+
39
+ # -------------------------------
40
+ # MODELO DE ENTRADA
41
+ # -------------------------------
42
+ class AnalyzeRequest(BaseModel):
43
+ url: str
44
+
45
+ # -------------------------------
46
+ # ENDPOINT DE ANÁLISIS
47
+ # -------------------------------
48
+ @app.post("/analyze")
49
+ async def analyze(request: AnalyzeRequest):
50
+ try:
51
+ payload = {
52
+ "input_value": request.url,
53
+ "output_type": "chat",
54
+ "input_type": "chat"
55
+ }
56
+ headers = {"Content-Type": "application/json"}
57
+ response = requests.post(FLOW_API_URL, json=payload, headers=headers)
58
+ response.raise_for_status()
59
+ data = response.json()
60
+ result_text = data.get("result", "⚠️ No se encontró respuesta en el output.")
61
+ return {"result": result_text}
62
+ except requests.exceptions.RequestException as e:
63
+ return {"result": f"❌ Error al conectarse al Flow: {e}"}
64
+ except Exception as e:
65
+ return {"result": f"❌ Error inesperado: {e}"}