Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Control Total AI API")
|
| 8 |
+
|
| 9 |
+
API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 10 |
+
if API_KEY:
|
| 11 |
+
genai.configure(api_key=API_KEY)
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
return {"status": "SaaS API activa", "version": "1.1-beta"}
|
| 16 |
+
|
| 17 |
+
@app.get("/api/context")
|
| 18 |
+
def get_context(url: str):
|
| 19 |
+
if not url:
|
| 20 |
+
raise HTTPException(status_code=400, detail="Falta la URL")
|
| 21 |
+
try:
|
| 22 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
|
| 23 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 24 |
+
if response.status_code != 200:
|
| 25 |
+
raise HTTPException(status_code=400, detail="Error al acceder a la web")
|
| 26 |
+
|
| 27 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 28 |
+
for script in soup(["script", "style"]):
|
| 29 |
+
script.extract()
|
| 30 |
+
text = soup.get_text(separator=' ', strip=True)[:4000]
|
| 31 |
+
|
| 32 |
+
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 33 |
+
prompt = f"Analiza este texto y estructúralo en un JSON limpio para agentes de IA con claves: 'nombre_empresa', 'propuesta_valor', 'servicios_principales'. Texto:\n{text}"
|
| 34 |
+
ai_response = model.generate_content(prompt)
|
| 35 |
+
|
| 36 |
+
return {"url_analizada": url, "ia_raw_response": ai_response.text}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise HTTPException(status_code=500, detail=str(e))
|