loayshabet commited on
Commit
9dbc598
·
verified ·
1 Parent(s): 3166bb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -32
app.py CHANGED
@@ -76,10 +76,20 @@ class NewsCache:
76
 
77
  cache = NewsCache(CACHE_SIZE)
78
 
79
- def fetch_rss_news(**category_sources):
80
  articles = []
81
  cutoff_time = datetime.now(pytz.UTC) - RSS_FETCH_INTERVAL
82
 
 
 
 
 
 
 
 
 
 
 
83
  logger.info(f"Selected sources: {category_sources}")
84
 
85
  for category, sources in category_sources.items():
@@ -156,23 +166,25 @@ def summarize_articles(articles, model_name):
156
  """)
157
  return "\n".join(summaries)
158
 
159
- def generate_summary(**kwargs):
160
- model_name = kwargs.pop('model_name') # Extract model_name from kwargs
161
-
162
- logger.info(f"Generating summary with sources: {kwargs} and model: {model_name}")
163
-
164
- # Check if any sources are selected
165
- if not any(kwargs.values()):
166
- return "Please select at least one news source."
167
-
168
  try:
169
- articles = fetch_rss_news(**kwargs)
 
 
 
 
 
 
 
 
170
  if not articles:
171
  return "No recent news found from the selected sources."
 
172
  return summarize_articles(articles, model_name)
173
  except Exception as e:
174
- logger.error(f"Error in generate_summary: {str(e)}")
175
- return f"An error occurred while generating the summary. Please try again."
176
 
177
  # Gradio Interface
178
  demo = gr.Blocks()
@@ -181,16 +193,50 @@ with demo:
181
  gr.Markdown("# 📰 AI News Summarizer")
182
 
183
  with gr.Row():
184
- checkbox_groups = {}
185
  with gr.Column():
186
- # Create checkbox groups for each category
187
- for category in CATEGORIES:
188
- checkbox_groups[category.lower().replace(" ", "_") + "_sources"] = gr.CheckboxGroup(
189
- choices=list(NEWS_SOURCES[category].keys()),
190
- label=f"{category} Sources",
191
- value=[]
192
- )
193
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  with gr.Column():
195
  model_selector = gr.Radio(
196
  choices=list(SUMMARIZER_MODELS.keys()),
@@ -201,19 +247,18 @@ with demo:
201
  summarize_button = gr.Button("Get News Summary")
202
  summary_output = gr.Textbox(label="News Summary", lines=20)
203
 
204
- def get_summary(**kwargs):
205
- try:
206
- selected_model = kwargs.pop('model_selector')
207
- model_name = SUMMARIZER_MODELS[selected_model]
208
- return generate_summary(model_name=model_name, **kwargs)
209
- except Exception as e:
210
- logger.error(f"Error in get_summary: {str(e)}")
211
- return "An error occurred while processing your request. Please try again."
212
-
213
  # Connect the components to the summary function
214
  summarize_button.click(
215
  get_summary,
216
- inputs=dict(list(checkbox_groups.items()) + [("model_selector", model_selector)]),
 
 
 
 
 
 
 
 
217
  outputs=summary_output
218
  )
219
 
 
76
 
77
  cache = NewsCache(CACHE_SIZE)
78
 
79
+ def fetch_rss_news(tech_sources, business_sources, science_sources, world_sources, sports_sources, health_sources):
80
  articles = []
81
  cutoff_time = datetime.now(pytz.UTC) - RSS_FETCH_INTERVAL
82
 
83
+ # Create a mapping of selected sources
84
+ category_sources = {
85
+ "Technology": tech_sources if tech_sources else [],
86
+ "Business": business_sources if business_sources else [],
87
+ "Science": science_sources if science_sources else [],
88
+ "World News": world_sources if world_sources else [],
89
+ "Sports": sports_sources if sports_sources else [],
90
+ "Health": health_sources if health_sources else []
91
+ }
92
+
93
  logger.info(f"Selected sources: {category_sources}")
94
 
95
  for category, sources in category_sources.items():
 
166
  """)
167
  return "\n".join(summaries)
168
 
169
+ def get_summary(tech_sources, business_sources, science_sources, world_sources,
170
+ sports_sources, health_sources, selected_model):
 
 
 
 
 
 
 
171
  try:
172
+ # Check if any sources are selected
173
+ if not any([tech_sources, business_sources, science_sources,
174
+ world_sources, sports_sources, health_sources]):
175
+ return "Please select at least one news source."
176
+
177
+ model_name = SUMMARIZER_MODELS[selected_model]
178
+ articles = fetch_rss_news(tech_sources, business_sources, science_sources,
179
+ world_sources, sports_sources, health_sources)
180
+
181
  if not articles:
182
  return "No recent news found from the selected sources."
183
+
184
  return summarize_articles(articles, model_name)
185
  except Exception as e:
186
+ logger.error(f"Error in get_summary: {str(e)}")
187
+ return "An error occurred while processing your request. Please try again."
188
 
189
  # Gradio Interface
190
  demo = gr.Blocks()
 
193
  gr.Markdown("# 📰 AI News Summarizer")
194
 
195
  with gr.Row():
 
196
  with gr.Column():
197
+ # Technology sources
198
+ tech_sources = gr.CheckboxGroup(
199
+ choices=list(NEWS_SOURCES["Technology"].keys()),
200
+ label="Technology Sources",
201
+ value=[]
202
+ )
203
+
204
+ # Business sources
205
+ business_sources = gr.CheckboxGroup(
206
+ choices=list(NEWS_SOURCES["Business"].keys()),
207
+ label="Business Sources",
208
+ value=[]
209
+ )
210
+
211
+ # Science sources
212
+ science_sources = gr.CheckboxGroup(
213
+ choices=list(NEWS_SOURCES["Science"].keys()),
214
+ label="Science Sources",
215
+ value=[]
216
+ )
217
+
218
+ with gr.Column():
219
+ # World News sources
220
+ world_sources = gr.CheckboxGroup(
221
+ choices=list(NEWS_SOURCES["World News"].keys()),
222
+ label="World News Sources",
223
+ value=[]
224
+ )
225
+
226
+ # Sports sources
227
+ sports_sources = gr.CheckboxGroup(
228
+ choices=list(NEWS_SOURCES["Sports"].keys()),
229
+ label="Sports Sources",
230
+ value=[]
231
+ )
232
+
233
+ # Health sources
234
+ health_sources = gr.CheckboxGroup(
235
+ choices=list(NEWS_SOURCES["Health"].keys()),
236
+ label="Health Sources",
237
+ value=[]
238
+ )
239
+
240
  with gr.Column():
241
  model_selector = gr.Radio(
242
  choices=list(SUMMARIZER_MODELS.keys()),
 
247
  summarize_button = gr.Button("Get News Summary")
248
  summary_output = gr.Textbox(label="News Summary", lines=20)
249
 
 
 
 
 
 
 
 
 
 
250
  # Connect the components to the summary function
251
  summarize_button.click(
252
  get_summary,
253
+ inputs=[
254
+ tech_sources,
255
+ business_sources,
256
+ science_sources,
257
+ world_sources,
258
+ sports_sources,
259
+ health_sources,
260
+ model_selector
261
+ ],
262
  outputs=summary_output
263
  )
264