Jayeshbhaal commited on
Commit
4ced569
1 Parent(s): e0cd389
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -6,23 +6,49 @@ from newsapi import NewsApiClient
6
  from datetime import date, timedelta
7
  from transformers import pipeline
8
 
9
- #Model 2: Sentence Transformer
10
  HF_TOKEN = os.environ["newsapi"]
11
- #headers = {"Authorization": f"Bearer {HF_TOKEN}"}
 
12
 
13
  classifier = pipeline(model="cardiffnlp/twitter-roberta-base-sentiment")
14
  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']]
15
- # Initialization
16
- newsapi = NewsApiClient(api_key=HF_TOKEN)
17
 
18
- # /v2/everything
19
- all_articles = newsapi.get_everything(#q='bitcoin',
20
- sources='the-times-of-india',
 
21
  domains='timesofindia.indiatimes.com',
22
- from_param='2022-10-30',
23
- to='2022-10-30',
24
  language='en',
25
  sort_by='relevancy',)
26
- #page=2)
27
- all_articles['articles']
28
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from datetime import date, timedelta
7
  from transformers import pipeline
8
 
 
9
  HF_TOKEN = os.environ["newsapi"]
10
+ # Initialization
11
+ newsapi = NewsApiClient(api_key=HF_TOKEN)
12
 
13
  classifier = pipeline(model="cardiffnlp/twitter-roberta-base-sentiment")
14
  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']]
 
 
15
 
16
+ #Driver
17
+ def inference(newssource): #, date):
18
+ today = str(date.today())
19
+ all_articles = newsapi.get_everything(sources='the-times-of-india',
20
  domains='timesofindia.indiatimes.com',
21
+ from_param=today,
22
+ to=today,
23
  language='en',
24
  sort_by='relevancy',)
25
+ dictnews = { 'description' : [entry['description'] for entry in all_articles['articles']],
26
+ 'content' : [entry['content'] for entry in all_articles['articles']],
27
+ 'url' : [entry['url'] for entry in all_articles['articles']],
28
+ 'urlToImage' : [entry['urlToImage'] for entry in all_articles['articles']],
29
+ 'sentiment' : sentiment,
30
+ }
31
+ df = pd.DataFrame.from_dict(dictnews)
32
+ html_out = "<img src= " + dictnews['urlToImage'][0] + ">"
33
+ return df, html_out
34
+
35
+
36
+ #Gradio Blocks
37
+ with gr.Blocks() as demo:
38
+ with gr.Row():
39
+ in_newssource = gr.Dropdown(["Google News", "The Hindu", "Times Of India"], label='Choose a News Outlet')
40
+ #in_date = gr.Textbox(visible = False, value = today)
41
+
42
+ with gr.Row():
43
+ b1 = gr.Button("Get Positive News")
44
+ b2 = gr.Button("Get Negative News")
45
+
46
+ with gr.Row():
47
+ #sample
48
+ out_news = gr.HTML(label="First News Link", show_label=True)
49
+ out_dataframe = gr.Dataframe(wrap=True, datatype = ["str", "str", "markdown", "markdown", "str"])
50
+
51
+ b1.click(fn=inference, inputs=in_newssource, outputs=[out_dataframe, out_news])
52
+ b2.click(fn=inference, inputs=in_newssource, outputs=out_dataframe)
53
+
54
+ demo.launch(debug=True, show_error=True)