pknayak commited on
Commit
7e56340
·
1 Parent(s): 997d553

Adding blocks and changinc call function method signature

Browse files

* Replacing the Interface with Blocks from GRadio
* Modifying the function`call_functions` by only sending the domain of the news channel
* restricting the number of columns in the dataframe.

Files changed (1) hide show
  1. app.py +46 -16
app.py CHANGED
@@ -17,11 +17,25 @@ def creating_data_dir(directory_path):
17
  else:
18
  print(f"Failed to create directory '{directory_path}'.")
19
 
20
- def retrieve_news_per_keyword(api,keywords):
21
- # %time
 
 
 
22
  for keyword in keywords:
23
  # print(f"{api} \n {keyword}")
24
- response = api.news_api( q= keyword , country = "us", language = 'en', full_content = True)
 
 
 
 
 
 
 
 
 
 
 
25
  # writing to a file
26
  file_path = os.path.join(directory_path, f"response_{keyword}.json")
27
  with open(file_path, "w") as outfile:
@@ -69,30 +83,34 @@ def convert_json_to_csv(file_name):
69
  json_data_df = pd.read_json(file_name)
70
  # json_data_df.head()
71
 
72
- columns = [ 'title', 'keywords', 'creator', 'description', 'content', 'pubDate', 'country', 'category', 'language', 'file_keyword' ]
 
73
  csv_file_name = 'combined_news_response.csv'
74
  json_data_df[columns].to_csv(csv_file_name)
75
  print(f'{csv_file_name} is created')
 
 
76
 
77
 
 
78
  # API key authorization, Initialize the client with your API key
79
  NEWSDATA_API_KEY = "pub_2915202f68e543f70bb9aba9611735142c1fd"
80
  keywords = [ "GDP", "CPI", "PPI", "Unemployment Rate", "Interest Rates", "Inflation", "Trade Balance", "Retail Sales", "Manufacturing Index", "Earnings Reports", "Revenue Growth", "Profit Margins", "Earnings Surprises", "Geopolitical Events", "Trade Tensions", "Elections", "Natural Disasters", "Global Health Crises", "Oil Prices", "Gold Prices", "Precious Metals", "Agricultural Commodities", "Federal Reserve", "ECB", "Forex Market", "Exchange Rates", "Currency Pairs", "Tech Company Earnings", "Tech Innovations", "Retail Trends", "Consumer Sentiment", "Financial Regulations", "Government Policies", "Technical Analysis", "Fundamental Analysis", "Cryptocurrency News", "Bitcoin", "Altcoins", "Cryptocurrency Regulations", "S&P 500", "Dow Jones", "NASDAQ", "Market Analysis", "Stock Market Indices" ]
 
81
 
82
  # creating a data directory
83
  # Define the directory path you want to create
84
  directory_path = './data'
85
 
86
- def call_functions(directory_path='./data'):
87
  creating_data_dir(directory_path)
88
  items = os.listdir(directory_path)
89
-
90
- file_name = './combined_news_response.json'
91
 
 
92
  if len(items) == 0:
93
  print(f"Directory '{directory_path}' is empty.")
94
  api = NewsDataApiClient(apikey=NEWSDATA_API_KEY)
95
- retrieve_news_per_keyword(api,keywords)
96
  combine_responses_into_one(directory_path)
97
  convert_json_to_csv(file_name)
98
  elif len(items) >= 2:
@@ -115,13 +133,25 @@ def call_functions(directory_path='./data'):
115
  return f"CSV file '{csv_file_name}' not found."
116
 
117
 
118
- # Create a Gradio interface
119
- iface = gr.Interface(
120
- fn=call_functions,
121
- inputs=gr.components.Textbox(label="Directory Path"),
122
- outputs=gr.components.Dataframe(type="pandas")
123
- )
124
 
125
 
126
- # Launch the Gradio app
127
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
  print(f"Failed to create directory '{directory_path}'.")
19
 
20
+ def retrieve_news_per_keyword(api, keywords, domain):
21
+
22
+ selected_domain = domain
23
+ selected_domain_url = domain_dict[domain]
24
+
25
  for keyword in keywords:
26
  # print(f"{api} \n {keyword}")
