loayshabet commited on
Commit
db799a7
Β·
verified Β·
1 Parent(s): 2c17938

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -56
app.py CHANGED
@@ -21,7 +21,7 @@ ARTICLE_LIMIT = 5 # Limit to the last 5 articles
21
 
22
  # News sources
23
  NEWS_SOURCES = {
24
- "Technology": {
25
  "TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml",
26
  "reutersagency": "https://www.reutersagency.com/feed/?best-topics=tech&post_type=best"
27
  },
@@ -118,8 +118,8 @@ def summarize_text(text):
118
  logging.error(f"Summarization failed: {e}")
119
  return "Summary unavailable."
120
 
121
- def summarize_articles(articles):
122
- """Summarize the last 5 fetched articles."""
123
  summaries = []
124
  for article in articles:
125
  try:
@@ -134,37 +134,42 @@ def summarize_articles(articles):
134
  summary = summarize_text(content)
135
 
136
  if summary:
137
- summaries.append(f"""
138
- πŸ“° {title}
139
-
140
- - πŸ“ Category: {category}
141
- - πŸ’‘ Source: {source}
142
- - ⏰ Published: {published}
143
-
144
- πŸ“ƒ Summary:
145
- {summary}
146
- πŸ”— Read the full article here ({link})
147
-
148
- ---
149
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  except Exception as e:
151
  logging.error(f"Error summarizing article: {e}")
152
  continue
153
  return summaries
154
 
155
- def generate_user_summary(name):
156
- """Generate a personalized news summary based on user preferences."""
157
- # Load preferences
158
- try:
159
- with open(f"user_preferences/preferences_{name}.json") as f:
160
- preferences = json.load(f)
161
- except FileNotFoundError:
162
- return "Preferences not found. Please set your preferences first."
163
- except Exception as e:
164
- logging.error(f"Error loading preferences: {e}")
165
- return "Failed to load preferences."
166
-
167
- categories = preferences.get("interests", [])
168
  if not categories:
169
  return "No categories selected. Please update your preferences."
170
 
@@ -174,7 +179,7 @@ def generate_user_summary(name):
174
  return "No recent news found in your selected categories."
175
 
176
  # Summarize all articles
177
- summaries = summarize_articles(articles)
178
 
179
  # Combine and return summaries
180
  return "\n\n".join(summaries) if summaries else "No summaries available."
@@ -185,36 +190,21 @@ demo = gr.Blocks()
185
  with demo:
186
  gr.Markdown("# πŸ“° AI News Summarizer")
187
 
188
- with gr.Tab("Set Preferences"):
189
- name_input = gr.Textbox(label="Your Name")
190
  interests = gr.CheckboxGroup(
191
  choices=list(NEWS_SOURCES.keys()),
192
  label="Select Your Interests"
193
  )
194
- save_button = gr.Button("Save Preferences")
195
- save_status = gr.Textbox(label="Status")
196
-
197
- def save_preferences(name, selected_interests):
198
- if not name or not selected_interests:
199
- return "Name and interests are required!"
200
- preferences = {"name": name, "interests": selected_interests}
201
- try:
202
- os.makedirs("user_preferences", exist_ok=True)
203
- with open(f"user_preferences/preferences_{name}.json", "w") as f:
204
- json.dump(preferences, f)
205
- return "Preferences saved successfully!"
206
- except Exception as e:
207
- logging.error(f"Failed to save preferences: {e}")
208
- return "Failed to save preferences."
209
-
210
- save_button.click(save_preferences, inputs=[name_input, interests], outputs=save_status)
211
-
212
- with gr.Tab("Get News Summary"):
213
- name_input_summary = gr.Textbox(label="Your Name")
214
- fetch_button = gr.Button("Get Summary")
215
- summary_output = gr.Textbox(label="News Summary", lines=20)
216
-
217
- fetch_button.click(generate_user_summary, inputs=[name_input_summary], outputs=summary_output)
218
 
219
  if __name__ == "__main__":
220
  demo.launch()
 
21
 
22
  # News sources
23
  NEWS_SOURCES = {
24
+ "Technology": {
25
  "TheNewYorkTimes": "https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml",
26
  "reutersagency": "https://www.reutersagency.com/feed/?best-topics=tech&post_type=best"
27
  },
 
118
  logging.error(f"Summarization failed: {e}")
119
  return "Summary unavailable."
120
 
121
+ def summarize_articles(articles, template_choice):
122
+ """Summarize the last 5 fetched articles using the selected template."""
123
  summaries = []
124
  for article in articles:
125
  try:
 
134
  summary = summarize_text(content)
135
 
136
  if summary:
137
+ if template_choice == "Default":
138
+ summaries.append(f"""
139
+ πŸ“° {title}
140
+
141
+ - πŸ“ Category: {category}
142
+ - πŸ’‘ Source: {source}
143
+ - ⏰ Published: {published}
144
+
145
+ πŸ“ƒ Summary:
146
+ {summary}
147
+ πŸ”— Read the full article here ({link})
148
+
149
+ ---
150
+ """)
151
+ elif template_choice == "Free":
152
+ summaries.append(f"""
153
+ **{title}**
154
+
155
+ *Category:* {category}
156
+ *Source:* {source}
157
+ *Published:* {published}
158
+
159
+ *Summary:*
160
+ {summary}
161
+ *Link:* [{link}]({link})
162
+
163
+ ---
164
+ """)
165
  except Exception as e:
166
  logging.error(f"Error summarizing article: {e}")
167
  continue
168
  return summaries
169
 
170
+ def generate_summary(selected_interests, template_choice):
171
+ """Generate a news summary based on user preferences."""
172
+ categories = selected_interests
 
 
 
 
 
 
 
 
 
 
173
  if not categories:
174
  return "No categories selected. Please update your preferences."
175
 
 
179
  return "No recent news found in your selected categories."
180
 
181
  # Summarize all articles
182
+ summaries = summarize_articles(articles, template_choice)
183
 
184
  # Combine and return summaries
185
  return "\n\n".join(summaries) if summaries else "No summaries available."
 
190
  with demo:
191
  gr.Markdown("# πŸ“° AI News Summarizer")
192
 
193
+ with gr.Row():
 
194
  interests = gr.CheckboxGroup(
195
  choices=list(NEWS_SOURCES.keys()),
196
  label="Select Your Interests"
197
  )
198
+ template_choice = gr.Radio(
199
+ choices=["Default", "Free"],
200
+ label="Choose Summary Template",
201
+ value="Default"
202
+ )
203
+
204
+ fetch_button = gr.Button("Get Summary")
205
+ summary_output = gr.Textbox(label="News Summary", lines=20)
206
+
207
+ fetch_button.click(generate_summary, inputs=[interests, template_choice], outputs=summary_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  if __name__ == "__main__":
210
  demo.launch()