Spaces:
Runtime error
Runtime error
Upload article_summarizer.py
Browse files- article_summarizer.py +73 -0
article_summarizer.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from newspaper import Article
|
| 2 |
+
from langchain.chat_models import ChatGooglePalm
|
| 3 |
+
from langchain.schema import HumanMessage
|
| 4 |
+
import time
|
| 5 |
+
import pyttsx3
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
chat = ChatGooglePalm(google_api_key=os.getenv("GOOGLE_API_KEY"), temperature=0)
|
| 15 |
+
|
| 16 |
+
headers = {
|
| 17 |
+
"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"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
article_url = input("URL: ")
|
| 21 |
+
print("\n")
|
| 22 |
+
|
| 23 |
+
session = requests.Session()
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = session.get(article_url, headers=headers, timeout=10)
|
| 27 |
+
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
arts = Article(article_url)
|
| 30 |
+
arts.download()
|
| 31 |
+
arts.parse()
|
| 32 |
+
|
| 33 |
+
print(f"Title: {arts.title}")
|
| 34 |
+
# print(f"Text: {article.text}")
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
print(f"Failed to fetch article at {article_url}")
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error occured while fetching article at {article_url}: {e}")
|
| 41 |
+
|
| 42 |
+
# article_title = article.title
|
| 43 |
+
# article_text = article.text
|
| 44 |
+
|
| 45 |
+
template = """You are a very good assistant that summarizes online articles.
|
| 46 |
+
|
| 47 |
+
Here's the article you want to summarize.
|
| 48 |
+
|
| 49 |
+
==================
|
| 50 |
+
Title: {article_title}
|
| 51 |
+
|
| 52 |
+
{article_text}
|
| 53 |
+
==================
|
| 54 |
+
|
| 55 |
+
Now, provide a summarized version of the article in serialwise list.
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def art_sums(url):
|
| 60 |
+
prompt = template.format(article_title=arts.title, article_text=arts.text)
|
| 61 |
+
url = [HumanMessage(content=prompt)]
|
| 62 |
+
print("\n")
|
| 63 |
+
summary = chat(url)
|
| 64 |
+
sum = summary.content
|
| 65 |
+
sum = sum.replace("*", "")
|
| 66 |
+
return sum
|
| 67 |
+
|
| 68 |
+
# art_sums(messages)
|
| 69 |
+
|
| 70 |
+
demo = gr.Interface(fn=art_sums, inputs="textbox", outputs="textbox",
|
| 71 |
+
title="Summarize news with me")
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch(share=True)
|