27
+ # response = api.news_api( q= keyword , country = "us", language = 'en', full_content = True)
28
+
29
+ response = api.news_api(
30
+ # domain=['bbc', 'forbes' , 'businessinsider_us'], # 'bbc', 'forbes' , 'businessinsider_us',
31
+ domainurl=['bbc.com', 'forbes.com', 'businessinsider.com'], # 'bbc.com', 'forbes.com', 'businessinsider.com',
32
+ category='business' ,
33
+ # country = "us",
34
+ timeframe=48,
35
+ language = 'en',
36
+ full_content = True,
37
+ size=10
38
+ )
39
  # writing to a file
40
  file_path = os.path.join(directory_path, f"response_{keyword}.json")
41
  with open(file_path, "w") as outfile:
 
83
  json_data_df = pd.read_json(file_name)
84
  # json_data_df.head()
85
 
86
+ # columns = [ 'title', 'keywords', 'creator', 'description', 'content', 'pubDate', 'country', 'category', 'language', 'file_keyword' ]
87
+ columns = [ 'title', 'pubDate', 'content', 'country', 'category', 'language' ]
88
  csv_file_name = 'combined_news_response.csv'
89
  json_data_df[columns].to_csv(csv_file_name)
90
  print(f'{csv_file_name} is created')
91
+
92
+
93
 
94
 
95
+ #-------------------------------------First Function called from the UI----------------------------
96
  # API key authorization, Initialize the client with your API key
97
  NEWSDATA_API_KEY = "pub_2915202f68e543f70bb9aba9611735142c1fd"
98
  keywords = [ "GDP", "CPI", "PPI", "Unemployment Rate", "Interest Rates", "Inflation", "Trade Balance", "Retail Sales", "Manufacturing Index", "Earnings Reports", "Revenue Growth", "Profit Margins", "Earnings Surprises", "Geopolitical Events", "Trade Tensions", "Elections", "Natural Disasters", "Global Health Crises", "Oil Prices", "Gold Prices", "Precious Metals", "Agricultural Commodities", "Federal Reserve", "ECB", "Forex Market", "Exchange Rates", "Currency Pairs", "Tech Company Earnings", "Tech Innovations", "Retail Trends", "Consumer Sentiment", "Financial Regulations", "Government Policies", "Technical Analysis", "Fundamental Analysis", "Cryptocurrency News", "Bitcoin", "Altcoins", "Cryptocurrency Regulations", "S&P 500", "Dow Jones", "NASDAQ", "Market Analysis", "Stock Market Indices" ]
99
+ domain_dict = {'bbc': 'bbc.com', 'forbes': 'forbes.com', 'businessinsider_us': 'businessinsider.com'}
100
 
101
  # creating a data directory
102
  # Define the directory path you want to create
103
  directory_path = './data'
104
 
105
+ def call_functions(domain):
106
  creating_data_dir(directory_path)
107
  items = os.listdir(directory_path)
 
 
108
 
109
+ file_name = './combined_news_response.json'
110
  if len(items) == 0:
111
  print(f"Directory '{directory_path}' is empty.")
112
  api = NewsDataApiClient(apikey=NEWSDATA_API_KEY)
113
+ retrieve_news_per_keyword(api, keywords, domain)
114
  combine_responses_into_one(directory_path)
115
  convert_json_to_csv(file_name)
116
  elif len(items) >= 2:
 
133
  return f"CSV file '{csv_file_name}' not found."
134
 
135
 
 
 
 
 
 
 
136
 
137
 
138
+ #----------------------------GRADIO APP--------------------------------------#
139
+ # # GRADIO APP USING INTERFACE
140
+ # # Create a Gradio interface
141
+ # iface = gr.Interface(
142
+ # fn=call_functions,
143
+ # inputs=gr.components.Textbox(label="Directory Path"),
144
+ # outputs=gr.components.Dataframe(type="pandas")
145
+ # )
146
+ # # Launch the Gradio app
147
+ # iface.launch(debug=True)
148
+
149
+ # GRADIO APP USING BLOCKS
150
+ with gr.Blocks() as demo:
151
+ ui_domain = gr.Dropdown(["bbc", "forbes", "businessinsider_us"], label="Select Domain")
152
+ df_output = gr.Dataframe(type="pandas")
153
+ retrieve_button = gr.Button("Retrieve news")
154
+
155
+ retrieve_button.click(call_functions, inputs=ui_domain, outputs=df_output)
156
+
157
+ demo.launch(debug=True)