Jayeshbhaal commited on
Commit
dabe490
1 Parent(s): 793fd68
Files changed (1) hide show
  1. app.py +68 -18
app.py CHANGED
@@ -38,8 +38,8 @@ print(f"sentiment_toi length is {len(sentiment_toi)}")
38
  print(f"all_articles_toi length is {len(all_articles_toi['articles'])}")
39
 
40
 
41
- #Driver
42
- def inference(newssource): #, date):
43
 
44
  if newssource == "Times Of India":
45
  sentiment = sentiment_toi
@@ -54,7 +54,7 @@ def inference(newssource): #, date):
54
  url = ["<a href=" + entry['url'] + ' target="_blank">Click here for the original news article</a>' for entry in all_articles['articles']]
55
  urlToImage = ["<img src= " + entry['urlToImage']+">" for entry in all_articles['articles']]
56
 
57
- print("********************************************************")
58
  print(f"Newssource is - {newssource}")
59
  print(f"description length is - {len(description)}")
60
  print(f"content length is - {len(content)}")
@@ -65,20 +65,70 @@ def inference(newssource): #, date):
65
  dictnews = { 'description' : description, 'content' : content, 'url' : url, 'urlToImage' : urlToImage, 'sentiment' : sentiment}
66
 
67
  df = pd.DataFrame.from_dict(dictnews)
68
- #print("************** df start ****************")
69
- #print(df)
70
- #print(f"description : {description}")
71
- print(f"content : {content}")
72
- print(f"url : {url}")
73
- print(f"urlToIm : {urlToImage}")
74
- print(f"sentiment : {sentiment}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- print("************** df end ****************")
 
 
 
77
  print(f"dataframe shape is :,{df.shape}")
78
- if dictnews['urlToImage'][0] is not None :
79
- html_out = "<img src= " + dictnews['urlToImage'][0] + ">"
80
- print(f"html_out is : {html_out}")
81
- return df #, html_out
82
 
83
 
84
  #Gradio Blocks
@@ -97,8 +147,8 @@ with gr.Blocks() as demo:
97
  #out_news = gr.HTML(label="First News Link", show_label=True)
98
  out_dataframe = gr.Dataframe(wrap=True, datatype = ["str", "str", "markdown", "markdown", "str"])
99
 
100
- b1.click(fn=inference, inputs=in_newssource, outputs=out_dataframe) #, out_news])
101
- b2.click(fn=inference, inputs=in_newssource, outputs=out_dataframe) #, out_news])
102
- b2.click(fn=inference, inputs=in_newssource, outputs=out_dataframe) #, out_news])
103
 
104
  demo.launch(debug=True, show_error=True)
 
38
  print(f"all_articles_toi length is {len(all_articles_toi['articles'])}")
39
 
40
 
41
+ #Driver positive
42
+ def inference_pos(newssource): #, date):
43
 
44
  if newssource == "Times Of India":
45
  sentiment = sentiment_toi
 
54
  url = ["<a href=" + entry['url'] + ' target="_blank">Click here for the original news article</a>' for entry in all_articles['articles']]
55
  urlToImage = ["<img src= " + entry['urlToImage']+">" for entry in all_articles['articles']]
56
 
57
+ print("********************* Positive News **************************")
58
  print(f"Newssource is - {newssource}")
59
  print(f"description length is - {len(description)}")
60
  print(f"content length is - {len(content)}")
 
65
  dictnews = { 'description' : description, 'content' : content, 'url' : url, 'urlToImage' : urlToImage, 'sentiment' : sentiment}
66
 
67
  df = pd.DataFrame.from_dict(dictnews)
68
+ df = df.loc[df['sentiment'] == 'Positive']
69
+
70
+ print(f"dataframe shape is :,{df.shape}")
71
+ return df
72
+
73
+ #Driver - negative
74
+ def inference_neg(newssource): #, date):
75
+
76
+ if newssource == "Times Of India":
77
+ sentiment = sentiment_toi
78
+ all_articles = all_articles_toi
79
+ elif newssource == "Top Headlines":
80
+ sentiment = sentiment_tophead
81
+ all_articles = all_top_headlines
82
+
83
+ description = [entry['description'] for entry in all_articles['articles']]
84
+ content = [entry['content'] for entry in all_articles['articles']]
85
+ url = ["<a href=" + entry['url'] + ' target="_blank">Click here for the original news article</a>' for entry in all_articles['articles']]
86
+ urlToImage = ["<img src= " + entry['urlToImage']+">" for entry in all_articles['articles']]
87
+
88
+ print("********************* Negative News ***********************")
89
+ print(f"Newssource is - {newssource}")
90
+ print(f"description length is - {len(description)}")
91
+ print(f"content length is - {len(content)}")
92
+ print(f"url length is - {len(url)}")
93
+ print(f"urlToImage length is - {len(urlToImage)}")
94
+ print(f"sentiment length is - {len(sentiment)}")
95
+
96
+ dictnews = { 'description' : description, 'content' : content, 'url' : url, 'urlToImage' : urlToImage, 'sentiment' : sentiment}
97
+
98
+ df = pd.DataFrame.from_dict(dictnews)
99
+ df = df.loc[df['sentiment'] == 'Negative']
100
+ print(f"dataframe shape is :,{df.shape}")
101
+ return df
102
+
103
+ #Driver - neutral
104
+ def inference_neut(newssource): #, date):
105
+
106
+ if newssource == "Times Of India":
107
+ sentiment = sentiment_toi
108
+ all_articles = all_articles_toi
109
+ elif newssource == "Top Headlines":
110
+ sentiment = sentiment_tophead
111
+ all_articles = all_top_headlines
112
+
113
+ description = [entry['description'] for entry in all_articles['articles']]
114
+ content = [entry['content'] for entry in all_articles['articles']]
115
+ url = ["<a href=" + entry['url'] + ' target="_blank">Click here for the original news article</a>' for entry in all_articles['articles']]
116
+ urlToImage = ["<img src= " + entry['urlToImage']+">" for entry in all_articles['articles']]
117
+
118
+ print("********************* Neutral News ***********************")
119
+ print(f"Newssource is - {newssource}")
120
+ print(f"description length is - {len(description)}")
121
+ print(f"content length is - {len(content)}")
122
+ print(f"url length is - {len(url)}")
123
+ print(f"urlToImage length is - {len(urlToImage)}")
124
+ print(f"sentiment length is - {len(sentiment)}")
125
 
126
+ dictnews = { 'description' : description, 'content' : content, 'url' : url, 'urlToImage' : urlToImage, 'sentiment' : sentiment}
127
+
128
+ df = pd.DataFrame.from_dict(dictnews)
129
+ df = df.loc[df['sentiment'] == 'Neutral']
130
  print(f"dataframe shape is :,{df.shape}")
131
+ return df
 
 
 
132
 
133
 
134
  #Gradio Blocks
 
147
  #out_news = gr.HTML(label="First News Link", show_label=True)
148
  out_dataframe = gr.Dataframe(wrap=True, datatype = ["str", "str", "markdown", "markdown", "str"])
149
 
150
+ b1.click(fn=inference_pos, inputs=in_newssource, outputs=out_dataframe) #, out_news])
151
+ b2.click(fn=inference_neg, inputs=in_newssource, outputs=out_dataframe) #, out_news])
152
+ b3.click(fn=inference_neut, inputs=in_newssource, outputs=out_dataframe) #, out_news])
153
 
154
  demo.launch(debug=True, show_error=True)