Spaces:
Runtime error
Runtime error
acecalisto3
commited on
Commit
•
2ec849e
1
Parent(s):
7051b81
Update app2.py
Browse files
app2.py
CHANGED
@@ -24,6 +24,11 @@ class Settings(BaseSettings):
|
|
24 |
db_user: str
|
25 |
db_password: str
|
26 |
db_name: str
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
class Config:
|
29 |
# Optional: if you want to allow extra fields
|
@@ -33,10 +38,10 @@ settings = Settings()
|
|
33 |
|
34 |
# --- Database Connection ---
|
35 |
def get_db_url(settings: Settings) -> str:
|
36 |
-
if settings.
|
37 |
-
return f"mysql+aiomysql://{settings.
|
38 |
-
elif settings.
|
39 |
-
return f"postgresql+asyncpg://{settings.
|
40 |
else:
|
41 |
return "sqlite+aiosqlite:///default.db"
|
42 |
|
@@ -52,17 +57,17 @@ async def set_db_connection(
|
|
52 |
try:
|
53 |
# Update settings if new values provided
|
54 |
if db_type:
|
55 |
-
settings.
|
56 |
if db_host:
|
57 |
-
settings.
|
58 |
if db_port:
|
59 |
-
settings.
|
60 |
if db_user:
|
61 |
-
settings.
|
62 |
if db_password:
|
63 |
-
settings.
|
64 |
if db_name:
|
65 |
-
settings.
|
66 |
|
67 |
# Close existing connection if any
|
68 |
if db_session:
|
@@ -101,7 +106,7 @@ async def set_db_connection():
|
|
101 |
global db_session, engine
|
102 |
try:
|
103 |
engine = create_async_engine(
|
104 |
-
f"mysql+aiomysql://{settings.
|
105 |
echo=False,
|
106 |
)
|
107 |
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
@@ -142,10 +147,21 @@ async def store_data(url: str, content: str, analysis: Dict[str, Any]):
|
|
142 |
|
143 |
async def update_feed_content():
|
144 |
try:
|
145 |
-
#
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
except Exception as e:
|
148 |
logger.error(f"Error updating feed content: {e}")
|
|
|
149 |
|
150 |
# --- Website Scraping and Analysis ---
|
151 |
async def scrape_website(url: str) -> str:
|
@@ -179,13 +195,13 @@ async def predict_website_traffic(url: str) -> Dict[str, Any]:
|
|
179 |
# --- Chatbot Integration ---
|
180 |
async def chatbot_response(message: str, chat_history: List[Dict[str, str]]) -> List[Dict[str, str]]:
|
181 |
try:
|
182 |
-
if not settings.
|
183 |
raise ValueError("Chatbot API key is not set.")
|
184 |
# ... (Your code to call the chatbot API) ...
|
185 |
# Example using a hypothetical API:
|
186 |
response = requests.post(
|
187 |
"https://your-chatbot-api.com/chat",
|
188 |
-
json={"message": message, "api_key": settings.
|
189 |
)
|
190 |
response.raise_for_status()
|
191 |
bot_response = response.json()["response"]
|
@@ -219,15 +235,12 @@ async def main():
|
|
219 |
with gr.Row():
|
220 |
with gr.Column():
|
221 |
gr.Markdown("## Database Settings")
|
222 |
-
# ... (Database settings are now configured in .env file) ...
|
223 |
db_status_textbox = gr.Textbox(label="Database Status", interactive=False)
|
224 |
status_text = gr.Textbox(label="Status", interactive=False)
|
225 |
|
226 |
gr.Markdown("## RSS Feed Reader Settings")
|
227 |
-
# ... (RSS feed settings are now configured in .env file) ...
|
228 |
view_button = gr.Button("View Feed")
|
229 |
target_urls = gr.Textbox(label="Target URLs (comma-separated)", placeholder="https://example.com, https://another-site.com")
|
230 |
-
# ... (Storage location is now configured in .env file) ...
|
231 |
feed_rss_checkbox = gr.Checkbox(label="Enable RSS Feed")
|
232 |
start_button = gr.Button("Start Monitoring")
|
233 |
stop_button = gr.Button("Stop Monitoring")
|
@@ -247,7 +260,7 @@ async def main():
|
|
247 |
# --- Button Actions ---
|
248 |
start_button.click(
|
249 |
fn=on_start_click,
|
250 |
-
inputs=[target_urls, feed_rss_checkbox],
|
251 |
outputs=[status_text],
|
252 |
)
|
253 |
|
@@ -255,7 +268,7 @@ async def main():
|
|
255 |
|
256 |
view_button.click(
|
257 |
fn=on_view_feed_click,
|
258 |
-
inputs=[],
|
259 |
outputs=[feed_content],
|
260 |
)
|
261 |
|
@@ -284,8 +297,12 @@ async def main():
|
|
284 |
)
|
285 |
|
286 |
# --- Periodic Updates ---
|
287 |
-
|
288 |
-
|
|
|
|
|
|
|
|
|
289 |
|
290 |
# --- Load Database Status ---
|
291 |
demo.load(fn=update_db_status, outputs=[db_status_textbox])
|
@@ -299,7 +316,7 @@ async def on_start_click(target_urls_str: str, feed_enabled: bool):
|
|
299 |
global monitoring_task
|
300 |
urls = [url.strip() for url in target_urls_str.split(",")]
|
301 |
await set_db_connection()
|
302 |
-
monitoring_task = asyncio.create_task(start_monitoring(urls, settings.
|
303 |
return "Monitoring started."
|
304 |
|
305 |
async def on_stop_click():
|
@@ -310,7 +327,7 @@ async def on_stop_click():
|
|
310 |
return "Monitoring stopped."
|
311 |
|
312 |
async def on_view_feed_click():
|
313 |
-
return await
|
314 |
|
315 |
async def on_scrape_click(url: str):
|
316 |
return await scrape_website(url)
|
|
|
24 |
db_user: str
|
25 |
db_password: str
|
26 |
db_name: str
|
27 |
+
database_type: str
|
28 |
+
database_port: int
|
29 |
+
chatbot_api_key: str
|
30 |
+
rss_feed_url: str
|
31 |
+
storage_location: str
|
32 |
|
33 |
class Config:
|
34 |
# Optional: if you want to allow extra fields
|
|
|
38 |
|
39 |
# --- Database Connection ---
|
40 |
def get_db_url(settings: Settings) -> str:
|
41 |
+
if settings.database_type == "mysql":
|
42 |
+
return f"mysql+aiomysql://{settings.db_user}:{settings.db_password}@{settings.db_host}:{settings.database_port}/{settings.db_name}"
|
43 |
+
elif settings.database_type == "postgresql":
|
44 |
+
return f"postgresql+asyncpg://{settings.db_user}:{settings.db_password}@{settings.db_host}:{settings.database_port}/{settings.db_name}"
|
45 |
else:
|
46 |
return "sqlite+aiosqlite:///default.db"
|
47 |
|
|
|
57 |
try:
|
58 |
# Update settings if new values provided
|
59 |
if db_type:
|
60 |
+
settings.database_type = db_type
|
61 |
if db_host:
|
62 |
+
settings.db_host = db_host
|
63 |
if db_port:
|
64 |
+
settings.database_port = db_port
|
65 |
if db_user:
|
66 |
+
settings.db_user = db_user
|
67 |
if db_password:
|
68 |
+
settings.db_password = db_password
|
69 |
if db_name:
|
70 |
+
settings.db_name = db_name
|
71 |
|
72 |
# Close existing connection if any
|
73 |
if db_session:
|
|
|
106 |
global db_session, engine
|
107 |
try:
|
108 |
engine = create_async_engine(
|
109 |
+
f"mysql+aiomysql://{settings.db_user}:{settings.db_password}@{settings.db_host}:{settings.database_port}/{settings.db_name}",
|
110 |
echo=False,
|
111 |
)
|
112 |
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
147 |
|
148 |
async def update_feed_content():
|
149 |
try:
|
150 |
+
# Fetch RSS feed content from database
|
151 |
+
async with db_session as session:
|
152 |
+
articles = await session.execute(select(Article))
|
153 |
+
feed_content = []
|
154 |
+
for article in articles.scalars():
|
155 |
+
feed_content.append({
|
156 |
+
"title": article.title,
|
157 |
+
"url": article.url,
|
158 |
+
"content": article.content,
|
159 |
+
"timestamp": article.timestamp,
|
160 |
+
})
|
161 |
+
return feed_content
|
162 |
except Exception as e:
|
163 |
logger.error(f"Error updating feed content: {e}")
|
164 |
+
return []
|
165 |
|
166 |
# --- Website Scraping and Analysis ---
|
167 |
async def scrape_website(url: str) -> str:
|
|
|
195 |
# --- Chatbot Integration ---
|
196 |
async def chatbot_response(message: str, chat_history: List[Dict[str, str]]) -> List[Dict[str, str]]:
|
197 |
try:
|
198 |
+
if not settings.chatbot_api_key:
|
199 |
raise ValueError("Chatbot API key is not set.")
|
200 |
# ... (Your code to call the chatbot API) ...
|
201 |
# Example using a hypothetical API:
|
202 |
response = requests.post(
|
203 |
"https://your-chatbot-api.com/chat",
|
204 |
+
json={"message": message, "api_key": settings.chatbot_api_key},
|
205 |
)
|
206 |
response.raise_for_status()
|
207 |
bot_response = response.json()["response"]
|
|
|
235 |
with gr.Row():
|
236 |
with gr.Column():
|
237 |
gr.Markdown("## Database Settings")
|
|
|
238 |
db_status_textbox = gr.Textbox(label="Database Status", interactive=False)
|
239 |
status_text = gr.Textbox(label="Status", interactive=False)
|
240 |
|
241 |
gr.Markdown("## RSS Feed Reader Settings")
|
|
|
242 |
view_button = gr.Button("View Feed")
|
243 |
target_urls = gr.Textbox(label="Target URLs (comma-separated)", placeholder="https://example.com, https://another-site.com")
|
|
|
244 |
feed_rss_checkbox = gr.Checkbox(label="Enable RSS Feed")
|
245 |
start_button = gr.Button("Start Monitoring")
|
246 |
stop_button = gr.Button("Stop Monitoring")
|
|
|
260 |
# --- Button Actions ---
|
261 |
start_button.click(
|
262 |
fn=on_start_click,
|
263 |
+
inputs=[target_urls, feed_rss_checkbox],
|
264 |
outputs=[status_text],
|
265 |
)
|
266 |
|
|
|
268 |
|
269 |
view_button.click(
|
270 |
fn=on_view_feed_click,
|
271 |
+
inputs=[],
|
272 |
outputs=[feed_content],
|
273 |
)
|
274 |
|
|
|
297 |
)
|
298 |
|
299 |
# --- Periodic Updates ---
|
300 |
+
async def update_feed_periodically(feed_content):
|
301 |
+
while True:
|
302 |
+
await update_feed_content()
|
303 |
+
await asyncio.sleep(300) # Check every 5 minutes
|
304 |
+
|
305 |
+
feed_updater = asyncio.create_task(update_feed_periodically(feed_content))
|
306 |
|
307 |
# --- Load Database Status ---
|
308 |
demo.load(fn=update_db_status, outputs=[db_status_textbox])
|
|
|
316 |
global monitoring_task
|
317 |
urls = [url.strip() for url in target_urls_str.split(",")]
|
318 |
await set_db_connection()
|
319 |
+
monitoring_task = asyncio.create_task(start_monitoring(urls, settings.storage_location, feed_enabled))
|
320 |
return "Monitoring started."
|
321 |
|
322 |
async def on_stop_click():
|
|
|
327 |
return "Monitoring stopped."
|
328 |
|
329 |
async def on_view_feed_click():
|
330 |
+
return await update_feed_content()
|
331 |
|
332 |
async def on_scrape_click(url: str):
|
333 |
return await scrape_website(url)
|