Spaces:
Running
Running
| 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() | |