acecalisto3 commited on
Commit
10aaa36
1 Parent(s): 4a949f6

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +59 -28
app2.py CHANGED
@@ -1,13 +1,14 @@
1
-
2
  import csv
3
  import logging
4
  import os
5
  from typing import List, Tuple
6
- import asyncio
 
7
  import datetime
8
  import hashlib
 
9
 
10
- import aiohttp
11
  import feedparser
12
  import gradio as gr
13
  from huggingface_hub import InferenceClient
@@ -53,7 +54,6 @@ async def create_db_engine(db_url):
53
  except SQLAlchemyError as e:
54
  logger.error(f"Database error: {e}")
55
  raise
56
-
57
  def sanitize_url(url: str) -> str:
58
  return url if validators.url(url) else None
59
 
@@ -148,20 +148,40 @@ def stop_monitoring(url: str):
148
  asyncio.create_task(cleanup_resources(url))
149
  del monitoring_tasks[url]
150
  return "Monitoring stopped"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  async def chatbot_response(message: str, history: List[Tuple[str, str]]):
153
  try:
154
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HUGGINGFACE_API_KEY)
155
  response = await client.text_generation(message, max_new_tokens=100)
156
 
157
- history.append({"role": "user", "content": message})
158
- history.append({"role": "assistant", "content": response[0]['generated_text']})
159
 
160
  return history, history
161
  except Exception as e:
162
  logger.error(f"Chatbot error: {e}")
163
- history.append({"role": "user", "content": message})
164
- history.append({"role": "assistant", "content": "Error: Could not get a response from the chatbot."})
165
  return history, history
166
 
167
  async def update_db_status(db_status):
@@ -175,7 +195,7 @@ async def update_db_status(db_status):
175
 
176
  async def update_feed_content(db_session):
177
  try:
178
- articles = db_session.query(Article).order_by(Article.timestamp.desc()).limit(20).all()
179
  feed = {
180
  'title': 'Website Changes Feed',
181
  'link': 'http://yourwebsite.com/feed',
@@ -192,6 +212,7 @@ async def update_feed_content(db_session):
192
  logger.error(f"Database error: {e}")
193
  return None
194
 
 
195
  async def periodic_update_with_error_handling(db_session):
196
  while True:
197
  try:
@@ -200,6 +221,7 @@ async def periodic_update_with_error_handling(db_session):
200
  except Exception as e:
201
  logger.error(f"Error in periodic update: {e}")
202
 
 
203
  async def main():
204
  global db_session
205
  try:
@@ -216,48 +238,56 @@ async def main():
216
 
217
  with gr.Row():
218
  with gr.Column():
219
- db_url = gr.Textbox(label="Database URL", value="sqlite:///monitoring.db")
220
- db_status = gr.Textbox(label="Database Status", interactive=False, value="Connected")
 
 
 
221
 
222
  with gr.Column():
223
  with gr.Tab("Configuration"):
224
- target_urls = gr.Textbox(label="Target URLs (comma-separated)", placeholder="https://example.com, https://another-site.com")
225
- storage_location = gr.Textbox(label="Storage Location (CSV file path)", placeholder="/path/to/your/file.csv")
 
 
 
 
 
226
  feed_rss_checkbox = gr.Checkbox(label="Enable RSS Feed")
227
  start_button = gr.Button("Start Monitoring")
228
  stop_button = gr.Button("Stop Monitoring")
229
- status_text = gr.Textbox(label="Status", interactive=False)
230
- history_text = gr.Textbox(label="History", lines=10, interactive=False)
 
 
 
231
 
232
  with gr.Tab("User-End View"):
233
  feed_content = gr.JSON(label="RSS Feed Content")
234
 
235
  with gr.Tab("Chatbot"):
236
- chatbot_interface = gr.Chatbot()
237
- message_input = gr.Textbox(placeholder="Type your message here...")
 
238
  send_button = gr.Button("Send")
239
 
240
  start_button.click(
241
  start_monitoring,
242
  inputs=[target_urls, storage_location, feed_rss_checkbox],
243
- outputs=status_text
244
- )
245
 
246
- stop_button.click(
247
- lambda url: stop_monitoring(url),
248
- inputs=target_urls,
249
- outputs=status_text
250
- )
251
 
252
  send_button.click(
253
  chatbot_response,
254
  inputs=[message_input, chatbot_interface],
255
- outputs=[chatbot_interface, message_input]
256
- )
257
 
258
  asyncio.create_task(periodic_update_with_error_handling(db_session))
259
  asyncio.create_task(update_db_status(db_status))
260
-
261
  try:
262
  await demo.launch()
263
  finally:
@@ -265,5 +295,6 @@ async def main():
265
  await db_session.close()
266
  engine.dispose()
267
 
 
268
  if __name__ == "__main__":
269
- asyncio.run(main())
 
1
+ import asyncio
2
  import csv
3
  import logging
4
  import os
5
  from typing import List, Tuple
6
+
7
+ import aiohttp
8
  import datetime
9
  import hashlib
10
+ from pathlib import Path
11
 
 
12
  import feedparser
13
  import gradio as gr
14
  from huggingface_hub import InferenceClient
 
54
  except SQLAlchemyError as e:
55
  logger.error(f"Database error: {e}")
56
  raise
 
57
  def sanitize_url(url: str) -> str:
58
  return url if validators.url(url) else None
59
 
 
148
  asyncio.create_task(cleanup_resources(url))
149
  del monitoring_tasks[url]
150
  return "Monitoring stopped"
151
+ def generate_rss_feed():
152
+ session = Session()
153
+ try:
154
+ articles = session.query(Article).order_by(
155
+ Article.timestamp.desc()).limit(20).all()
156
+ feed = feedparser.FeedParserDict()
157
+ feed['title'] = 'Website Changes Feed'
158
+ feed['link'] = 'http://yourwebsite.com/feed' # Replace if needed
159
+ feed['description'] = 'Feed of changes detected on monitored websites.'
160
+ feed['entries'] = [{
161
+ 'title': article.title,
162
+ 'link': article.url,
163
+ 'description': article.content,
164
+ 'published': article.timestamp
165
+ } for article in articles]
166
+ return feedparser.FeedGenerator().feed_from_dictionary(
167
+ feed).writeString('utf-8')
168
+ except SQLAlchemyError as e:
169
+ logger.error(f"Database error: {e}")
170
+ return None
171
+ finally:
172
+ session.close()
173
 
174
  async def chatbot_response(message: str, history: List[Tuple[str, str]]):
175
  try:
176
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HUGGINGFACE_API_KEY)
177
  response = await client.text_generation(message, max_new_tokens=100)
