-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
146 lines (126 loc) · 4.58 KB
/
main.py
File metadata and controls
146 lines (126 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Zomato Review Analysis - Main Application
Serves both backend API and frontend static files.
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import uvicorn
from backend.routes.review_analysis import router as review_analysis_router
# Add the backend directory to Python path for imports
backend_path = Path(__file__).parent / "backend"
sys.path.append(str(backend_path))
# Load environment variables
load_dotenv()
# Create FastAPI application
app = FastAPI(
title="Zomato Review Analysis System",
description="Complete system with backend API and frontend interface",
version="2.0.0",
docs_url="/api/docs",
redoc_url="/api/redoc"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routers
app.include_router(review_analysis_router)
# Mount static files (frontend)
frontend_path = Path(__file__).parent / "frontend"
if frontend_path.exists():
app.mount("/static", StaticFiles(directory=str(frontend_path)), name="static")
@app.get("/")
async def serve_frontend():
"""Serve the frontend application."""
frontend_file = Path(__file__).parent / "frontend" / "index.html"
if frontend_file.exists():
return FileResponse(str(frontend_file))
else:
return {"message": "Frontend not found. Please ensure frontend files are in the frontend/ directory."}
@app.get("/api/")
async def api_root():
"""API root endpoint with information."""
return {
"message": "Zomato Review Analysis API",
"version": "2.0.0",
"description": "Complete system with backend API and frontend interface",
"docs": "/api/docs",
"health": "/api/v1/health",
"main_route": "/api/v1/review",
"agent_status": "/api/v1/agent-status",
"frontend": "/"
}
@app.get("/api/health")
async def health():
"""Simple health check endpoint."""
return {"status": "healthy", "service": "zomato-review-analysis-system"}
@app.get("/styles.css")
async def serve_css():
"""Serve CSS file."""
css_file = Path(__file__).parent / "frontend" / "styles.css"
if css_file.exists():
return FileResponse(str(css_file), media_type="text/css")
else:
return {"error": "CSS file not found"}
@app.get("/script.js")
async def serve_js():
"""Serve JavaScript file."""
js_file = Path(__file__).parent / "frontend" / "script.js"
if js_file.exists():
return FileResponse(str(js_file), media_type="application/javascript")
else:
return {"error": "JavaScript file not found"}
def check_environment():
"""Check if the environment is properly set up."""
if not os.getenv('OPENAI_API_KEY'):
print("ERROR: OPENAI_API_KEY not found in environment variables.")
print("Please create a .env file with your OpenAI API key:")
print("OPENAI_API_KEY=your_api_key_here")
return False
db_path = Path(__file__).parent / "backend" / "zomato_reviews.sqlite"
if not db_path.exists():
print("ERROR: zomato_reviews.sqlite not found in backend directory.")
print("Please ensure the database file exists in the backend/ directory.")
return False
return True
def main():
"""
Main function to run the complete application.
"""
print("ZOMATO REVIEW ANALYSIS SYSTEM")
print("=" * 50)
print("Complete system with backend API and frontend interface")
print("Agent-driven analysis with intelligent tool orchestration")
print("Processes data from T-30 to Target date T")
print("LLM-based topic generation and consolidation")
print("Iterative analysis with confidence-based decisions")
# Check environment
if not check_environment():
print("Environment check failed. Please fix the issues above.")
sys.exit(1)
print("Environment check passed")
print("Frontend available at: http://localhost:8000/")
print("API Documentation available at: http://localhost:8000/api/docs")
print("Main analysis route: http://localhost:8000/api/v1/review")
print("Agent status: http://localhost:8000/api/v1/agent-status")
print("=" * 50)
# Run the application
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=False,
log_level="info"
)
if __name__ == "__main__":
main()