Spaces:
Runtime error
Runtime error
achimoraites
commited on
Commit
•
225fbae
1
Parent(s):
f655421
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from newspaper import Config, Article
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
summarizer = pipeline("summarization", model="achimoraites/flan-t5-base-xsum")
|
7 |
+
|
8 |
+
def summarize(text):
|
9 |
+
return summarizer(text, max_length=128, min_length=30)[0]['summary_text']
|
10 |
+
|
11 |
+
def summarize_article(url):
|
12 |
+
config = Config()
|
13 |
+
config.browser_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
|
14 |
+
config.request_timeout = 6
|
15 |
+
|
16 |
+
article = Article(url, config=config)
|
17 |
+
summary = ''
|
18 |
+
try:
|
19 |
+
article.download()
|
20 |
+
article.parse()
|
21 |
+
text = article.text()
|
22 |
+
summary = summarize(text)
|
23 |
+
except Exception as e:
|
24 |
+
return f"Failed to summarize. Error: {e}"
|
25 |
+
return summary
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
iface = gr.Interface(fn=summarize_article,
|
30 |
+
outputs="text",
|
31 |
+
live=False,
|
32 |
+
capture_session=True,
|
33 |
+
inputs=gr.inputs.Textbox(lines=1, label="Page URL"),
|
34 |
+
outputs="text",
|
35 |
+
title="Page Summarizer",
|
36 |
+
)
|
37 |
+
iface.launch(inline = False)
|