178
 
179
+ history.append((message, response[0]['generated_text']))
 
180
 
181
  return history, history
182
  except Exception as e:
183
  logger.error(f"Chatbot error: {e}")
184
+ history.append((message, "Error: Could not get a response from the chatbot."))
 
185
  return history, history
186
 
187
  async def update_db_status(db_status):
 
195
 
196
  async def update_feed_content(db_session):
197
  try:
198
+ articles = await db_session. query(Article).order_by(Article.timestamp.desc()).limit(20).all()
199
  feed = {
200
  'title': 'Website Changes Feed',
201
  'link': 'http://yourwebsite.com/feed',
 
212
  logger.error(f"Database error: {e}")
213
  return None
214
 
215
+
216
  async def periodic_update_with_error_handling(db_session):
217
  while True:
218
  try:
 
221
  except Exception as e:
222
  logger.error(f"Error in periodic update: {e}")
223
 
224
+
225
  async def main():
226
  global db_session
227
  try:
 
238
 
239
  with gr.Row():
240
  with gr.Column():
241
+ db_url = gr.Textbox(
242
+ label="Database URL", value="sqlite:///monitoring.db")
243
+ db_status = gr.Textbox(label="Database Status",
244
+ interactive=False,
245
+ value="Connected")
246
 
247
  with gr.Column():
248
  with gr.Tab("Configuration"):
249
+ target_urls = gr.Textbox(
250
+ label="Target URLs (comma-separated)",
251
+ placeholder=
252
+ "https://example.com, https://another-site.com")
253
+ storage_location = gr.Textbox(
254
+ label="Storage Location (CSV file path)",
255
+ placeholder="/path/to/your/file.csv")
256
  feed_rss_checkbox = gr.Checkbox(label="Enable RSS Feed")
257
  start_button = gr.Button("Start Monitoring")
258
  stop_button = gr.Button("Stop Monitoring")
259
+ status_text = gr.Textbox(label="Status",
260
+ interactive=False)
261
+ history_text = gr.Textbox(label="History",
262
+ lines=10,
263
+ interactive=False)
264
 
265
  with gr.Tab("User-End View"):
266
  feed_content = gr.JSON(label="RSS Feed Content")
267
 
268
  with gr.Tab("Chatbot"):
269
+ chatbot_interface = gr.Chatbot(type='messages')
270
+ message_input = gr.Textbox(
271
+ placeholder="Type your message here...")
272
  send_button = gr.Button("Send")
273
 
274
  start_button.click(
275
  start_monitoring,
276
  inputs=[target_urls, storage_location, feed_rss_checkbox],
277
+ outputs=status_text)
 
278
 
279
+ stop_button.click(lambda url: stop_monitoring(url),
280
+ inputs=target_urls,
281
+ outputs=status_text)
 
 
282
 
283
  send_button.click(
284
  chatbot_response,
285
  inputs=[message_input, chatbot_interface],
286
+ outputs=[chatbot_interface, message_input])
 
287
 
288
  asyncio.create_task(periodic_update_with_error_handling(db_session))
289
  asyncio.create_task(update_db_status(db_status))
290
+
291
  try:
292
  await demo.launch()
293
  finally:
 
295
  await db_session.close()
296
  engine.dispose()
297
 
298
+
299
  if __name__ == "__main__":
300
+ asyncio.run(main())