Add startup status display and enhance server initialization in app.py
Browse files- app.py +72 -2
- requirements.txt +1 -1
app.py
CHANGED
@@ -19,6 +19,14 @@ from utils import ChatAnalyzer, setup_chat_analysis
|
|
19 |
import requests.exceptions
|
20 |
import aiohttp
|
21 |
from typing import Union
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Initialize environment variables
|
24 |
load_dotenv()
|
@@ -305,6 +313,68 @@ async def check_directory_status():
|
|
305 |
}
|
306 |
}
|
307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
308 |
if __name__ == "__main__":
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
import requests.exceptions
|
20 |
import aiohttp
|
21 |
from typing import Union
|
22 |
+
import uvicorn
|
23 |
+
import logging
|
24 |
+
from rich import print as rprint
|
25 |
+
from rich.console import Console
|
26 |
+
from rich.panel import Panel
|
27 |
+
from rich.table import Table
|
28 |
+
|
29 |
+
console = Console()
|
30 |
|
31 |
# Initialize environment variables
|
32 |
load_dotenv()
|
|
|
313 |
}
|
314 |
}
|
315 |
|
316 |
+
# Добавим функцию для вывода статуса
|
317 |
+
def print_startup_status():
|
318 |
+
"""Print application startup status with rich formatting"""
|
319 |
+
try:
|
320 |
+
# Create status table
|
321 |
+
table = Table(show_header=True, header_style="bold magenta")
|
322 |
+
table.add_column("Component", style="cyan")
|
323 |
+
table.add_column("Status", style="green")
|
324 |
+
|
325 |
+
# Check directories
|
326 |
+
vector_store_exists = os.path.exists(VECTOR_STORE_PATH)
|
327 |
+
chat_history_exists = os.path.exists(CHAT_HISTORY_PATH)
|
328 |
+
|
329 |
+
table.add_row(
|
330 |
+
"Vector Store Directory",
|
331 |
+
"✅ Created" if vector_store_exists else "❌ Missing"
|
332 |
+
)
|
333 |
+
table.add_row(
|
334 |
+
"Chat History Directory",
|
335 |
+
"✅ Created" if chat_history_exists else "❌ Missing"
|
336 |
+
)
|
337 |
+
|
338 |
+
# Check environment variables
|
339 |
+
table.add_row(
|
340 |
+
"GROQ API Key",
|
341 |
+
"✅ Set" if os.getenv("GROQ_API_KEY") else "❌ Missing"
|
342 |
+
)
|
343 |
+
|
344 |
+
# Create status panel
|
345 |
+
status_panel = Panel(
|
346 |
+
table,
|
347 |
+
title="[bold blue]Status Law Assistant API Status[/bold blue]",
|
348 |
+
border_style="blue"
|
349 |
+
)
|
350 |
+
|
351 |
+
# Print startup message and status
|
352 |
+
console.print("\n")
|
353 |
+
console.print("[bold green]🚀 Server started successfully![/bold green]")
|
354 |
+
console.print(status_panel)
|
355 |
+
console.print("\n[bold yellow]API Documentation:[/bold yellow]")
|
356 |
+
console.print("📚 Swagger UI: http://0.0.0.0:8000/docs")
|
357 |
+
console.print("📘 ReDoc: http://0.0.0.0:8000/redoc\n")
|
358 |
+
|
359 |
+
except Exception as e:
|
360 |
+
console.print(f"[bold red]Error printing status: {str(e)}[/bold red]")
|
361 |
+
|
362 |
if __name__ == "__main__":
|
363 |
+
config = uvicorn.Config(
|
364 |
+
"app:app",
|
365 |
+
host="0.0.0.0",
|
366 |
+
port=8000,
|
367 |
+
log_level="info",
|
368 |
+
reload=True
|
369 |
+
)
|
370 |
+
server = uvicorn.Server(config)
|
371 |
+
|
372 |
+
try:
|
373 |
+
# Start the server
|
374 |
+
console.print("[bold yellow]Starting Status Law Assistant API...[/bold yellow]")
|
375 |
+
server.run()
|
376 |
+
except Exception as e:
|
377 |
+
console.print(f"[bold red]Server failed to start: {str(e)}[/bold red]")
|
378 |
+
finally:
|
379 |
+
# Print startup status after uvicorn starts
|
380 |
+
print_startup_status()
|
requirements.txt
CHANGED
@@ -21,5 +21,5 @@ pytest-asyncio
|
|
21 |
aiohttp
|
22 |
requests
|
23 |
tenacity
|
24 |
-
|
25 |
|
|
|
21 |
aiohttp
|
22 |
requests
|
23 |
tenacity
|
24 |
+
rich>=10.0.0
|
25 |
|