File size: 1,689 Bytes
e88b43f
 
 
622bb7c
e88b43f
 
 
 
3b25301
 
 
e88b43f
622bb7c
e88b43f
ca9bf74
3b25301
 
ca9bf74
 
385935d
 
3b25301
622bb7c
385935d
 
 
 
622bb7c
 
 
ca9bf74
bca18f2
3b25301
 
 
 
 
 
 
 
 
 
 
bca18f2
ca9bf74
3b25301
 
 
 
 
e88b43f
 
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
import gradio as gr
from newspaper import Article

def extraer_info(url):
    try:
        articulo = Article(url, language='es')
        articulo.download()
        articulo.parse()
        titulo_y_texto = f"{articulo.title}\n\n{articulo.text}"
        imagen_url = articulo.top_image  # Imagen principal
        return titulo_y_texto, imagen_url
    except Exception as e:
        return f"[Error al extraer texto]: {str(e)}", None

with gr.Blocks() as demo:
    gr.Markdown("## Extractor de Texto e Imagen de Noticias 📰🖼️")
    gr.Markdown("Pega el enlace de una noticia para extraer su contenido y la imagen principal.")

    url_input = gr.Textbox(label="URL de la noticia", placeholder="https://...")

    texto_output = gr.Textbox(
        label="Texto extraído (incluye título)",
        lines=5,
        interactive=False,
        elem_id="texto_extraido"
    )

    imagen_output = gr.Image(label="Imagen principal", interactive=False)

    btn_extraer = gr.Button("Extraer texto e imagen")

    gr.HTML("""
    <button id="btn_copiar" style="margin-top:10px;" onclick="
        const txt = document.querySelector('#texto_extraido textarea');
        const btn = document.getElementById('btn_copiar');
        if (txt) {
            navigator.clipboard.writeText(txt.value).then(() => {
                const original = btn.innerHTML;
                btn.innerHTML = '✔️ Copiado';
                setTimeout(() => { btn.innerHTML = original; }, 2000);
            });
        }
    ">📋 Copiar texto</button>
    """)

    btn_extraer.click(
        fn=extraer_info,
        inputs=[url_input],
        outputs=[texto_output, imagen_output]
    )

demo.launch()