Spaces:
Sleeping
Sleeping
app.py
Browse files- app.py +69 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
5 |
+
headers = {
|
6 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
|
7 |
+
}
|
8 |
+
|
9 |
+
def summarize (article_url):
|
10 |
+
session = requests.Session()
|
11 |
+
|
12 |
+
try:
|
13 |
+
response = session.get(article_url, headers = headers, timeout = 10)
|
14 |
+
|
15 |
+
if response.status_code == 200:
|
16 |
+
article = Article(article_url)
|
17 |
+
article.download()
|
18 |
+
article.parse()
|
19 |
+
|
20 |
+
# print(f'Title: {article.title}')
|
21 |
+
# print(f'Content: {article.text}')
|
22 |
+
|
23 |
+
else:
|
24 |
+
print(f'Failed to retrieve article at url: {article_url}')
|
25 |
+
except Exception as e:
|
26 |
+
print(f'Error fectching article at url: {article_url}')
|
27 |
+
|
28 |
+
article_title = article.title
|
29 |
+
article_text = article.text
|
30 |
+
|
31 |
+
template = """You are a very good assistant that summarizes online articles
|
32 |
+
|
33 |
+
Here's the article you want to summarize
|
34 |
+
|
35 |
+
============
|
36 |
+
Title: {article_title}
|
37 |
+
|
38 |
+
{article_text}
|
39 |
+
|
40 |
+
============
|
41 |
+
|
42 |
+
Write a summary of the previous article in a bulleted list.
|
43 |
+
|
44 |
+
"""
|
45 |
+
|
46 |
+
prompt = template.format(article_title = article_title, article_text = article_text)
|
47 |
+
|
48 |
+
|
49 |
+
messages = [HumanMessage(content= prompt)]
|
50 |
+
|
51 |
+
# print('=============== Model================')
|
52 |
+
|
53 |
+
# print (messages)
|
54 |
+
chat = ChatOpenAI(model_name = 'gpt-3.5-turbo', temperature=0)
|
55 |
+
|
56 |
+
summary = chat(messages)
|
57 |
+
|
58 |
+
return(summary.content)
|
59 |
+
|
60 |
+
|
61 |
+
demo = gr.Interface(
|
62 |
+
fn= summarize,
|
63 |
+
inputs = ['text'],
|
64 |
+
outputs = ['text'],
|
65 |
+
article = 'The project takes in a url of an article or blog and returns a summary of it.'
|
66 |
+
)
|
67 |
+
|
68 |
+
demo.launch()
|
69 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
langchain
|
3 |
+
python3.10
|
4 |
+
newspaper3k
|