File size: 2,221 Bytes
5430213
 
bd02e1e
ecec571
bd02e1e
 
 
 
283921a
 
4ced569
 
283921a
 
 
 
4ced569
 
 
 
283921a
4ced569
 
283921a
 
4ced569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = "<img src= " + dictnews['urlToImage'][0] + ">"
  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)