Spaces:
Sleeping
Sleeping
File size: 1,794 Bytes
490893f 32d231a 0c64383 22e1c6c f616787 22e1c6c ad78cf5 22e1c6c f616787 490893f a8bf99c 490893f |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import requests
import os
import gradio as gr
from newspaper import Article
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
headers = {
'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'
}
def summarize (article_url):
session = requests.Session()
try:
response = session.get(article_url, headers = headers, timeout = 10)
if response.status_code == 200:
article = Article(article_url)
article.download()
article.parse()
# print(f'Title: {article.title}')
# print(f'Content: {article.text}')
else:
print(f'Failed to retrieve article at url: {article_url}')
except Exception as e:
print(f'Error fectching article at url: {article_url}')
article_title = article.title
article_text = article.text
template = """You are a very good assistant that summarizes online articles
Here's the article you want to summarize
============
Title: {article_title}
{article_text}
============
Write a summary of the previous article in a bulleted list.
"""
prompt = template.format(article_title = article_title, article_text = article_text)
messages = [HumanMessage(content= prompt)]
chat = ChatOpenAI(model_name = 'gpt-3.5-turbo', temperature=0)
summary = chat(messages)
return(summary.content)
demo = gr.Interface(
fn= summarize,
inputs = ['text'],
outputs = ['text'],
article = 'The project takes in a url of an article or blog and returns a summary of it.'
)
demo.launch()
|