#!/usr/bin/env python3 import gradio as gr import requests import json from typing import Optional # ======================== # CONFIG # ======================== API_URL = None # Si tienes una API desplegada, ponla aquí MODEL_NAME = None # Si tienes un modelo local, ponlo aquí # ======================== # FUNCIONES DE NEGOCIO # ======================== def summarize_text(text: str, style: str = \"concise\") -> str: \"\"\"Resume texto usando la API o modelo local.\"\"\" if API_URL: try: response = requests.post( API_URL, json={\"text\": text, \"style\": style}, timeout=30 ) return response.json().get(\"summary\", \"Error en API\") except Exception as e: return f\"Error conectando a API: {e}\" else: return f\"📋 Modo demo: Configura API_URL para resumen real.\n\nTexto recibido ({len(text)} chars):\n{text[:500]}...\" def check_word_count(text: str) -> int: return len(text.split()) def generate_report(text: str, include_keywords: bool = False) -> dict: \"\"\"Genera un mini-report del documento.\"\"\" words = text.split() sentences = text.split('.') return { \"word_count\": len(words), \"sentence_count\": len(sentences), \"avg_word_length\": sum(len(w) for w in words) / max(len(words), 1), \"estimated_read_time\": f\"{len(words) // 200} min\" # 200 WPM } # ======================== # UI GRADIO # ======================== with gr.Blocks(title=\"📝 Smart Summarizer API\", theme=\"default\") as demo: gr.Markdown(\"# 📝 Smart Document Summarizer\n### API para resúmenes inteligentes de documentos\" ) with gr.Tabs(): with gr.TabItem(\"🔍 Resumir\"): with gr.Row(): with gr.Column(scale=2): input_text = gr.Textbox( label=\"Texto a resumir\", placeholder=\"Pega aquí el texto, URL, o documento...\", lines=12 ) with gr.Row(): style = gr.Dropdown( choices=[\"concise\", \"detailed\", \"bullet_points\"], value=\"concise\", label=\"Estilo\" ) max_length = gr.Slider(50, 500, value=150, step=10, label=\"Máx palabras\") submit_btn = gr.Button(\"🚀 Resumir\", variant=\"primary\") with gr.Column(scale=2): output = gr.Textbox(label=\"Resumen\", lines=12, interactive=False) with gr.TabItem(\"📊 Analizar\"): with gr.Row(): analysis_input = gr.Textbox(label=\"Texto para análisis\", lines=8) analysis_output = gr.JSON(label=\"Reporte\") analyze_btn = gr.Button(\"📊 Analizar documento\") with gr.TabItem(\"💡 Demo\") as demo_tab: gr.Markdown(\"\"\"## 🎯 Cómo usar esta API **Modo Free (demo):** Uso ilimitado de la interfaz web **Modo Pro:** - 100 req/minuto - Modelos especializados (legal, médico, técnico) - Soporte prioritario ### Endpoints de la API ```bash curl -X POST https://api.huggingface.co/summarize \\ -H \"Authorization: Bearer YOUR_API_KEY\" \\ -d '{\"text\": \"tu texto aquí\"}' ``` **Precios:** $0 (free) / $10/mes (pro) \"\"\") # EVENTOS submit_btn.click( fn=summarize_text, inputs=[input_text, style], outputs=output ) analyze_btn.click( fn=generate_report, inputs=[analysis_input], outputs=analysis_output ) demo.launch(server_name=\"0.0.0.0\")