File size: 3,989 Bytes
5b0e22b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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\")