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()