Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,169 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
import base64
|
| 5 |
-
import tempfile
|
| 6 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
-
from openai import OpenAI
|
| 10 |
-
from playwright.sync_api import sync_playwright
|
| 11 |
-
|
| 12 |
-
GROQ_KEY = os.environ.get("GROQ_KEY", "")
|
| 13 |
-
|
| 14 |
-
# Modelo de vision de Groq (si cambia el nombre, ajustalo aqui).
|
| 15 |
-
VISION_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct"
|
| 16 |
-
# Respaldo solo-texto si la vision falla.
|
| 17 |
-
TEXT_MODEL = "llama-3.3-70b-versatile"
|
| 18 |
-
|
| 19 |
-
_groq = OpenAI(api_key=GROQ_KEY, base_url="https://api.groq.com/openai/v1") if GROQ_KEY else None
|
| 20 |
-
|
| 21 |
-
# Playwright (API sync) debe correr en un hilo SIN loop asyncio. Usamos un
|
| 22 |
-
# executor propio para garantizarlo.
|
| 23 |
-
_executor = ThreadPoolExecutor(max_workers=1)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def _interactuar(pagina):
|
| 27 |
-
"""Best-effort: rellena inputs visibles y pulsa un boton de accion real,
|
| 28 |
-
para que el Tester vea la app DESPUES de usarla, no solo al cargar."""
|
| 29 |
-
try:
|
| 30 |
-
inputs = pagina.locator("input[type=text], input[type=number], input:not([type]), textarea").all()
|
| 31 |
-
for inp in inputs[:6]:
|
| 32 |
-
try:
|
| 33 |
-
if not inp.is_visible():
|
| 34 |
-
continue
|
| 35 |
-
tipo = inp.get_attribute("type") or "text"
|
| 36 |
-
inp.fill("5" if tipo == "number" else "prueba")
|
| 37 |
-
except Exception:
|
| 38 |
-
continue
|
| 39 |
-
except Exception:
|
| 40 |
-
pass
|
| 41 |
-
try:
|
| 42 |
-
for btn in pagina.locator("button").all():
|
| 43 |
-
try:
|
| 44 |
-
if not btn.is_visible():
|
| 45 |
-
continue
|
| 46 |
-
texto_btn = (btn.inner_text() or "").strip().lower()
|
| 47 |
-
if any(p in texto_btn for p in ["clear", "limpiar", "borrar", "flag"]):
|
| 48 |
-
continue
|
| 49 |
-
btn.click(timeout=3000)
|
| 50 |
-
pagina.wait_for_timeout(3000)
|
| 51 |
-
try:
|
| 52 |
-
pagina.wait_for_load_state("networkidle", timeout=8000)
|
| 53 |
-
except Exception:
|
| 54 |
-
pass
|
| 55 |
-
break
|
| 56 |
-
except Exception:
|
| 57 |
-
continue
|
| 58 |
-
except Exception:
|
| 59 |
-
pass
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def _abrir_y_capturar(url):
|
| 63 |
-
"""Abre la URL, interactua con la app (rellena+clic), y devuelve
|
| 64 |
-
(texto_visible, ruta_captura) DESPUES de esa interaccion."""
|
| 65 |
-
with sync_playwright() as p:
|
| 66 |
-
navegador = p.chromium.launch(args=["--no-sandbox", "--disable-dev-shm-usage"])
|
| 67 |
-
pagina = navegador.new_page(viewport={"width": 1280, "height": 1600})
|
| 68 |
-
pagina.goto(url, wait_until="domcontentloaded", timeout=45000)
|
| 69 |
-
try:
|
| 70 |
-
pagina.wait_for_selector(".gradio-container, gradio-app, #root", timeout=15000)
|
| 71 |
-
except Exception:
|
| 72 |
-
pass
|
| 73 |
-
pagina.wait_for_timeout(2500)
|
| 74 |
-
_interactuar(pagina)
|
| 75 |
-
pagina.wait_for_timeout(1000)
|
| 76 |
-
texto = pagina.inner_text("body")[:6000]
|
| 77 |
-
ruta = os.path.join(tempfile.gettempdir(), "captura.png")
|
| 78 |
-
pagina.screenshot(path=ruta, full_page=True)
|
| 79 |
-
navegador.close()
|
| 80 |
-
return texto, ruta
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
def _abrir_seguro(url):
|
| 84 |
-
return _executor.submit(_abrir_y_capturar, url).result()
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def _img_b64(ruta):
|
| 88 |
-
with open(ruta, "rb") as f:
|
| 89 |
-
return base64.b64encode(f.read()).decode("utf-8")
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
SYSTEM = (
|
| 93 |
-
"Eres un tester de control de calidad de webs y apps. Te dan el OBJETIVO de una "
|
| 94 |
-
"pagina y lo que se ve en ella (texto y, si hay, captura). Decide si cumple el "
|
| 95 |
-
"objetivo y si parece funcional. Marca ok=false si la pagina esta en blanco, "
|
| 96 |
-
"muestra errores, le faltan secciones del objetivo o se ve rota. "
|
| 97 |
-
'Responde SOLO JSON: {"ok": true/false, "problemas": ["..."], "resumen": "..."}'
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
def _juzgar(objetivo, texto, ruta_img):
|
| 102 |
-
if _groq is None:
|
| 103 |
-
return '{"ok": false, "problemas": ["Falta GROQ_KEY en los secrets"], "resumen": "Sin clave"}'
|
| 104 |
-
base = f"OBJETIVO:\n{objetivo}\n\nTEXTO VISIBLE EN LA PAGINA:\n{texto}"
|
| 105 |
-
# 1) Intento con vision (incluye la captura)
|
| 106 |
-
try:
|
| 107 |
-
b64 = _img_b64(ruta_img)
|
| 108 |
-
resp = _groq.chat.completions.create(
|
| 109 |
-
model=VISION_MODEL,
|
| 110 |
-
messages=[
|
| 111 |
-
{"role": "system", "content": SYSTEM},
|
| 112 |
-
{"role": "user", "content": [
|
| 113 |
-
{"type": "text", "text": base},
|
| 114 |
-
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}},
|
| 115 |
-
]},
|
| 116 |
-
],
|
| 117 |
-
temperature=0,
|
| 118 |
-
)
|
| 119 |
-
return resp.choices[0].message.content
|
| 120 |
-
except Exception:
|
| 121 |
-
pass
|
| 122 |
-
# 2) Respaldo solo-texto
|
| 123 |
-
try:
|
| 124 |
-
resp = _groq.chat.completions.create(
|
| 125 |
-
model=TEXT_MODEL,
|
| 126 |
-
messages=[
|
| 127 |
-
{"role": "system", "content": SYSTEM},
|
| 128 |
-
{"role": "user", "content": base},
|
| 129 |
-
],
|
| 130 |
-
temperature=0,
|
| 131 |
-
)
|
| 132 |
-
return resp.choices[0].message.content
|
| 133 |
-
except Exception as e:
|
| 134 |
-
return '{"ok": false, "problemas": ["Error del modelo: ' + str(e)[:120] + '"], "resumen": "Fallo al juzgar"}'
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
def probar(url, objetivo):
|
| 138 |
-
url = (url or "").strip()
|
| 139 |
-
if not url:
|
| 140 |
-
return "Pon una URL.", None
|
| 141 |
-
if not url.startswith("http"):
|
| 142 |
-
url = "https://" + url
|
| 143 |
-
try:
|
| 144 |
-
texto, ruta = _abrir_seguro(url)
|
| 145 |
-
except Exception as e:
|
| 146 |
-
return f"No se pudo abrir la pagina: {e}", None
|
| 147 |
-
veredicto = _juzgar(objetivo or "La pagina debe cargar sin errores y verse completa.", texto, ruta)
|
| 148 |
-
salida = veredicto
|
| 149 |
-
try:
|
| 150 |
-
limpio = veredicto.replace("```json", "").replace("```", "").strip()
|
| 151 |
-
d = json.loads(limpio)
|
| 152 |
-
estado = "PASA" if d.get("ok") else "NO PASA"
|
| 153 |
-
problemas = "\n".join(f"- {x}" for x in d.get("problemas", []))
|
| 154 |
-
salida = f"### {estado}\n\n{d.get('resumen','')}\n\n{problemas}".strip()
|
| 155 |
-
except Exception:
|
| 156 |
-
pass
|
| 157 |
-
return salida, ruta
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
with gr.Blocks(title="Forja Tester") as demo:
|
| 161 |
-
gr.Markdown("# Forja Tester\nAbre una app en un navegador real, la mira y dice si funciona.")
|
| 162 |
-
url_in = gr.Textbox(label="URL de la app a probar")
|
| 163 |
-
obj_in = gr.Textbox(label="Que deberia hacer / contener", lines=3)
|
| 164 |
-
btn = gr.Button("Probar", variant="primary")
|
| 165 |
-
out = gr.Markdown()
|
| 166 |
-
img = gr.Image(label="Lo que vio el navegador")
|
|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