acecalisto3 commited on
Commit
c3f6567
1 Parent(s): cfd616d

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +30 -6
app2.py CHANGED
@@ -26,6 +26,9 @@ engine = None
26
  monitoring_task = None
27
  logger = logging.getLogger(__name__)
28
 
 
 
 
29
  # Function for dynamically setting the database connection
30
  async def set_db_connection(host: str, port: str, user: str, password: str, db_name: str):
31
  global db_session, engine
@@ -95,11 +98,19 @@ async def predict_website_traffic(url: str) -> Dict[str, Any]:
95
  model = LinearRegression()
96
  model.fit(X_train, y_train)
97
  y_pred = model.predict(X_test)
98
- return {'traffic': y_pred[0]}
99
  except Exception as e:
100
  logger.error(f"Error predicting website traffic: {e}")
101
  return {}
102
 
 
 
 
 
 
 
 
 
103
  # Main application that runs Gradio UI and background tasks
104
  async def main():
105
  global db_session, monitoring_task
@@ -128,15 +139,15 @@ async def main():
128
  stop_button = gr.Button("Stop Monitoring")
129
  with gr.Column():
130
  feed_content = gr.JSON(label="RSS Feed Content")
131
- chatbot_interface = gr.Chatbot(type='messages')
132
  message_input = gr.Textbox(placeholder="Type your message here...")
133
  send_button = gr.Button("Send")
134
  scrape_button = gr.Button("Scrape Website")
135
  analyze_button = gr.Button("Analyze Website Content")
136
  predict_button = gr.Button("Predict Website Traffic")
137
  scrape_output = gr.Textbox(label="Scraped Website Content", interactive=False)
138
- analyze_output = gr.JSON(label="Website Content Analysis", interactive=False)
139
- predict_output = gr.JSON(label="Website Traffic Prediction", interactive=False)
140
  # Define button actions
141
  async def on_start_click(target_urls_str: str, storage_loc: str, feed_enabled: bool, host: str, port: str, user: str, password: str, db_name: str):
142
  global monitoring_task
@@ -170,8 +181,21 @@ async def main():
170
  # Load and check database status when the UI is loaded
171
  demo.load(update_db_status, outputs=db_status_textbox)
172
  asyncio.create_task(periodic_update_with_error_handling())
173
- # Launch the Gradio demo
174
- await demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  # Launch the app using asyncio
177
  if __name__ == "__main__":
 
26
  monitoring_task = None
27
  logger = logging.getLogger(__name__)
28
 
29
+ # Configure logging
30
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
31
+
32
  # Function for dynamically setting the database connection
33
  async def set_db_connection(host: str, port: str, user: str, password: str, db_name: str):
34
  global db_session, engine
 
98
  model = LinearRegression()
99
  model.fit(X_train, y_train)
100
  y_pred = model.predict(X_test)
101
+ return {'traffic': y_pred}
102
  except Exception as e:
103
  logger.error(f"Error predicting website traffic: {e}")
104
  return {}
105
 
106
+ # Function to update database status
107
+ async def update_db_status():
108
+ try:
109
+ await db_session.execute("SELECT 1")
110
+ return "Connected"
111
+ except SQLAlchemyError:
112
+ return "Disconnected"
113
+
114
  # Main application that runs Gradio UI and background tasks
115
  async def main():
116
  global db_session, monitoring_task
 
139
  stop_button = gr.Button("Stop Monitoring")
140
  with gr.Column():
141
  feed_content = gr.JSON(label="RSS Feed Content")
142
+ chatbot_interface = gr.Chatbot(type='messages', stateful=True) # Enable session state
143
  message_input = gr.Textbox(placeholder="Type your message here...")
144
  send_button = gr.Button("Send")
145
  scrape_button = gr.Button("Scrape Website")
146
  analyze_button = gr.Button("Analyze Website Content")
147
  predict_button = gr.Button("Predict Website Traffic")
148
  scrape_output = gr.Textbox(label="Scraped Website Content", interactive=False)
149
+ analyze_output = gr.JSON(label="Website Content Analysis") # Removed interactive=False
150
+ predict_output = gr.JSON(label="Website Traffic Prediction") # Removed interactive=False
151
  # Define button actions
152
  async def on_start_click(target_urls_str: str, storage_loc: str, feed_enabled: bool, host: str, port: str, user: str, password: str, db_name: str):
153
  global monitoring_task
 
181
  # Load and check database status when the UI is loaded
182
  demo.load(update_db_status, outputs=db_status_textbox)
183
  asyncio.create_task(periodic_update_with_error_handling())
184
+ # Launch the Gradio demo with a custom theme
185
+ await demo.launch(theme="default", title="Website Monitor and Chatbot")
186
+
187
+ # Function to handle chatbot responses with session state
188
+ async def chatbot_response(message: str, chat_history: List[str]) -> List[str]:
189
+ chat_history = chat_history or []
190
+ response = f"Echo: {message}"
191
+ chat_history.append((message, response))
192
+ return chat_history, ""
193
+
194
+ # Function to start monitoring
195
+ async def start_monitoring(urls: List[str], storage_location: str, feed_enabled: bool):
196
+ # Logic to start monitoring URLs and optionally save to CSV or enable RSS
197
+ print(f"Starting monitoring for {urls}, saving to {storage_location}, RSS enabled: {feed_enabled}")
198
+ return
199
 
200
  # Launch the app using asyncio
201
  if __name__ == "__main__":