Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
.env.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY=tu_api_key_de_openai_aqui
|
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🚀
|
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
CHANGED
|
@@ -1,6 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def simulate_speed_test():
|
| 6 |
"""Simula un test de velocidad realista"""
|
|
@@ -11,80 +17,127 @@ def simulate_speed_test():
|
|
| 11 |
|
| 12 |
return round(download, 2), round(upload, 2), round(ping, 2), jitter
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
"""Analiza los resultados con
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if download > 80:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
elif download > 50:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
elif download > 25:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
elif download > 10:
|
| 27 |
-
quality = "REGULAR"
|
| 28 |
-
analysis = "Suficiente para uso básico, considera optimizar"
|
| 29 |
else:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Recomendaciones basadas en métricas
|
| 34 |
-
recommendations = []
|
| 35 |
|
|
|
|
| 36 |
if download < 25:
|
| 37 |
-
|
| 38 |
if ping > 60:
|
| 39 |
-
|
| 40 |
if upload < 10:
|
| 41 |
-
|
| 42 |
-
if jitter > 5:
|
| 43 |
-
recommendations.append("📶 Jitter elevado puede afectar llamadas VoIP")
|
| 44 |
|
| 45 |
-
if not
|
| 46 |
-
|
| 47 |
|
| 48 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def run_complete_test():
|
| 51 |
-
"""Ejecuta el test completo"""
|
| 52 |
download, upload, ping, jitter = simulate_speed_test()
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
return download, upload, ping, jitter,
|
| 56 |
|
| 57 |
def run_test_with_ui():
|
| 58 |
"""Interfaz con progreso para Gradio"""
|
| 59 |
|
| 60 |
-
#
|
| 61 |
yield "🔄 **Iniciando test de velocidad...**", "", "", "", 0, 0, 0, 0
|
| 62 |
time.sleep(1)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
yield "📡 **Buscando servidor óptimo...**", "", "", "", 0, 0, 0, 0
|
| 66 |
time.sleep(1)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
yield "⏱️ **Calculando latencia...**", "", "", "", 0, 0, 0, 0
|
| 70 |
time.sleep(1)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
yield "🔽 **Midiendo velocidad de descarga...**", "", "", "", 0, 0, 0, 0
|
| 74 |
time.sleep(2)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
yield "🔼 **Midiendo velocidad de subida...**", "", "", "", 0, 0, 0, 0
|
| 78 |
time.sleep(1)
|
| 79 |
|
| 80 |
-
# Fase 6: IA
|
| 81 |
yield "🤖 **Analizando con IA...**", "", "", "", 0, 0, 0, 0
|
| 82 |
-
time.sleep(
|
| 83 |
|
| 84 |
# Ejecutar test final
|
| 85 |
-
download, upload, ping, jitter,
|
| 86 |
|
| 87 |
-
# Formatear resultados
|
| 88 |
metrics_html = f"""
|
| 89 |
<div style="text-align: center; background: white; padding: 25px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
|
| 90 |
<h2 style="color: #333; margin-bottom: 25px;">📊 Resultados del Test</h2>
|
|
@@ -107,7 +160,7 @@ def run_test_with_ui():
|
|
| 107 |
</div>
|
| 108 |
</div>
|
| 109 |
<div style="background: #e9ecef; padding: 15px; border-radius: 8px; margin-top: 20px;">
|
| 110 |
-
<p style="margin: 0; color: #333; font-weight: bold;">Calificación: {
|
| 111 |
</div>
|
| 112 |
</div>
|
| 113 |
"""
|
|
@@ -115,26 +168,25 @@ def run_test_with_ui():
|
|
| 115 |
ai_html = f"""
|
| 116 |
<div style="background: white; padding: 20px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1); border-left: 4px solid #6f42c1;">
|
| 117 |
<h3 style="color: #333; margin-top: 0;">🧠 Análisis de Inteligencia Artificial</h3>
|
| 118 |
-
<p style="color: #333; line-height: 1.6; margin: 0;">{
|
| 119 |
</div>
|
| 120 |
"""
|
| 121 |
|
| 122 |
rec_html = f"""
|
| 123 |
<div style="background: white; padding: 20px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1); border-left: 4px solid #20c997;">
|
| 124 |
<h3 style="color: #333; margin-top: 0;">💡 Recomendaciones Inteligentes</h3>
|
| 125 |
-
{"".join([f'<p style="margin: 10px 0; padding: 12px; background: #f8f9fa; border-radius: 6px; color: #333;">{rec}</p>' for rec in
|
| 126 |
</div>
|
| 127 |
"""
|
| 128 |
|
| 129 |
-
final_status = f"✅ **Test completado** - {
|
| 130 |
|
| 131 |
-
# Devolver los 8 valores que espera Gradio
|
| 132 |
yield final_status, metrics_html, ai_html, rec_html, download, upload, ping, jitter
|
| 133 |
|
| 134 |
-
# Interfaz Gradio
|
| 135 |
with gr.Blocks(
|
| 136 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 137 |
-
title="🚀 Test de Velocidad con IA - DoctorLinux",
|
| 138 |
css="""
|
| 139 |
.gradio-container {
|
| 140 |
background: #ffffff !important;
|
|
@@ -149,22 +201,22 @@ with gr.Blocks(
|
|
| 149 |
gr.Markdown(
|
| 150 |
"""
|
| 151 |
<div style="text-align: center; padding: 30px; background: white; border-bottom: 3px solid #007bff; margin-bottom: 20px;">
|
| 152 |
-
<h1 style="margin: 0; font-size: 2.2em; color: #333;">🚀 Test de Velocidad con IA</h1>
|
| 153 |
-
<h3 style="margin: 10px 0; color: #333;">DoctorLinux.com - Análisis inteligente
|
| 154 |
-
<p style="color: #666; margin: 5px 0;">Potenciado por Hugging Face 🤗</p>
|
| 155 |
</div>
|
| 156 |
"""
|
| 157 |
)
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
start_btn = gr.Button(
|
| 161 |
-
"🧠 Iniciar Test con IA",
|
| 162 |
variant="primary",
|
| 163 |
size="lg"
|
| 164 |
)
|
| 165 |
|
| 166 |
status = gr.Markdown(
|
| 167 |
-
"**Presiona el botón para comenzar el análisis con
|
| 168 |
)
|
| 169 |
|
| 170 |
metrics = gr.HTML()
|
|
@@ -180,8 +232,9 @@ with gr.Blocks(
|
|
| 180 |
gr.Markdown(
|
| 181 |
"""
|
| 182 |
<div style="text-align: center; margin-top: 30px; padding: 20px; background: #f8f9fa; border-radius: 10px;">
|
| 183 |
-
<p style="color: #333; margin: 5px 0; font-size: 14px;">🔧 Tecnología: Python + Gradio +
|
| 184 |
<p style="color: #333; margin: 5px 0; font-size: 14px;">🌐 Desarrollado para <strong style="color: #007bff;">DoctorLinux.com</strong></p>
|
|
|
|
| 185 |
</div>
|
| 186 |
"""
|
| 187 |
)
|
|
@@ -190,14 +243,8 @@ with gr.Blocks(
|
|
| 190 |
start_btn.click(
|
| 191 |
fn=run_test_with_ui,
|
| 192 |
outputs=[
|
| 193 |
-
status,
|
| 194 |
-
|
| 195 |
-
ai_analysis, # 3: HTML
|
| 196 |
-
recommendations, # 4: HTML
|
| 197 |
-
download_metric, # 5: Number
|
| 198 |
-
upload_metric, # 6: Number
|
| 199 |
-
ping_metric, # 7: Number
|
| 200 |
-
jitter_metric # 8: Number
|
| 201 |
]
|
| 202 |
)
|
| 203 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import random
|
| 4 |
+
import openai
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Configuración de OpenAI (usa variable de entorno)
|
| 9 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
|
| 11 |
def simulate_speed_test():
|
| 12 |
"""Simula un test de velocidad realista"""
|
|
|
|
| 17 |
|
| 18 |
return round(download, 2), round(upload, 2), round(ping, 2), jitter
|
| 19 |
|
| 20 |
+
def analyze_with_openai(download, upload, ping, jitter):
|
| 21 |
+
"""Analiza los resultados con OpenAI REAL"""
|
| 22 |
+
try:
|
| 23 |
+
prompt = f"""
|
| 24 |
+
Como experto en telecomunicaciones, analiza ESTAS MÉTRICAS REALES de test de velocidad:
|
| 25 |
+
|
| 26 |
+
📊 MÉTRICAS:
|
| 27 |
+
- Velocidad de DESCARGA: {download} Mbps
|
| 28 |
+
- Velocidad de SUBIDA: {upload} Mbps
|
| 29 |
+
- Latencia (PING): {ping} ms
|
| 30 |
+
- Jitter: {jitter} ms
|
| 31 |
+
|
| 32 |
+
Proporciona un análisis TÉCNICO en español con:
|
| 33 |
+
1. CALIFICACIÓN (Excelente/Muy Buena/Buena/Regular/Limitada)
|
| 34 |
+
2. ANÁLISIS breve (1-2 líneas)
|
| 35 |
+
3. 2-3 RECOMENDACIONES específicas con emojis
|
| 36 |
+
|
| 37 |
+
Formato exacto:
|
| 38 |
+
CALIFICACIÓN: [tu calificación aquí]
|
| 39 |
+
ANÁLISIS: [tu análisis aquí]
|
| 40 |
+
RECOMENDACIONES: [1. 🎯 recom1] [2. 📡 recom2] [3. ⚡ recom3]
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
model="gpt-3.5-turbo",
|
| 45 |
+
messages=[
|
| 46 |
+
{"role": "system", "content": "Eres un experto técnico en telecomunicaciones. Responde solo con el formato solicitado."},
|
| 47 |
+
{"role": "user", "content": prompt}
|
| 48 |
+
],
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
max_tokens=200
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return response.choices[0].message.content
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
# Fallback a análisis automático si OpenAI falla
|
| 57 |
+
return analyze_with_fallback(download, upload, ping, jitter)
|
| 58 |
+
|
| 59 |
+
def analyze_with_fallback(download, upload, ping, jitter):
|
| 60 |
+
"""Análisis automático si OpenAI no funciona"""
|
| 61 |
if download > 80:
|
| 62 |
+
calidad = "Excelente"
|
| 63 |
+
analisis = "Conexión ideal para streaming 4K y gaming"
|
| 64 |
elif download > 50:
|
| 65 |
+
calidad = "Muy Buena"
|
| 66 |
+
analisis = "Excelente para teletrabajo y videollamadas"
|
| 67 |
elif download > 25:
|
| 68 |
+
calidad = "Buena"
|
| 69 |
+
analisis = "Adecuada para uso general y redes sociales"
|
|
|
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
+
calidad = "Regular"
|
| 72 |
+
analisis = "Puede tener limitaciones con contenido multimedia"
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
recomendaciones = []
|
| 75 |
if download < 25:
|
| 76 |
+
recomendaciones.append("🎯 Considera mejorar tu plan de internet")
|
| 77 |
if ping > 60:
|
| 78 |
+
recomendaciones.append("📡 Optimiza tu conexión WiFi")
|
| 79 |
if upload < 10:
|
| 80 |
+
recomendaciones.append("⚡ Subida limitada para streaming")
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
if not recomendaciones:
|
| 83 |
+
recomendaciones.append("✅ Tu conexión está bien optimizada")
|
| 84 |
|
| 85 |
+
return f"CALIFICACIÓN: {calidad}\nANÁLISIS: {analisis}\nRECOMENDACIONES: {' '.join(recomendaciones)}"
|
| 86 |
+
|
| 87 |
+
def parse_ai_response(ai_text):
|
| 88 |
+
"""Convierte la respuesta de IA en componentes separados"""
|
| 89 |
+
lines = ai_text.split('\n')
|
| 90 |
+
calidad = "No disponible"
|
| 91 |
+
analisis = "No disponible"
|
| 92 |
+
recomendaciones = []
|
| 93 |
+
|
| 94 |
+
for line in lines:
|
| 95 |
+
if line.startswith('CALIFICACIÓN:'):
|
| 96 |
+
calidad = line.replace('CALIFICACIÓN:', '').strip()
|
| 97 |
+
elif line.startswith('ANÁLISIS:'):
|
| 98 |
+
analisis = line.replace('ANÁLISIS:', '').strip()
|
| 99 |
+
elif line.startswith('RECOMENDACIONES:'):
|
| 100 |
+
recs_text = line.replace('RECOMENDACIONES:', '').strip()
|
| 101 |
+
# Separar recomendaciones individuales
|
| 102 |
+
recomendaciones = [r.strip() for r in recs_text.split(']') if r.strip()]
|
| 103 |
+
recomendaciones = [r + ']' for r in recomendaciones if r]
|
| 104 |
+
|
| 105 |
+
return calidad, analisis, recomendaciones
|
| 106 |
|
| 107 |
def run_complete_test():
|
| 108 |
+
"""Ejecuta el test completo con IA real"""
|
| 109 |
download, upload, ping, jitter = simulate_speed_test()
|
| 110 |
+
ai_response = analyze_with_openai(download, upload, ping, jitter)
|
| 111 |
+
calidad, analisis, recomendaciones = parse_ai_response(ai_response)
|
| 112 |
|
| 113 |
+
return download, upload, ping, jitter, calidad, analisis, recomendaciones
|
| 114 |
|
| 115 |
def run_test_with_ui():
|
| 116 |
"""Interfaz con progreso para Gradio"""
|
| 117 |
|
| 118 |
+
# Simular progreso del test
|
| 119 |
yield "🔄 **Iniciando test de velocidad...**", "", "", "", 0, 0, 0, 0
|
| 120 |
time.sleep(1)
|
| 121 |
|
| 122 |
+
yield "📡 **Conectando con servidores...**", "", "", "", 0, 0, 0, 0
|
|
|
|
| 123 |
time.sleep(1)
|
| 124 |
|
| 125 |
+
yield "⏱️ **Midiendo latencia...**", "", "", "", 0, 0, 0, 0
|
|
|
|
| 126 |
time.sleep(1)
|
| 127 |
|
| 128 |
+
yield "🔽 **Probando velocidad de descarga...**", "", "", "", 0, 0, 0, 0
|
|
|
|
| 129 |
time.sleep(2)
|
| 130 |
|
| 131 |
+
yield "🔼 **Probando velocidad de subida...**", "", "", "", 0, 0, 0, 0
|
|
|
|
| 132 |
time.sleep(1)
|
| 133 |
|
|
|
|
| 134 |
yield "🤖 **Analizando con IA...**", "", "", "", 0, 0, 0, 0
|
| 135 |
+
time.sleep(2)
|
| 136 |
|
| 137 |
# Ejecutar test final
|
| 138 |
+
download, upload, ping, jitter, calidad, analisis, recomendaciones = run_complete_test()
|
| 139 |
|
| 140 |
+
# Formatear resultados
|
| 141 |
metrics_html = f"""
|
| 142 |
<div style="text-align: center; background: white; padding: 25px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
|
| 143 |
<h2 style="color: #333; margin-bottom: 25px;">📊 Resultados del Test</h2>
|
|
|
|
| 160 |
</div>
|
| 161 |
</div>
|
| 162 |
<div style="background: #e9ecef; padding: 15px; border-radius: 8px; margin-top: 20px;">
|
| 163 |
+
<p style="margin: 0; color: #333; font-weight: bold;">Calificación IA: {calidad}</p>
|
| 164 |
</div>
|
| 165 |
</div>
|
| 166 |
"""
|
|
|
|
| 168 |
ai_html = f"""
|
| 169 |
<div style="background: white; padding: 20px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1); border-left: 4px solid #6f42c1;">
|
| 170 |
<h3 style="color: #333; margin-top: 0;">🧠 Análisis de Inteligencia Artificial</h3>
|
| 171 |
+
<p style="color: #333; line-height: 1.6; margin: 0;">{analisis}</p>
|
| 172 |
</div>
|
| 173 |
"""
|
| 174 |
|
| 175 |
rec_html = f"""
|
| 176 |
<div style="background: white; padding: 20px; border-radius: 10px; margin: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1); border-left: 4px solid #20c997;">
|
| 177 |
<h3 style="color: #333; margin-top: 0;">💡 Recomendaciones Inteligentes</h3>
|
| 178 |
+
{"".join([f'<p style="margin: 10px 0; padding: 12px; background: #f8f9fa; border-radius: 6px; color: #333;">{rec}</p>' for rec in recomendaciones])}
|
| 179 |
</div>
|
| 180 |
"""
|
| 181 |
|
| 182 |
+
final_status = f"✅ **Test completado** - {datetime.now().strftime('%H:%M:%S')}"
|
| 183 |
|
|
|
|
| 184 |
yield final_status, metrics_html, ai_html, rec_html, download, upload, ping, jitter
|
| 185 |
|
| 186 |
+
# Interfaz Gradio
|
| 187 |
with gr.Blocks(
|
| 188 |
theme=gr.themes.Soft(primary_hue="blue"),
|
| 189 |
+
title="🚀 Test de Velocidad con IA Real - DoctorLinux",
|
| 190 |
css="""
|
| 191 |
.gradio-container {
|
| 192 |
background: #ffffff !important;
|
|
|
|
| 201 |
gr.Markdown(
|
| 202 |
"""
|
| 203 |
<div style="text-align: center; padding: 30px; background: white; border-bottom: 3px solid #007bff; margin-bottom: 20px;">
|
| 204 |
+
<h1 style="margin: 0; font-size: 2.2em; color: #333;">🚀 Test de Velocidad con IA REAL</h1>
|
| 205 |
+
<h3 style="margin: 10px 0; color: #333;">DoctorLinux.com - Análisis inteligente con OpenAI</h3>
|
| 206 |
+
<p style="color: #666; margin: 5px 0;">Potenciado por Hugging Face + OpenAI 🤗</p>
|
| 207 |
</div>
|
| 208 |
"""
|
| 209 |
)
|
| 210 |
|
| 211 |
with gr.Row():
|
| 212 |
start_btn = gr.Button(
|
| 213 |
+
"🧠 Iniciar Test con IA Real",
|
| 214 |
variant="primary",
|
| 215 |
size="lg"
|
| 216 |
)
|
| 217 |
|
| 218 |
status = gr.Markdown(
|
| 219 |
+
"**Presiona el botón para comenzar el análisis con Inteligencia Artificial**"
|
| 220 |
)
|
| 221 |
|
| 222 |
metrics = gr.HTML()
|
|
|
|
| 232 |
gr.Markdown(
|
| 233 |
"""
|
| 234 |
<div style="text-align: center; margin-top: 30px; padding: 20px; background: #f8f9fa; border-radius: 10px;">
|
| 235 |
+
<p style="color: #333; margin: 5px 0; font-size: 14px;">🔧 Tecnología: Python + Gradio + OpenAI API</p>
|
| 236 |
<p style="color: #333; margin: 5px 0; font-size: 14px;">🌐 Desarrollado para <strong style="color: #007bff;">DoctorLinux.com</strong></p>
|
| 237 |
+
<p style="color: #666; margin: 5px 0; font-size: 12px;">🤖 Usa IA real para análisis de conexión</p>
|
| 238 |
</div>
|
| 239 |
"""
|
| 240 |
)
|
|
|
|
| 243 |
start_btn.click(
|
| 244 |
fn=run_test_with_ui,
|
| 245 |
outputs=[
|
| 246 |
+
status, metrics, ai_analysis, recommendations,
|
| 247 |
+
download_metric, upload_metric, ping_metric, jitter_metric
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
]
|
| 249 |
)
|
| 250 |
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
gradio>=4.
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
openai>=1.0.0
|