acecalisto3 commited on
Commit
b3b63d8
1 Parent(s): 78e55e5

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +20 -12
app2.py CHANGED
@@ -5,7 +5,6 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
5
  from sqlalchemy.future import select # Correct async query API
6
  from sqlalchemy.orm import sessionmaker
7
  import logging
8
-
9
  import os
10
  import sys
11
 
@@ -15,6 +14,11 @@ sys.path.insert(0, parent_dir)
15
 
16
  logger = logging.getLogger(__name__)
17
 
 
 
 
 
 
18
  # This will constantly check the database status and update the textbox
19
  async def update_db_status(db_status_textbox):
20
  while True:
@@ -74,7 +78,7 @@ async def set_db_connection(host, port, user, password, db_name):
74
 
75
  # Main application that runs Gradio UI and background tasks
76
  async def main():
77
- global db_session
78
  engine = None
79
 
80
  demo = gr.Blocks()
@@ -117,14 +121,24 @@ async def main():
117
  message_input = gr.Textbox(placeholder="Type your message here...")
118
  send_button = gr.Button("Send")
119
 
120
- # Define button actions
121
  async def on_start_click(target_urls_str: str, storage_loc: str, feed_enabled: bool,
122
  host: str, port: str, user: str, password: str, db_name: str):
 
123
  urls = [url.strip() for url in target_urls_str.split(",")]
124
  await set_db_connection(host, port, user, password, db_name) # Connect to the DB
125
- asyncio.create_task(start_monitoring(urls, storage_loc, feed_enabled)) # Start monitoring
126
  return "Monitoring started."
127
 
 
 
 
 
 
 
 
 
 
128
  async def on_view_feed_click(feed_url: str):
129
  # Logic to fetch and view RSS feed data based on URL
130
  return await fetch_feed_content(feed_url)
@@ -148,9 +162,8 @@ async def main():
148
  # Launch the Gradio demo
149
  await demo.launch()
150
 
151
- async def fetch_feed_content(feed_url: str):
152
  # Logic to fetch RSS feed content from the provided URL
153
- # You would replace this with actual RSS fetching and parsing logic
154
  return {
155
  'title': 'Sample Feed',
156
  'link': feed_url,
@@ -165,17 +178,12 @@ async def start_monitoring(urls, storage_location, feed_enabled):
165
  print(f"Starting monitoring for {urls}, saving to {storage_location}, RSS enabled: {feed_enabled}")
166
  return
167
 
168
- def stop_monitoring(url):
169
- # Logic to stop monitoring a specific URL
170
- print(f"Stopping monitoring for {url}")
171
- return
172
-
173
  async def chatbot_response(message, chat_interface):
174
  # Example chatbot logic to respond to a user message
175
  response = f"Echo: {message}"
176
  chat_interface.append((message, response))
177
  return chat_interface, ""
178
 
179
- # Launch the app using asyncio
180
  if __name__ == "__main__":
181
  asyncio.run(main())
 
5
  from sqlalchemy.future import select # Correct async query API
6
  from sqlalchemy.orm import sessionmaker
7
  import logging
 
8
  import os
9
  import sys
10
 
 
14
 
15
  logger = logging.getLogger(__name__)
16
 
17
+ # Global variables for database session and engine
18
+ db_session = None
19
+ engine = None
20
+ monitoring_task = None # Global variable to track the monitoring task
21
+
22
  # This will constantly check the database status and update the textbox
23
  async def update_db_status(db_status_textbox):
24
  while True:
 
78
 
79
  # Main application that runs Gradio UI and background tasks
80
  async def main():
81
+ global db_session, monitoring_task
82
  engine = None
83
 
84
  demo = gr.Blocks()
 
121
  message_input = gr.Textbox(placeholder="Type your message here...")
122
  send_button = gr.Button("Send")
123
 
124
+ # Define button actions
125
  async def on_start_click(target_urls_str: str, storage_loc: str, feed_enabled: bool,
126
  host: str, port: str, user: str, password: str, db_name: str):
127
+ global monitoring_task
128
  urls = [url.strip() for url in target_urls_str.split(",")]
129
  await set_db_connection(host, port, user, password, db_name) # Connect to the DB
130
+ monitoring_task = asyncio.create_task(start_monitoring(urls, storage_loc, feed_enabled)) # Start monitoring
131
  return "Monitoring started."
132
 
133
+ async def on_stop_click(): # Define the on_stop_click function
134
+ global monitoring_task
135
+ if monitoring_task:
136
+ monitoring_task.cancel() # Cancel the monitoring task
137
+ monitoring_task = None
138
+ return "Monitoring stopped."
139
+ else:
140
+ return "Monitoring is not running."
141
+
142
  async def on_view_feed_click(feed_url: str):
143
  # Logic to fetch and view RSS feed data based on URL
144
  return await fetch_feed_content(feed_url)
 
162
  # Launch the Gradio demo
163
  await demo.launch()
164
 
165
+ async def fetch_feed_content(feed_url: str):
166
  # Logic to fetch RSS feed content from the provided URL
 
167
  return {
168
  'title': 'Sample Feed',
169
  'link': feed_url,
 
178
  print(f"Starting monitoring for {urls}, saving to {storage_location}, RSS enabled: {feed_enabled}")
179
  return
180
 
 
 
 
 
 
181
  async def chatbot_response(message, chat_interface):
182
  # Example chatbot logic to respond to a user message
183
  response = f"Echo: {message}"
184
  chat_interface.append((message, response))
185
  return chat_interface, ""
186
 
187
+ # Launch the app using asyncio
188
  if __name__ == "__main__":
189
  asyncio.run(main())