import requests import gradio as gr import pandas as pd import os from newsapi import NewsApiClient from datetime import date, timedelta from transformers import pipeline HF_TOKEN = os.environ["newsapi"] # Initialization newsapi = NewsApiClient(api_key=HF_TOKEN) classifier = pipeline(model="cardiffnlp/twitter-roberta-base-sentiment") sentiment = ['Negative' if classifier(entry['content'])[0]['label'] == 'LABEL_0' else 'Neutral' if classifier(entry['content'])[0]['label'] == 'LABEL_1' else 'Positive' for entry in all_articles['articles']] #Driver def inference(newssource): #, date): today = str(date.today()) all_articles = newsapi.get_everything(sources='the-times-of-india', domains='timesofindia.indiatimes.com', from_param=today, to=today, language='en', sort_by='relevancy',) dictnews = { 'description' : [entry['description'] for entry in all_articles['articles']], 'content' : [entry['content'] for entry in all_articles['articles']], 'url' : [entry['url'] for entry in all_articles['articles']], 'urlToImage' : [entry['urlToImage'] for entry in all_articles['articles']], 'sentiment' : sentiment, } df = pd.DataFrame.from_dict(dictnews) html_out = "" return df, html_out #Gradio Blocks with gr.Blocks() as demo: with gr.Row(): in_newssource = gr.Dropdown(["Google News", "The Hindu", "Times Of India"], label='Choose a News Outlet') #in_date = gr.Textbox(visible = False, value = today) with gr.Row(): b1 = gr.Button("Get Positive News") b2 = gr.Button("Get Negative News") with gr.Row(): #sample out_news = gr.HTML(label="First News Link", show_label=True) out_dataframe = gr.Dataframe(wrap=True, datatype = ["str", "str", "markdown", "markdown", "str"]) b1.click(fn=inference, inputs=in_newssource, outputs=[out_dataframe, out_news]) b2.click(fn=inference, inputs=in_newssource, outputs=out_dataframe) demo.launch(debug=True, show_error=True)