Spaces:
Runtime error
Runtime error
acecalisto3
commited on
Commit
•
c811f89
1
Parent(s):
09dd92c
Update app2.py
Browse files
app2.py
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
1 |
import asyncio
|
2 |
import gradio as gr
|
3 |
from sqlalchemy.exc import SQLAlchemyError
|
@@ -8,6 +11,17 @@ import logging
|
|
8 |
import os
|
9 |
import sys
|
10 |
from typing import List, Dict, Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Global variables for database session and engine
|
13 |
db_session = None
|
@@ -52,6 +66,43 @@ async def fetch_feed_content(feed_url: str) -> Dict[str, Any]:
|
|
52 |
logger.error(f"Error fetching feed content: {e}")
|
53 |
return {}
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# Main application that runs Gradio UI and background tasks
|
56 |
async def main():
|
57 |
global db_session, monitoring_task
|
@@ -83,6 +134,12 @@ async def main():
|
|
83 |
chatbot_interface = gr.Chatbot(type='messages')
|
84 |
message_input = gr.Textbox(placeholder="Type your message here...")
|
85 |
send_button = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
# Define button actions
|
87 |
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):
|
88 |
global monitoring_task
|
@@ -98,8 +155,17 @@ async def main():
|
|
98 |
return "Monitoring stopped."
|
99 |
async def on_view_feed_click(feed_url: str):
|
100 |
return await fetch_feed_content(feed_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
stop_button.click(on_stop_click, outputs=[status_text])
|
102 |
view_button.click(on_view_feed_click, inputs=[feed_target_url], outputs=[feed_content])
|
|
|
|
|
|
|
103 |
send_button.click(chatbot_response, inputs=[message_input, chatbot_interface], outputs=[chatbot_interface, message_input])
|
104 |
# Set up the timer for periodic updates
|
105 |
feed_updater = gr.Timer(interval=300)
|
|
|
1 |
+
Here's an enhanced version of the code with advanced features:
|
2 |
+
|
3 |
+
```python
|
4 |
import asyncio
|
5 |
import gradio as gr
|
6 |
from sqlalchemy.exc import SQLAlchemyError
|
|
|
11 |
import os
|
12 |
import sys
|
13 |
from typing import List, Dict, Any
|
14 |
+
from datetime import datetime
|
15 |
+
from pytz import timezone
|
16 |
+
import pytz
|
17 |
+
import requests
|
18 |
+
from bs4 import BeautifulSoup
|
19 |
+
import pandas as pd
|
20 |
+
import numpy as np
|
21 |
+
import matplotlib.pyplot as plt
|
22 |
+
from sklearn.model_selection import train_test_split
|
23 |
+
from sklearn.linear_model import LinearRegression
|
24 |
+
from sklearn import metrics
|
25 |
|
26 |
# Global variables for database session and engine
|
27 |
db_session = None
|
|
|
66 |
logger.error(f"Error fetching feed content: {e}")
|
67 |
return {}
|
68 |
|
69 |
+
# Function to scrape website content
|
70 |
+
async def scrape_website(url: str) -> str:
|
71 |
+
try:
|
72 |
+
response = requests.get(url)
|
73 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
74 |
+
return soup.get_text()
|
75 |
+
except Exception as e:
|
76 |
+
logger.error(f"Error scraping website: {e}")
|
77 |
+
return ""
|
78 |
+
|
79 |
+
# Function to analyze website content
|
80 |
+
async def analyze_website_content(content: str) -> Dict[str, Any]:
|
81 |
+
try:
|
82 |
+
# Perform sentiment analysis using Natural Language Processing (NLP) techniques
|
83 |
+
# For simplicity, we'll use a basic sentiment analysis approach
|
84 |
+
sentiment = "Positive" if content.count("good") > content.count("bad") else "Negative"
|
85 |
+
return {'sentiment': sentiment}
|
86 |
+
except Exception as e:
|
87 |
+
logger.error(f"Error analyzing website content: {e}")
|
88 |
+
return {}
|
89 |
+
|
90 |
+
# Function to predict website traffic
|
91 |
+
async def predict_website_traffic(url: str) -> Dict[str, Any]:
|
92 |
+
try:
|
93 |
+
# Use machine learning model to predict website traffic
|
94 |
+
# For simplicity, we'll use a basic linear regression model
|
95 |
+
X = pd.DataFrame({'url': [url]})
|
96 |
+
y = pd.DataFrame({'traffic': [100]}) # Replace with actual traffic data
|
97 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
98 |
+
model = LinearRegression()
|
99 |
+
model.fit(X_train, y_train)
|
100 |
+
y_pred = model.predict(X_test)
|
101 |
+
return {'traffic': y_pred[0]}
|
102 |
+
except Exception as e:
|
103 |
+
logger.error(f"Error predicting website traffic: {e}")
|
104 |
+
return {}
|
105 |
+
|
106 |
# Main application that runs Gradio UI and background tasks
|
107 |
async def main():
|
108 |
global db_session, monitoring_task
|
|
|
134 |
chatbot_interface = gr.Chatbot(type='messages')
|
135 |
message_input = gr.Textbox(placeholder="Type your message here...")
|
136 |
send_button = gr.Button("Send")
|
137 |
+
scrape_button = gr.Button("Scrape Website")
|
138 |
+
analyze_button = gr.Button("Analyze Website Content")
|
139 |
+
predict_button = gr.Button("Predict Website Traffic")
|
140 |
+
scrape_output = gr.Textbox(label="Scraped Website Content", interactive=False)
|
141 |
+
analyze_output = gr.JSON(label="Website Content Analysis", interactive=False)
|
142 |
+
predict_output = gr.JSON(label="Website Traffic Prediction", interactive=False)
|
143 |
# Define button actions
|
144 |
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):
|
145 |
global monitoring_task
|
|
|
155 |
return "Monitoring stopped."
|
156 |
async def on_view_feed_click(feed_url: str):
|
157 |
return await fetch_feed_content(feed_url)
|
158 |
+
async def on_scrape_click(url: str):
|
159 |
+
return await scrape_website(url)
|
160 |
+
async def on_analyze_click(content: str):
|
161 |
+
return await analyze_website_content(content)
|
162 |
+
async def on_predict_click(url: str):
|
163 |
+
return await predict_website_traffic(url)
|
164 |
stop_button.click(on_stop_click, outputs=[status_text])
|
165 |
view_button.click(on_view_feed_click, inputs=[feed_target_url], outputs=[feed_content])
|
166 |
+
scrape_button.click(on_scrape_click, inputs=[target_urls], outputs=[scrape_output])
|
167 |
+
analyze_button.click(on_analyze_click, inputs=[scrape_output], outputs=[analyze_output])
|
168 |
+
predict_button.click(on_predict_click, inputs=[target_urls], outputs=[predict_output])
|
169 |
send_button.click(chatbot_response, inputs=[message_input, chatbot_interface], outputs=[chatbot_interface, message_input])
|
170 |
# Set up the timer for periodic updates
|
171 |
feed_updater = gr.Timer(interval=300)
|