from newspaper import Article from langchain.chat_models import ChatGooglePalm from langchain.schema import HumanMessage # from dotenv import load_dotenv import requests import os import gradio as gr # load_dotenv() chat = ChatGooglePalm(google_api_key=os.getenv("GOOGLE_API_KEY"), temperature=0) 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" } article_url = input("URL: ") print("\n") session = requests.Session() try: response = session.get(article_url, headers=headers, timeout=10) if response.status_code == 200: arts = Article(article_url) arts.download() arts.parse() print(f"Title: {arts.title}") # print(f"Text: {article.text}") else: print(f"Failed to fetch article at {article_url}") except Exception as e: print(f"Error occured while fetching article at {article_url}: {e}") # 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} ================== Now, provide a summarized version of the article in serialwise list. """ def art_sums(url): prompt = template.format(article_title=arts.title, article_text=arts.text) url = [HumanMessage(content=prompt)] print("\n") summary = chat(url) sum = summary.content sum = sum.replace("*", "") return sum # art_sums(messages) demo = gr.Interface(fn=art_sums, inputs="textbox", outputs="textbox", title="Summarize news with me") if __name__ == "__main__": demo.launch(share=True)