Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- modal_backend.py +112 -79
- src/ui/gradio_app.py +167 -91
modal_backend.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
"""
|
| 2 |
Modal Backend for Restaurant Intelligence Agent
|
| 3 |
-
With
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
import modal
|
|
@@ -29,36 +32,42 @@ image = (
|
|
| 29 |
"python-dotenv",
|
| 30 |
"matplotlib",
|
| 31 |
"fastapi[standard]",
|
| 32 |
-
"
|
| 33 |
-
"reportlab", # For PDF generation
|
| 34 |
)
|
| 35 |
.add_local_python_source("src")
|
| 36 |
)
|
| 37 |
|
| 38 |
|
| 39 |
# ============================================================================
|
| 40 |
-
#
|
| 41 |
# ============================================================================
|
| 42 |
|
| 43 |
-
def
|
| 44 |
-
"""
|
| 45 |
-
if not
|
| 46 |
-
return
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
# ============================================================================
|
| 59 |
# MCP SERVER (TRUE MCP INTEGRATION)
|
| 60 |
# ============================================================================
|
| 61 |
|
|
|
|
| 62 |
REVIEW_INDEX: Dict[str, List[str]] = {}
|
| 63 |
ANALYSIS_CACHE: Dict[str, Dict[str, Any]] = {}
|
| 64 |
|
|
@@ -66,7 +75,9 @@ ANALYSIS_CACHE: Dict[str, Dict[str, Any]] = {}
|
|
| 66 |
@app.function(image=image, timeout=300)
|
| 67 |
@modal.asgi_app()
|
| 68 |
def mcp_server():
|
| 69 |
-
"""
|
|
|
|
|
|
|
| 70 |
from fastapi import FastAPI, HTTPException
|
| 71 |
from pydantic import BaseModel
|
| 72 |
from datetime import datetime
|
|
@@ -86,6 +97,7 @@ def mcp_server():
|
|
| 86 |
question: str
|
| 87 |
top_k: int = 5
|
| 88 |
|
|
|
|
| 89 |
def index_reviews(restaurant_name: str, reviews: List[str]) -> Dict[str, Any]:
|
| 90 |
REVIEW_INDEX[restaurant_name] = reviews
|
| 91 |
return {
|
|
@@ -112,6 +124,11 @@ def mcp_server():
|
|
| 112 |
"review_count": min(top_k, len(reviews))
|
| 113 |
}
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def list_tools() -> Dict[str, Any]:
|
| 116 |
return {
|
| 117 |
"success": True,
|
|
@@ -124,7 +141,7 @@ def mcp_server():
|
|
| 124 |
|
| 125 |
@mcp_api.get("/")
|
| 126 |
async def root():
|
| 127 |
-
return {"name": "Restaurant Intelligence MCP Server", "protocol": "MCP", "version": "
|
| 128 |
|
| 129 |
@mcp_api.get("/health")
|
| 130 |
async def health():
|
|
@@ -136,9 +153,11 @@ def mcp_server():
|
|
| 136 |
|
| 137 |
@mcp_api.post("/mcp/call")
|
| 138 |
async def call_tool(request: ToolRequest):
|
|
|
|
| 139 |
tool_map = {
|
| 140 |
"index_reviews": lambda args: index_reviews(args["restaurant_name"], args["reviews"]),
|
| 141 |
"query_reviews": lambda args: query_reviews(args["restaurant_name"], args["question"], args.get("top_k", 5)),
|
|
|
|
| 142 |
"list_tools": lambda args: list_tools()
|
| 143 |
}
|
| 144 |
|
|
@@ -151,31 +170,40 @@ def mcp_server():
|
|
| 151 |
except Exception as e:
|
| 152 |
raise HTTPException(status_code=500, detail=str(e))
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
return mcp_api
|
| 155 |
|
| 156 |
|
| 157 |
# ============================================================================
|
| 158 |
-
#
|
| 159 |
# ============================================================================
|
| 160 |
|
| 161 |
@app.function(image=image)
|
| 162 |
def hello() -> Dict[str, Any]:
|
| 163 |
-
return {"status": "Modal is working!", "mcp": "enabled"
|
| 164 |
|
| 165 |
|
| 166 |
-
@app.function(image=image, timeout=
|
| 167 |
def scrape_restaurant_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
| 168 |
-
"""Scrape reviews
|
| 169 |
-
platform = detect_platform(url)
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
from src.scrapers.opentable_scraper import scrape_opentable
|
| 173 |
result = scrape_opentable(url=url, max_reviews=max_reviews, headless=True)
|
| 174 |
-
elif
|
| 175 |
from src.scrapers.google_maps_scraper import scrape_google_maps
|
| 176 |
result = scrape_google_maps(url=url, max_reviews=max_reviews, headless=True)
|
| 177 |
else:
|
| 178 |
-
return {"success": False, "error":
|
| 179 |
|
| 180 |
if not result.get("success"):
|
| 181 |
return {"success": False, "error": result.get("error")}
|
|
@@ -185,24 +213,21 @@ def scrape_restaurant_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 185 |
df = process_reviews(result)
|
| 186 |
reviews = clean_reviews_for_ai(df["review_text"].tolist(), verbose=False)
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
|
| 190 |
for _, row in df.iterrows():
|
| 191 |
-
|
|
|
|
| 192 |
"date": str(row.get("date", "")),
|
| 193 |
"rating": float(row.get("overall_rating", 0) or 0),
|
| 194 |
-
"
|
| 195 |
-
"service_rating": float(row.get("service_rating", 0) or 0),
|
| 196 |
-
"ambience_rating": float(row.get("ambience_rating", 0) or 0),
|
| 197 |
-
"text": str(row.get("review_text", ""))
|
| 198 |
})
|
| 199 |
|
| 200 |
return {
|
| 201 |
"success": True,
|
| 202 |
-
"source": platform,
|
| 203 |
"total_reviews": len(reviews),
|
| 204 |
"reviews": reviews,
|
| 205 |
-
"
|
| 206 |
"metadata": result.get("metadata", {}),
|
| 207 |
}
|
| 208 |
|
|
@@ -213,18 +238,27 @@ def scrape_restaurant_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 213 |
timeout=2400,
|
| 214 |
)
|
| 215 |
def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
| 216 |
-
"""
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
#
|
| 220 |
if platform == "opentable":
|
| 221 |
from src.scrapers.opentable_scraper import scrape_opentable
|
| 222 |
result = scrape_opentable(url=url, max_reviews=max_reviews, headless=True)
|
| 223 |
-
|
| 224 |
from src.scrapers.google_maps_scraper import scrape_google_maps
|
| 225 |
result = scrape_google_maps(url=url, max_reviews=max_reviews, headless=True)
|
| 226 |
-
else:
|
| 227 |
-
return {"success": False, "error": "Unsupported platform. Use OpenTable or Google Maps URL."}
|
| 228 |
|
| 229 |
if not result.get("success"):
|
| 230 |
return {"success": False, "error": result.get("error")}
|
|
@@ -235,16 +269,15 @@ def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 235 |
df = process_reviews(result)
|
| 236 |
reviews = clean_reviews_for_ai(df["review_text"].tolist(), verbose=False)
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
|
|
|
|
| 240 |
for _, row in df.iterrows():
|
| 241 |
-
|
|
|
|
| 242 |
"date": str(row.get("date", "")),
|
| 243 |
"rating": float(row.get("overall_rating", 0) or 0),
|
| 244 |
-
"
|
| 245 |
-
"service_rating": float(row.get("service_rating", 0) or 0),
|
| 246 |
-
"ambience_rating": float(row.get("ambience_rating", 0) or 0),
|
| 247 |
-
"text": str(row.get("review_text", ""))
|
| 248 |
})
|
| 249 |
|
| 250 |
# Extract restaurant name from URL
|
|
@@ -257,7 +290,7 @@ def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 257 |
else:
|
| 258 |
restaurant_name = "Restaurant"
|
| 259 |
|
| 260 |
-
#
|
| 261 |
agent = RestaurantAnalysisAgent()
|
| 262 |
analysis = agent.analyze_restaurant(
|
| 263 |
restaurant_url=url,
|
|
@@ -265,18 +298,23 @@ def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 265 |
reviews=reviews,
|
| 266 |
)
|
| 267 |
|
| 268 |
-
# Store in MCP cache
|
| 269 |
REVIEW_INDEX[restaurant_name] = reviews
|
| 270 |
|
| 271 |
-
# Add
|
| 272 |
-
analysis['
|
| 273 |
analysis['source'] = platform
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
return analysis
|
| 276 |
|
| 277 |
|
| 278 |
# ============================================================================
|
| 279 |
-
# FASTAPI APP
|
| 280 |
# ============================================================================
|
| 281 |
|
| 282 |
@app.function(
|
|
@@ -286,11 +324,11 @@ def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
|
| 286 |
)
|
| 287 |
@modal.asgi_app()
|
| 288 |
def fastapi_app():
|
| 289 |
-
"""Main API with
|
| 290 |
from fastapi import FastAPI, HTTPException
|
| 291 |
from pydantic import BaseModel
|
| 292 |
|
| 293 |
-
web_app = FastAPI(title="Restaurant Intelligence API
|
| 294 |
|
| 295 |
class AnalyzeRequest(BaseModel):
|
| 296 |
url: str
|
|
@@ -306,7 +344,7 @@ def fastapi_app():
|
|
| 306 |
"name": "Restaurant Intelligence API",
|
| 307 |
"version": "3.0",
|
| 308 |
"mcp": "enabled",
|
| 309 |
-
"
|
| 310 |
"endpoints": {
|
| 311 |
"analyze": "/analyze",
|
| 312 |
"mcp_tools": "/mcp/call",
|
|
@@ -316,24 +354,17 @@ def fastapi_app():
|
|
| 316 |
|
| 317 |
@web_app.get("/health")
|
| 318 |
async def health():
|
| 319 |
-
return {"status": "healthy", "mcp": "enabled"
|
| 320 |
|
| 321 |
@web_app.post("/analyze")
|
| 322 |
async def analyze(request: AnalyzeRequest):
|
| 323 |
try:
|
| 324 |
-
# Detect platform first
|
| 325 |
-
platform = detect_platform(request.url)
|
| 326 |
-
if platform == "unknown":
|
| 327 |
-
raise HTTPException(
|
| 328 |
-
status_code=400,
|
| 329 |
-
detail="Unsupported URL. Please use OpenTable or Google Maps URL."
|
| 330 |
-
)
|
| 331 |
-
|
| 332 |
result = full_analysis_modal.remote(url=request.url, max_reviews=request.max_reviews)
|
| 333 |
return result
|
| 334 |
except Exception as e:
|
| 335 |
raise HTTPException(status_code=500, detail=str(e))
|
| 336 |
|
|
|
|
| 337 |
@web_app.get("/mcp/tools")
|
| 338 |
async def mcp_list_tools():
|
| 339 |
return {
|
|
@@ -346,6 +377,8 @@ def fastapi_app():
|
|
| 346 |
|
| 347 |
@web_app.post("/mcp/call")
|
| 348 |
async def mcp_call(request: MCPCallRequest):
|
|
|
|
|
|
|
| 349 |
if request.tool_name == "index_reviews":
|
| 350 |
args = request.arguments
|
| 351 |
REVIEW_INDEX[args["restaurant_name"]] = args["reviews"]
|
|
@@ -374,20 +407,20 @@ def fastapi_app():
|
|
| 374 |
|
| 375 |
@app.local_entrypoint()
|
| 376 |
def main():
|
| 377 |
-
print("π§ͺ Testing Modal deployment
|
| 378 |
|
| 379 |
print("1οΈβ£ Testing connection...")
|
| 380 |
result = hello.remote()
|
| 381 |
print(f"β
{result}\n")
|
| 382 |
|
| 383 |
-
print("2οΈβ£
|
| 384 |
-
print(" β’ OpenTable (opentable.com)")
|
| 385 |
-
print(" β’ Google Maps (google.com/maps)")
|
| 386 |
-
|
| 387 |
-
print("\n3οΈβ£ MCP Server deployed at:")
|
| 388 |
print(" https://tushar-pingle--restaurant-intelligence-mcp-server.modal.run")
|
| 389 |
|
| 390 |
-
print("\
|
| 391 |
print(" https://tushar-pingle--restaurant-intelligence-fastapi-app.modal.run")
|
| 392 |
|
| 393 |
-
print("\nβ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Modal Backend for Restaurant Intelligence Agent
|
| 3 |
+
With TRUE MCP Server Integration
|
| 4 |
|
| 5 |
+
UPDATED: Now sends slim trend_data instead of full raw_reviews
|
| 6 |
+
- Reduces response size by ~97%
|
| 7 |
+
- Pre-calculates sentiment in backend
|
| 8 |
+
- Fixes HuggingFace timeout issues
|
| 9 |
+
|
| 10 |
+
Deploys:
|
| 11 |
+
1. Analysis API endpoint
|
| 12 |
+
2. MCP Server endpoint
|
| 13 |
"""
|
| 14 |
|
| 15 |
import modal
|
|
|
|
| 32 |
"python-dotenv",
|
| 33 |
"matplotlib",
|
| 34 |
"fastapi[standard]",
|
| 35 |
+
"httpx",
|
|
|
|
| 36 |
)
|
| 37 |
.add_local_python_source("src")
|
| 38 |
)
|
| 39 |
|
| 40 |
|
| 41 |
# ============================================================================
|
| 42 |
+
# HELPER FUNCTION - Calculate sentiment from text
|
| 43 |
# ============================================================================
|
| 44 |
|
| 45 |
+
def calculate_sentiment(text: str) -> float:
|
| 46 |
+
"""Simple sentiment calculation from review text."""
|
| 47 |
+
if not text:
|
| 48 |
+
return 0.0
|
| 49 |
+
text = str(text).lower()
|
| 50 |
+
|
| 51 |
+
positive = ['amazing', 'excellent', 'fantastic', 'great', 'awesome', 'delicious',
|
| 52 |
+
'perfect', 'outstanding', 'loved', 'beautiful', 'fresh', 'friendly',
|
| 53 |
+
'best', 'wonderful', 'incredible', 'superb', 'exceptional']
|
| 54 |
+
negative = ['terrible', 'horrible', 'awful', 'bad', 'worst', 'disappointing',
|
| 55 |
+
'poor', 'overpriced', 'slow', 'rude', 'cold', 'bland', 'mediocre',
|
| 56 |
+
'disgusting', 'inedible', 'undercooked', 'overcooked']
|
| 57 |
+
|
| 58 |
+
pos = sum(1 for w in positive if w in text)
|
| 59 |
+
neg = sum(1 for w in negative if w in text)
|
| 60 |
+
|
| 61 |
+
if pos + neg == 0:
|
| 62 |
+
return 0.0
|
| 63 |
+
return (pos - neg) / max(pos + neg, 1)
|
| 64 |
|
| 65 |
|
| 66 |
# ============================================================================
|
| 67 |
# MCP SERVER (TRUE MCP INTEGRATION)
|
| 68 |
# ============================================================================
|
| 69 |
|
| 70 |
+
# In-memory storage for MCP
|
| 71 |
REVIEW_INDEX: Dict[str, List[str]] = {}
|
| 72 |
ANALYSIS_CACHE: Dict[str, Dict[str, Any]] = {}
|
| 73 |
|
|
|
|
| 75 |
@app.function(image=image, timeout=300)
|
| 76 |
@modal.asgi_app()
|
| 77 |
def mcp_server():
|
| 78 |
+
"""
|
| 79 |
+
TRUE MCP Server - exposes tools via MCP protocol over HTTP.
|
| 80 |
+
"""
|
| 81 |
from fastapi import FastAPI, HTTPException
|
| 82 |
from pydantic import BaseModel
|
| 83 |
from datetime import datetime
|
|
|
|
| 97 |
question: str
|
| 98 |
top_k: int = 5
|
| 99 |
|
| 100 |
+
# MCP Tools
|
| 101 |
def index_reviews(restaurant_name: str, reviews: List[str]) -> Dict[str, Any]:
|
| 102 |
REVIEW_INDEX[restaurant_name] = reviews
|
| 103 |
return {
|
|
|
|
| 124 |
"review_count": min(top_k, len(reviews))
|
| 125 |
}
|
| 126 |
|
| 127 |
+
def save_report(restaurant_name: str, report_data: Dict, report_type: str = "analysis") -> Dict[str, Any]:
|
| 128 |
+
report_id = f"{restaurant_name}_{report_type}_{datetime.now().isoformat()}"
|
| 129 |
+
ANALYSIS_CACHE[report_id] = {"restaurant": restaurant_name, "type": report_type, "data": report_data}
|
| 130 |
+
return {"success": True, "report_id": report_id}
|
| 131 |
+
|
| 132 |
def list_tools() -> Dict[str, Any]:
|
| 133 |
return {
|
| 134 |
"success": True,
|
|
|
|
| 141 |
|
| 142 |
@mcp_api.get("/")
|
| 143 |
async def root():
|
| 144 |
+
return {"name": "Restaurant Intelligence MCP Server", "protocol": "MCP", "version": "1.0"}
|
| 145 |
|
| 146 |
@mcp_api.get("/health")
|
| 147 |
async def health():
|
|
|
|
| 153 |
|
| 154 |
@mcp_api.post("/mcp/call")
|
| 155 |
async def call_tool(request: ToolRequest):
|
| 156 |
+
"""TRUE MCP interface - agent calls tools via this endpoint."""
|
| 157 |
tool_map = {
|
| 158 |
"index_reviews": lambda args: index_reviews(args["restaurant_name"], args["reviews"]),
|
| 159 |
"query_reviews": lambda args: query_reviews(args["restaurant_name"], args["question"], args.get("top_k", 5)),
|
| 160 |
+
"save_report": lambda args: save_report(args["restaurant_name"], args["report_data"], args.get("report_type", "analysis")),
|
| 161 |
"list_tools": lambda args: list_tools()
|
| 162 |
}
|
| 163 |
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
raise HTTPException(status_code=500, detail=str(e))
|
| 172 |
|
| 173 |
+
@mcp_api.post("/tools/index_reviews")
|
| 174 |
+
async def api_index_reviews(request: IndexReviewsRequest):
|
| 175 |
+
return index_reviews(request.restaurant_name, request.reviews)
|
| 176 |
+
|
| 177 |
+
@mcp_api.post("/tools/query_reviews")
|
| 178 |
+
async def api_query_reviews(request: QueryReviewsRequest):
|
| 179 |
+
return query_reviews(request.restaurant_name, request.question, request.top_k)
|
| 180 |
+
|
| 181 |
return mcp_api
|
| 182 |
|
| 183 |
|
| 184 |
# ============================================================================
|
| 185 |
+
# MAIN ANALYSIS FUNCTIONS
|
| 186 |
# ============================================================================
|
| 187 |
|
| 188 |
@app.function(image=image)
|
| 189 |
def hello() -> Dict[str, Any]:
|
| 190 |
+
return {"status": "Modal is working!", "mcp": "enabled"}
|
| 191 |
|
| 192 |
|
| 193 |
+
@app.function(image=image, timeout=600)
|
| 194 |
def scrape_restaurant_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
| 195 |
+
"""Scrape reviews from OpenTable or Google Maps."""
|
|
|
|
| 196 |
|
| 197 |
+
# Detect platform
|
| 198 |
+
url_lower = url.lower()
|
| 199 |
+
if 'opentable' in url_lower:
|
| 200 |
from src.scrapers.opentable_scraper import scrape_opentable
|
| 201 |
result = scrape_opentable(url=url, max_reviews=max_reviews, headless=True)
|
| 202 |
+
elif any(x in url_lower for x in ['google.com/maps', 'goo.gl/maps', 'maps.google', 'maps.app.goo.gl']):
|
| 203 |
from src.scrapers.google_maps_scraper import scrape_google_maps
|
| 204 |
result = scrape_google_maps(url=url, max_reviews=max_reviews, headless=True)
|
| 205 |
else:
|
| 206 |
+
return {"success": False, "error": "Unsupported platform. Use OpenTable or Google Maps."}
|
| 207 |
|
| 208 |
if not result.get("success"):
|
| 209 |
return {"success": False, "error": result.get("error")}
|
|
|
|
| 213 |
df = process_reviews(result)
|
| 214 |
reviews = clean_reviews_for_ai(df["review_text"].tolist(), verbose=False)
|
| 215 |
|
| 216 |
+
# Create SLIM trend_data (pre-calculate sentiment, no text!)
|
| 217 |
+
trend_data = []
|
| 218 |
for _, row in df.iterrows():
|
| 219 |
+
text = str(row.get("review_text", ""))
|
| 220 |
+
trend_data.append({
|
| 221 |
"date": str(row.get("date", "")),
|
| 222 |
"rating": float(row.get("overall_rating", 0) or 0),
|
| 223 |
+
"sentiment": calculate_sentiment(text) # Pre-calculate!
|
|
|
|
|
|
|
|
|
|
| 224 |
})
|
| 225 |
|
| 226 |
return {
|
| 227 |
"success": True,
|
|
|
|
| 228 |
"total_reviews": len(reviews),
|
| 229 |
"reviews": reviews,
|
| 230 |
+
"trend_data": trend_data, # Slim version, no text!
|
| 231 |
"metadata": result.get("metadata", {}),
|
| 232 |
}
|
| 233 |
|
|
|
|
| 238 |
timeout=2400,
|
| 239 |
)
|
| 240 |
def full_analysis_modal(url: str, max_reviews: int = 100) -> Dict[str, Any]:
|
| 241 |
+
"""
|
| 242 |
+
Complete end-to-end analysis with MCP integration.
|
| 243 |
+
|
| 244 |
+
UPDATED: Returns slim trend_data instead of full raw_reviews.
|
| 245 |
+
This reduces response size by ~97% and fixes timeout issues.
|
| 246 |
+
"""
|
| 247 |
+
|
| 248 |
+
# Detect platform
|
| 249 |
+
url_lower = url.lower()
|
| 250 |
+
platform = "opentable" if 'opentable' in url_lower else "google_maps" if any(x in url_lower for x in ['google.com/maps', 'goo.gl/maps', 'maps.google', 'maps.app.goo.gl']) else "unknown"
|
| 251 |
+
|
| 252 |
+
if platform == "unknown":
|
| 253 |
+
return {"success": False, "error": "Unsupported platform. Use OpenTable or Google Maps."}
|
| 254 |
|
| 255 |
+
# Import scrapers
|
| 256 |
if platform == "opentable":
|
| 257 |
from src.scrapers.opentable_scraper import scrape_opentable
|
| 258 |
result = scrape_opentable(url=url, max_reviews=max_reviews, headless=True)
|
| 259 |
+
else:
|
| 260 |
from src.scrapers.google_maps_scraper import scrape_google_maps
|
| 261 |
result = scrape_google_maps(url=url, max_reviews=max_reviews, headless=True)
|
|
|
|
|
|
|
| 262 |
|
| 263 |
if not result.get("success"):
|
| 264 |
return {"success": False, "error": result.get("error")}
|
|
|
|
| 269 |
df = process_reviews(result)
|
| 270 |
reviews = clean_reviews_for_ai(df["review_text"].tolist(), verbose=False)
|
| 271 |
|
| 272 |
+
# Create SLIM trend_data (pre-calculate sentiment in backend!)
|
| 273 |
+
# This is ~97% smaller than sending full review text
|
| 274 |
+
trend_data = []
|
| 275 |
for _, row in df.iterrows():
|
| 276 |
+
text = str(row.get("review_text", ""))
|
| 277 |
+
trend_data.append({
|
| 278 |
"date": str(row.get("date", "")),
|
| 279 |
"rating": float(row.get("overall_rating", 0) or 0),
|
| 280 |
+
"sentiment": calculate_sentiment(text) # Pre-calculated!
|
|
|
|
|
|
|
|
|
|
| 281 |
})
|
| 282 |
|
| 283 |
# Extract restaurant name from URL
|
|
|
|
| 290 |
else:
|
| 291 |
restaurant_name = "Restaurant"
|
| 292 |
|
| 293 |
+
# Run analysis
|
| 294 |
agent = RestaurantAnalysisAgent()
|
| 295 |
analysis = agent.analyze_restaurant(
|
| 296 |
restaurant_url=url,
|
|
|
|
| 298 |
reviews=reviews,
|
| 299 |
)
|
| 300 |
|
| 301 |
+
# Store in MCP cache for Q&A
|
| 302 |
REVIEW_INDEX[restaurant_name] = reviews
|
| 303 |
|
| 304 |
+
# Add slim trend_data (NOT full raw_reviews!)
|
| 305 |
+
analysis['trend_data'] = trend_data
|
| 306 |
analysis['source'] = platform
|
| 307 |
|
| 308 |
+
# Log response size for debugging
|
| 309 |
+
import json
|
| 310 |
+
response_size = len(json.dumps(analysis))
|
| 311 |
+
print(f"[MODAL] Response size: {response_size / 1024:.1f} KB")
|
| 312 |
+
|
| 313 |
return analysis
|
| 314 |
|
| 315 |
|
| 316 |
# ============================================================================
|
| 317 |
+
# FASTAPI APP (serves both analysis and MCP)
|
| 318 |
# ============================================================================
|
| 319 |
|
| 320 |
@app.function(
|
|
|
|
| 324 |
)
|
| 325 |
@modal.asgi_app()
|
| 326 |
def fastapi_app():
|
| 327 |
+
"""Main API with MCP integration."""
|
| 328 |
from fastapi import FastAPI, HTTPException
|
| 329 |
from pydantic import BaseModel
|
| 330 |
|
| 331 |
+
web_app = FastAPI(title="Restaurant Intelligence API with MCP")
|
| 332 |
|
| 333 |
class AnalyzeRequest(BaseModel):
|
| 334 |
url: str
|
|
|
|
| 344 |
"name": "Restaurant Intelligence API",
|
| 345 |
"version": "3.0",
|
| 346 |
"mcp": "enabled",
|
| 347 |
+
"optimizations": ["slim_trend_data", "pre_calculated_sentiment"],
|
| 348 |
"endpoints": {
|
| 349 |
"analyze": "/analyze",
|
| 350 |
"mcp_tools": "/mcp/call",
|
|
|
|
| 354 |
|
| 355 |
@web_app.get("/health")
|
| 356 |
async def health():
|
| 357 |
+
return {"status": "healthy", "mcp": "enabled"}
|
| 358 |
|
| 359 |
@web_app.post("/analyze")
|
| 360 |
async def analyze(request: AnalyzeRequest):
|
| 361 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
result = full_analysis_modal.remote(url=request.url, max_reviews=request.max_reviews)
|
| 363 |
return result
|
| 364 |
except Exception as e:
|
| 365 |
raise HTTPException(status_code=500, detail=str(e))
|
| 366 |
|
| 367 |
+
# MCP Endpoints
|
| 368 |
@web_app.get("/mcp/tools")
|
| 369 |
async def mcp_list_tools():
|
| 370 |
return {
|
|
|
|
| 377 |
|
| 378 |
@web_app.post("/mcp/call")
|
| 379 |
async def mcp_call(request: MCPCallRequest):
|
| 380 |
+
"""TRUE MCP interface."""
|
| 381 |
+
# For now, this delegates to local functions
|
| 382 |
if request.tool_name == "index_reviews":
|
| 383 |
args = request.arguments
|
| 384 |
REVIEW_INDEX[args["restaurant_name"]] = args["reviews"]
|
|
|
|
| 407 |
|
| 408 |
@app.local_entrypoint()
|
| 409 |
def main():
|
| 410 |
+
print("π§ͺ Testing Modal deployment with MCP...\n")
|
| 411 |
|
| 412 |
print("1οΈβ£ Testing connection...")
|
| 413 |
result = hello.remote()
|
| 414 |
print(f"β
{result}\n")
|
| 415 |
|
| 416 |
+
print("2οΈβ£ MCP Server deployed at:")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
print(" https://tushar-pingle--restaurant-intelligence-mcp-server.modal.run")
|
| 418 |
|
| 419 |
+
print("\n3οΈβ£ Analysis API deployed at:")
|
| 420 |
print(" https://tushar-pingle--restaurant-intelligence-fastapi-app.modal.run")
|
| 421 |
|
| 422 |
+
print("\nβ
Both endpoints ready!")
|
| 423 |
+
print("\nπ Optimizations enabled:")
|
| 424 |
+
print(" - Slim trend_data (no full review text)")
|
| 425 |
+
print(" - Pre-calculated sentiment in backend")
|
| 426 |
+
print(" - ~97% smaller response size")
|
src/ui/gradio_app.py
CHANGED
|
@@ -128,41 +128,47 @@ def calculate_review_sentiment(text: str) -> float:
|
|
| 128 |
return (pos - neg) / max(pos + neg, 1)
|
| 129 |
|
| 130 |
|
| 131 |
-
def generate_trend_chart(
|
| 132 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
import matplotlib
|
| 134 |
matplotlib.use('Agg')
|
| 135 |
import matplotlib.pyplot as plt
|
| 136 |
import matplotlib.dates as mdates
|
| 137 |
|
| 138 |
-
if not
|
| 139 |
return None
|
| 140 |
|
| 141 |
dated_reviews = []
|
| 142 |
-
for r in
|
| 143 |
if not isinstance(r, dict):
|
| 144 |
continue
|
| 145 |
date = parse_opentable_date(r.get('date', ''))
|
| 146 |
if date:
|
| 147 |
-
rating = float(r.get('rating', 0) or
|
| 148 |
-
|
| 149 |
dated_reviews.append({
|
| 150 |
'date': date,
|
| 151 |
'rating': rating if rating > 0 else 3.5,
|
| 152 |
-
'sentiment':
|
| 153 |
})
|
| 154 |
|
| 155 |
-
|
|
|
|
| 156 |
dated_reviews = []
|
| 157 |
-
for i, r in enumerate(
|
| 158 |
if not isinstance(r, dict):
|
| 159 |
continue
|
| 160 |
-
rating = float(r.get('rating', 0) or
|
| 161 |
-
|
| 162 |
dated_reviews.append({
|
| 163 |
'date': datetime.now() - timedelta(days=i),
|
| 164 |
'rating': rating if rating > 0 else 3.5,
|
| 165 |
-
'sentiment':
|
| 166 |
})
|
| 167 |
|
| 168 |
if len(dated_reviews) < 3:
|
|
@@ -246,26 +252,36 @@ def generate_trend_chart(raw_reviews: List[Dict], restaurant_name: str) -> Optio
|
|
| 246 |
return None
|
| 247 |
|
| 248 |
|
| 249 |
-
def generate_trend_insight(
|
| 250 |
-
"""
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
return "Not enough data to analyze trends (need 3+ reviews)."
|
| 253 |
|
| 254 |
ratings = []
|
| 255 |
sentiments = []
|
| 256 |
-
for r in
|
| 257 |
if isinstance(r, dict):
|
| 258 |
rating = float(r.get('rating', 0) or r.get('overall_rating', 0) or 0)
|
| 259 |
if rating > 0:
|
| 260 |
ratings.append(rating)
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
if not ratings:
|
| 265 |
return "No rating data available."
|
| 266 |
|
| 267 |
avg_rating = sum(ratings) / len(ratings)
|
| 268 |
-
avg_sentiment = sum(sentiments) / len(sentiments)
|
| 269 |
|
| 270 |
insight = f"**{restaurant_name}** has an average rating of **{avg_rating:.1f} stars** "
|
| 271 |
|
|
@@ -659,7 +675,8 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 659 |
menu = state.get('menu_analysis', {})
|
| 660 |
aspects = state.get('aspect_analysis', {})
|
| 661 |
insights = state.get('insights', {})
|
| 662 |
-
|
|
|
|
| 663 |
|
| 664 |
food_items = menu.get('food_items', [])
|
| 665 |
drinks = menu.get('drinks', [])
|
|
@@ -684,7 +701,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 684 |
|
| 685 |
styles = getSampleStyleSheet()
|
| 686 |
|
| 687 |
-
# Custom styles
|
| 688 |
styles.add(ParagraphStyle('CoverTitle', parent=styles['Heading1'],
|
| 689 |
fontSize=32, textColor=PRIMARY, alignment=TA_CENTER,
|
| 690 |
spaceAfter=10, fontName='Helvetica-Bold'))
|
|
@@ -705,7 +722,8 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 705 |
fontSize=14, textColor=TEXT_DARK, spaceBefore=15,
|
| 706 |
spaceAfter=8, fontName='Helvetica-Bold'))
|
| 707 |
|
| 708 |
-
|
|
|
|
| 709 |
fontSize=10, textColor=TEXT_DARK, spaceAfter=8,
|
| 710 |
leading=14, fontName='Helvetica'))
|
| 711 |
|
|
@@ -717,7 +735,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 717 |
fontSize=10, textColor=TEXT_LIGHT, leftIndent=20,
|
| 718 |
rightIndent=20, spaceAfter=10, fontName='Helvetica-Oblique'))
|
| 719 |
|
| 720 |
-
styles.add(ParagraphStyle('
|
| 721 |
fontSize=8, textColor=TEXT_LIGHT, alignment=TA_CENTER))
|
| 722 |
|
| 723 |
styles.add(ParagraphStyle('PriorityHigh', parent=styles['Normal'],
|
|
@@ -744,12 +762,12 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 744 |
elements.append(Spacer(1, 0.5*inch))
|
| 745 |
elements.append(Paragraph(restaurant_name, styles['CoverRestaurant']))
|
| 746 |
elements.append(Spacer(1, 0.3*inch))
|
| 747 |
-
elements.append(Paragraph(f"Data Source: {source}", styles['
|
| 748 |
elements.append(Spacer(1, 0.5*inch))
|
| 749 |
|
| 750 |
# Stats boxes
|
| 751 |
stats_data = [[
|
| 752 |
-
str(len(
|
| 753 |
], [
|
| 754 |
"Reviews", "Menu Items", "Aspects"
|
| 755 |
]]
|
|
@@ -768,8 +786,8 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 768 |
]))
|
| 769 |
elements.append(stats_table)
|
| 770 |
elements.append(Spacer(1, 1*inch))
|
| 771 |
-
elements.append(Paragraph(f"Generated: {datetime.now().strftime('%B %d, %Y at %I:%M %p')}", styles['
|
| 772 |
-
elements.append(Paragraph("Powered by Claude AI β’ Restaurant Intelligence Agent", styles['
|
| 773 |
elements.append(PageBreak())
|
| 774 |
|
| 775 |
# ==================== EXECUTIVE SUMMARY ====================
|
|
@@ -804,14 +822,14 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 804 |
elements.append(Paragraph("Key Highlights", styles['SubHeader']))
|
| 805 |
top_items = sorted(all_menu, key=lambda x: x.get('sentiment', 0), reverse=True)[:3]
|
| 806 |
if top_items:
|
| 807 |
-
elements.append(Paragraph("β
<b>Top Performing Items:</b>", styles['
|
| 808 |
for item in top_items:
|
| 809 |
elements.append(Paragraph(f" β’ {item.get('name', '?').title()} (sentiment: {item.get('sentiment', 0):+.2f})", styles['Bullet']))
|
| 810 |
|
| 811 |
concern_items = [i for i in all_menu if i.get('sentiment', 0) < -0.2]
|
| 812 |
if concern_items:
|
| 813 |
elements.append(Spacer(1, 10))
|
| 814 |
-
elements.append(Paragraph("β οΈ <b>Items Needing Attention:</b>", styles['
|
| 815 |
for item in sorted(concern_items, key=lambda x: x.get('sentiment', 0))[:3]:
|
| 816 |
elements.append(Paragraph(f" β’ {item.get('name', '?').title()} (sentiment: {item.get('sentiment', 0):+.2f})", styles['Bullet']))
|
| 817 |
|
|
@@ -825,7 +843,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 825 |
|
| 826 |
summary_data = [
|
| 827 |
['Metric', 'Value', 'Details'],
|
| 828 |
-
['Reviews Analyzed', str(len(
|
| 829 |
['Menu Items', str(len(all_menu)), f'{len(food_items)} food, {len(drinks)} drinks'],
|
| 830 |
['Customer Favorites', str(stars), 'Sentiment > 0.5'],
|
| 831 |
['Performing Well', str(good), 'Sentiment 0.2 - 0.5'],
|
|
@@ -853,8 +871,8 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 853 |
|
| 854 |
if all_menu:
|
| 855 |
elements.append(Paragraph(
|
| 856 |
-
f"Analysis of <b>{len(all_menu)}</b> menu items ({len(food_items)} food, {len(drinks)} drinks) based on {len(
|
| 857 |
-
styles['
|
| 858 |
))
|
| 859 |
elements.append(Spacer(1, 10))
|
| 860 |
|
|
@@ -917,7 +935,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 917 |
if chef_data:
|
| 918 |
if chef_data.get('summary'):
|
| 919 |
elements.append(Paragraph("Summary", styles['SubHeader']))
|
| 920 |
-
elements.append(Paragraph(str(chef_data['summary']), styles['
|
| 921 |
|
| 922 |
if chef_data.get('strengths'):
|
| 923 |
elements.append(Paragraph("β
Strengths", styles['SubHeader']))
|
|
@@ -948,7 +966,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 948 |
else:
|
| 949 |
elements.append(Paragraph(f"β’ {r}", styles['Bullet']))
|
| 950 |
else:
|
| 951 |
-
elements.append(Paragraph("Chef insights will be available after full analysis.", styles['
|
| 952 |
|
| 953 |
elements.append(Spacer(1, 20))
|
| 954 |
|
|
@@ -960,7 +978,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 960 |
if manager_data:
|
| 961 |
if manager_data.get('summary'):
|
| 962 |
elements.append(Paragraph("Summary", styles['SubHeader']))
|
| 963 |
-
elements.append(Paragraph(str(manager_data['summary']), styles['
|
| 964 |
|
| 965 |
if manager_data.get('strengths'):
|
| 966 |
elements.append(Paragraph("β
Operational Strengths", styles['SubHeader']))
|
|
@@ -991,7 +1009,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 991 |
else:
|
| 992 |
elements.append(Paragraph(f"β’ {r}", styles['Bullet']))
|
| 993 |
else:
|
| 994 |
-
elements.append(Paragraph("Manager insights will be available after full analysis.", styles['
|
| 995 |
|
| 996 |
elements.append(PageBreak())
|
| 997 |
|
|
@@ -1002,25 +1020,37 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 1002 |
positive_reviews = []
|
| 1003 |
negative_reviews = []
|
| 1004 |
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1022 |
positive_reviews.append(text[:180])
|
| 1023 |
-
elif
|
| 1024 |
negative_reviews.append(text[:180])
|
| 1025 |
|
| 1026 |
elements.append(Paragraph("β
Positive Feedback", styles['SubHeader']))
|
|
@@ -1028,7 +1058,7 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 1028 |
for review in positive_reviews:
|
| 1029 |
elements.append(Paragraph(f'"{review}..."', styles['Quote']))
|
| 1030 |
else:
|
| 1031 |
-
elements.append(Paragraph("Detailed positive feedback samples not available.", styles['
|
| 1032 |
|
| 1033 |
elements.append(Spacer(1, 15))
|
| 1034 |
|
|
@@ -1037,15 +1067,15 @@ def generate_pdf_report(state: dict) -> Optional[str]:
|
|
| 1037 |
for review in negative_reviews:
|
| 1038 |
elements.append(Paragraph(f'"{review}..."', styles['Quote']))
|
| 1039 |
else:
|
| 1040 |
-
elements.append(Paragraph("No significant negative feedback identified. Great job!", styles['
|
| 1041 |
|
| 1042 |
# ==================== FOOTER ====================
|
| 1043 |
elements.append(Spacer(1, 30))
|
| 1044 |
elements.append(HRFlowable(width="100%", thickness=1, color=BORDER, spaceBefore=10, spaceAfter=10))
|
| 1045 |
-
elements.append(Paragraph(f"Report generated for {restaurant_name}", styles['
|
| 1046 |
-
elements.append(Paragraph(f"Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}", styles['
|
| 1047 |
-
elements.append(Paragraph("Restaurant Intelligence Agent β’ Powered by Claude AI", styles['
|
| 1048 |
-
elements.append(Paragraph("Β© 2025 - Built for Anthropic MCP Hackathon", styles['
|
| 1049 |
|
| 1050 |
# Build PDF
|
| 1051 |
doc.build(elements)
|
|
@@ -1144,7 +1174,7 @@ AMBIANCE_WORDS = {'ambiance', 'atmosphere', 'vibe', 'decor', 'noise', 'loud', 'q
|
|
| 1144 |
|
| 1145 |
|
| 1146 |
def find_relevant_reviews(question: str, state: dict, top_k: int = 8) -> List[str]:
|
| 1147 |
-
"""Find relevant reviews for the question."""
|
| 1148 |
if not state:
|
| 1149 |
return []
|
| 1150 |
|
|
@@ -1153,7 +1183,6 @@ def find_relevant_reviews(question: str, state: dict, top_k: int = 8) -> List[st
|
|
| 1153 |
|
| 1154 |
menu = state.get('menu_analysis', {})
|
| 1155 |
aspects = state.get('aspect_analysis', {})
|
| 1156 |
-
raw_reviews = state.get('raw_reviews', [])
|
| 1157 |
|
| 1158 |
all_items = menu.get('food_items', []) + menu.get('drinks', [])
|
| 1159 |
all_aspects = aspects.get('aspects', [])
|
|
@@ -1195,26 +1224,22 @@ def find_relevant_reviews(question: str, state: dict, top_k: int = 8) -> List[st
|
|
| 1195 |
if text not in relevant_reviews:
|
| 1196 |
relevant_reviews.append(text)
|
| 1197 |
|
| 1198 |
-
# Fallback:
|
| 1199 |
-
if not relevant_reviews
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
-
|
| 1203 |
-
|
| 1204 |
-
text = str(r)
|
| 1205 |
-
|
| 1206 |
-
# Check if any question word appears in review
|
| 1207 |
-
if any(w in text.lower() for w in q_words):
|
| 1208 |
relevant_reviews.append(text)
|
| 1209 |
|
| 1210 |
-
#
|
| 1211 |
-
|
| 1212 |
-
|
| 1213 |
-
|
| 1214 |
-
|
| 1215 |
-
|
| 1216 |
-
text = str(r)
|
| 1217 |
-
if text and len(text) > 20:
|
| 1218 |
relevant_reviews.append(text)
|
| 1219 |
|
| 1220 |
return relevant_reviews[:top_k]
|
|
@@ -1342,7 +1367,7 @@ EXAMPLE_QUESTIONS = [
|
|
| 1342 |
# ============================================================================
|
| 1343 |
|
| 1344 |
def analyze_restaurant(url: str, review_count: int):
|
| 1345 |
-
"""Main analysis function - calls Modal API."""
|
| 1346 |
|
| 1347 |
empty = {}
|
| 1348 |
default_summary = "Run analysis to see performance overview."
|
|
@@ -1376,23 +1401,56 @@ def analyze_restaurant(url: str, review_count: int):
|
|
| 1376 |
|
| 1377 |
try:
|
| 1378 |
print(f"[ANALYZE] {platform_emoji} Analyzing {restaurant_name} from {platform}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1379 |
|
| 1380 |
-
|
|
|
|
| 1381 |
f"{MODAL_API_URL}/analyze",
|
| 1382 |
json={"url": url, "max_reviews": review_count},
|
| 1383 |
-
timeout=
|
|
|
|
| 1384 |
)
|
| 1385 |
|
|
|
|
|
|
|
| 1386 |
if response.status_code != 200:
|
|
|
|
|
|
|
| 1387 |
return (
|
| 1388 |
-
f"β **API Error ({response.status_code}):** {
|
| 1389 |
None, "No trend data available.",
|
| 1390 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1391 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1392 |
empty
|
| 1393 |
)
|
| 1394 |
|
| 1395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1396 |
|
| 1397 |
if not data.get("success"):
|
| 1398 |
return (
|
|
@@ -1406,24 +1464,29 @@ def analyze_restaurant(url: str, review_count: int):
|
|
| 1406 |
menu = data.get('menu_analysis', {})
|
| 1407 |
aspects = data.get('aspect_analysis', {})
|
| 1408 |
insights = data.get('insights', {})
|
| 1409 |
-
|
|
|
|
|
|
|
|
|
|
| 1410 |
|
| 1411 |
food_items = menu.get('food_items', [])
|
| 1412 |
drinks = menu.get('drinks', [])
|
| 1413 |
aspect_list = aspects.get('aspects', [])
|
| 1414 |
all_menu = food_items + drinks
|
| 1415 |
|
|
|
|
|
|
|
| 1416 |
state = {
|
| 1417 |
"menu_analysis": menu,
|
| 1418 |
"aspect_analysis": aspects,
|
| 1419 |
"insights": insights,
|
| 1420 |
"restaurant_name": restaurant_name,
|
| 1421 |
-
"
|
| 1422 |
"source": platform
|
| 1423 |
}
|
| 1424 |
|
| 1425 |
-
trend_chart = generate_trend_chart(
|
| 1426 |
-
trend_insight = generate_trend_insight(
|
| 1427 |
|
| 1428 |
# Use improved detailed summaries
|
| 1429 |
menu_summary = translate_menu_performance(menu, restaurant_name)
|
|
@@ -1448,13 +1511,15 @@ def analyze_restaurant(url: str, review_count: int):
|
|
| 1448 |
|
| 1449 |
**π Summary:**
|
| 1450 |
β’ Source: **{platform.replace('_', ' ').title()}**
|
| 1451 |
-
β’ Reviews analyzed: **{
|
| 1452 |
β’ Menu items found: **{len(all_menu)}** ({len(food_items)} food, {len(drinks)} drinks)
|
| 1453 |
β’ Aspects discovered: **{len(aspect_list)}**
|
| 1454 |
|
| 1455 |
π **Explore the tabs below for detailed insights!**
|
| 1456 |
"""
|
| 1457 |
|
|
|
|
|
|
|
| 1458 |
return (
|
| 1459 |
status,
|
| 1460 |
trend_chart, trend_insight,
|
|
@@ -1464,6 +1529,7 @@ def analyze_restaurant(url: str, review_count: int):
|
|
| 1464 |
)
|
| 1465 |
|
| 1466 |
except requests.exceptions.Timeout:
|
|
|
|
| 1467 |
return (
|
| 1468 |
"β **Timeout:** Request took too long. Try with fewer reviews (50-100).",
|
| 1469 |
None, "No trend data available.",
|
|
@@ -1471,11 +1537,21 @@ def analyze_restaurant(url: str, review_count: int):
|
|
| 1471 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1472 |
empty
|
| 1473 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1474 |
except Exception as e:
|
| 1475 |
import traceback
|
| 1476 |
traceback.print_exc()
|
|
|
|
| 1477 |
return (
|
| 1478 |
-
f"β **Error:** {str(e)}",
|
| 1479 |
None, "No trend data available.",
|
| 1480 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1481 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
|
|
|
| 128 |
return (pos - neg) / max(pos + neg, 1)
|
| 129 |
|
| 130 |
|
| 131 |
+
def generate_trend_chart(trend_data: List[Dict], restaurant_name: str) -> Optional[str]:
|
| 132 |
+
"""
|
| 133 |
+
Generate Rating vs Sentiment trend chart.
|
| 134 |
+
|
| 135 |
+
UPDATED: Now uses pre-calculated trend_data from backend.
|
| 136 |
+
Format: [{"date": "2 days ago", "rating": 4.5, "sentiment": 0.6}, ...]
|
| 137 |
+
"""
|
| 138 |
import matplotlib
|
| 139 |
matplotlib.use('Agg')
|
| 140 |
import matplotlib.pyplot as plt
|
| 141 |
import matplotlib.dates as mdates
|
| 142 |
|
| 143 |
+
if not trend_data or len(trend_data) < 3:
|
| 144 |
return None
|
| 145 |
|
| 146 |
dated_reviews = []
|
| 147 |
+
for r in trend_data:
|
| 148 |
if not isinstance(r, dict):
|
| 149 |
continue
|
| 150 |
date = parse_opentable_date(r.get('date', ''))
|
| 151 |
if date:
|
| 152 |
+
rating = float(r.get('rating', 0) or 0)
|
| 153 |
+
sentiment = float(r.get('sentiment', 0) or 0) # Already calculated!
|
| 154 |
dated_reviews.append({
|
| 155 |
'date': date,
|
| 156 |
'rating': rating if rating > 0 else 3.5,
|
| 157 |
+
'sentiment': sentiment
|
| 158 |
})
|
| 159 |
|
| 160 |
+
# Fallback: if no dates parsed, use sequential ordering
|
| 161 |
+
if len(dated_reviews) < 3 and len(trend_data) >= 3:
|
| 162 |
dated_reviews = []
|
| 163 |
+
for i, r in enumerate(trend_data):
|
| 164 |
if not isinstance(r, dict):
|
| 165 |
continue
|
| 166 |
+
rating = float(r.get('rating', 0) or 3.5)
|
| 167 |
+
sentiment = float(r.get('sentiment', 0) or 0)
|
| 168 |
dated_reviews.append({
|
| 169 |
'date': datetime.now() - timedelta(days=i),
|
| 170 |
'rating': rating if rating > 0 else 3.5,
|
| 171 |
+
'sentiment': sentiment
|
| 172 |
})
|
| 173 |
|
| 174 |
if len(dated_reviews) < 3:
|
|
|
|
| 252 |
return None
|
| 253 |
|
| 254 |
|
| 255 |
+
def generate_trend_insight(trend_data: List[Dict], restaurant_name: str) -> str:
|
| 256 |
+
"""
|
| 257 |
+
Generate text insight from trend data.
|
| 258 |
+
|
| 259 |
+
UPDATED: Now uses pre-calculated trend_data from backend.
|
| 260 |
+
Format: [{"date": "...", "rating": 4.5, "sentiment": 0.6}, ...]
|
| 261 |
+
"""
|
| 262 |
+
if not trend_data or len(trend_data) < 3:
|
| 263 |
return "Not enough data to analyze trends (need 3+ reviews)."
|
| 264 |
|
| 265 |
ratings = []
|
| 266 |
sentiments = []
|
| 267 |
+
for r in trend_data:
|
| 268 |
if isinstance(r, dict):
|
| 269 |
rating = float(r.get('rating', 0) or r.get('overall_rating', 0) or 0)
|
| 270 |
if rating > 0:
|
| 271 |
ratings.append(rating)
|
| 272 |
+
# Use pre-calculated sentiment if available, otherwise calculate
|
| 273 |
+
sentiment = r.get('sentiment')
|
| 274 |
+
if sentiment is not None:
|
| 275 |
+
sentiments.append(float(sentiment))
|
| 276 |
+
else:
|
| 277 |
+
text = r.get('text', '') or r.get('review_text', '')
|
| 278 |
+
sentiments.append(calculate_review_sentiment(text))
|
| 279 |
|
| 280 |
if not ratings:
|
| 281 |
return "No rating data available."
|
| 282 |
|
| 283 |
avg_rating = sum(ratings) / len(ratings)
|
| 284 |
+
avg_sentiment = sum(sentiments) / len(sentiments) if sentiments else 0
|
| 285 |
|
| 286 |
insight = f"**{restaurant_name}** has an average rating of **{avg_rating:.1f} stars** "
|
| 287 |
|
|
|
|
| 675 |
menu = state.get('menu_analysis', {})
|
| 676 |
aspects = state.get('aspect_analysis', {})
|
| 677 |
insights = state.get('insights', {})
|
| 678 |
+
# Use trend_data (slim) or fall back to raw_reviews for backward compatibility
|
| 679 |
+
trend_data = state.get('trend_data', state.get('raw_reviews', []))
|
| 680 |
|
| 681 |
food_items = menu.get('food_items', [])
|
| 682 |
drinks = menu.get('drinks', [])
|
|
|
|
| 701 |
|
| 702 |
styles = getSampleStyleSheet()
|
| 703 |
|
| 704 |
+
# Custom styles - use 'Custom' prefix to avoid conflicts with default styles
|
| 705 |
styles.add(ParagraphStyle('CoverTitle', parent=styles['Heading1'],
|
| 706 |
fontSize=32, textColor=PRIMARY, alignment=TA_CENTER,
|
| 707 |
spaceAfter=10, fontName='Helvetica-Bold'))
|
|
|
|
| 722 |
fontSize=14, textColor=TEXT_DARK, spaceBefore=15,
|
| 723 |
spaceAfter=8, fontName='Helvetica-Bold'))
|
| 724 |
|
| 725 |
+
# Use 'CustomBody' instead of 'BodyText' (which already exists)
|
| 726 |
+
styles.add(ParagraphStyle('CustomBody', parent=styles['Normal'],
|
| 727 |
fontSize=10, textColor=TEXT_DARK, spaceAfter=8,
|
| 728 |
leading=14, fontName='Helvetica'))
|
| 729 |
|
|
|
|
| 735 |
fontSize=10, textColor=TEXT_LIGHT, leftIndent=20,
|
| 736 |
rightIndent=20, spaceAfter=10, fontName='Helvetica-Oblique'))
|
| 737 |
|
| 738 |
+
styles.add(ParagraphStyle('CustomFooter', parent=styles['Normal'],
|
| 739 |
fontSize=8, textColor=TEXT_LIGHT, alignment=TA_CENTER))
|
| 740 |
|
| 741 |
styles.add(ParagraphStyle('PriorityHigh', parent=styles['Normal'],
|
|
|
|
| 762 |
elements.append(Spacer(1, 0.5*inch))
|
| 763 |
elements.append(Paragraph(restaurant_name, styles['CoverRestaurant']))
|
| 764 |
elements.append(Spacer(1, 0.3*inch))
|
| 765 |
+
elements.append(Paragraph(f"Data Source: {source}", styles['CustomFooter']))
|
| 766 |
elements.append(Spacer(1, 0.5*inch))
|
| 767 |
|
| 768 |
# Stats boxes
|
| 769 |
stats_data = [[
|
| 770 |
+
str(len(trend_data)), str(len(all_menu)), str(len(aspect_list))
|
| 771 |
], [
|
| 772 |
"Reviews", "Menu Items", "Aspects"
|
| 773 |
]]
|
|
|
|
| 786 |
]))
|
| 787 |
elements.append(stats_table)
|
| 788 |
elements.append(Spacer(1, 1*inch))
|
| 789 |
+
elements.append(Paragraph(f"Generated: {datetime.now().strftime('%B %d, %Y at %I:%M %p')}", styles['CustomFooter']))
|
| 790 |
+
elements.append(Paragraph("Powered by Claude AI β’ Restaurant Intelligence Agent", styles['CustomFooter']))
|
| 791 |
elements.append(PageBreak())
|
| 792 |
|
| 793 |
# ==================== EXECUTIVE SUMMARY ====================
|
|
|
|
| 822 |
elements.append(Paragraph("Key Highlights", styles['SubHeader']))
|
| 823 |
top_items = sorted(all_menu, key=lambda x: x.get('sentiment', 0), reverse=True)[:3]
|
| 824 |
if top_items:
|
| 825 |
+
elements.append(Paragraph("β
<b>Top Performing Items:</b>", styles['CustomBody']))
|
| 826 |
for item in top_items:
|
| 827 |
elements.append(Paragraph(f" β’ {item.get('name', '?').title()} (sentiment: {item.get('sentiment', 0):+.2f})", styles['Bullet']))
|
| 828 |
|
| 829 |
concern_items = [i for i in all_menu if i.get('sentiment', 0) < -0.2]
|
| 830 |
if concern_items:
|
| 831 |
elements.append(Spacer(1, 10))
|
| 832 |
+
elements.append(Paragraph("β οΈ <b>Items Needing Attention:</b>", styles['CustomBody']))
|
| 833 |
for item in sorted(concern_items, key=lambda x: x.get('sentiment', 0))[:3]:
|
| 834 |
elements.append(Paragraph(f" β’ {item.get('name', '?').title()} (sentiment: {item.get('sentiment', 0):+.2f})", styles['Bullet']))
|
| 835 |
|
|
|
|
| 843 |
|
| 844 |
summary_data = [
|
| 845 |
['Metric', 'Value', 'Details'],
|
| 846 |
+
['Reviews Analyzed', str(len(trend_data)), f'From {source}'],
|
| 847 |
['Menu Items', str(len(all_menu)), f'{len(food_items)} food, {len(drinks)} drinks'],
|
| 848 |
['Customer Favorites', str(stars), 'Sentiment > 0.5'],
|
| 849 |
['Performing Well', str(good), 'Sentiment 0.2 - 0.5'],
|
|
|
|
| 871 |
|
| 872 |
if all_menu:
|
| 873 |
elements.append(Paragraph(
|
| 874 |
+
f"Analysis of <b>{len(all_menu)}</b> menu items ({len(food_items)} food, {len(drinks)} drinks) based on {len(trend_data)} customer reviews.",
|
| 875 |
+
styles['CustomBody']
|
| 876 |
))
|
| 877 |
elements.append(Spacer(1, 10))
|
| 878 |
|
|
|
|
| 935 |
if chef_data:
|
| 936 |
if chef_data.get('summary'):
|
| 937 |
elements.append(Paragraph("Summary", styles['SubHeader']))
|
| 938 |
+
elements.append(Paragraph(str(chef_data['summary']), styles['CustomBody']))
|
| 939 |
|
| 940 |
if chef_data.get('strengths'):
|
| 941 |
elements.append(Paragraph("β
Strengths", styles['SubHeader']))
|
|
|
|
| 966 |
else:
|
| 967 |
elements.append(Paragraph(f"β’ {r}", styles['Bullet']))
|
| 968 |
else:
|
| 969 |
+
elements.append(Paragraph("Chef insights will be available after full analysis.", styles['CustomBody']))
|
| 970 |
|
| 971 |
elements.append(Spacer(1, 20))
|
| 972 |
|
|
|
|
| 978 |
if manager_data:
|
| 979 |
if manager_data.get('summary'):
|
| 980 |
elements.append(Paragraph("Summary", styles['SubHeader']))
|
| 981 |
+
elements.append(Paragraph(str(manager_data['summary']), styles['CustomBody']))
|
| 982 |
|
| 983 |
if manager_data.get('strengths'):
|
| 984 |
elements.append(Paragraph("β
Operational Strengths", styles['SubHeader']))
|
|
|
|
| 1009 |
else:
|
| 1010 |
elements.append(Paragraph(f"β’ {r}", styles['Bullet']))
|
| 1011 |
else:
|
| 1012 |
+
elements.append(Paragraph("Manager insights will be available after full analysis.", styles['CustomBody']))
|
| 1013 |
|
| 1014 |
elements.append(PageBreak())
|
| 1015 |
|
|
|
|
| 1020 |
positive_reviews = []
|
| 1021 |
negative_reviews = []
|
| 1022 |
|
| 1023 |
+
# Extract sample reviews from menu items and aspects (they have related_reviews with text)
|
| 1024 |
+
all_related_reviews = []
|
| 1025 |
+
|
| 1026 |
+
# Get reviews from menu items
|
| 1027 |
+
for item in all_menu:
|
| 1028 |
+
for r in item.get('related_reviews', [])[:2]:
|
| 1029 |
+
if isinstance(r, dict):
|
| 1030 |
+
text = r.get('review_text', r.get('text', ''))
|
| 1031 |
+
else:
|
| 1032 |
+
text = str(r)
|
| 1033 |
+
if text and len(text) > 30:
|
| 1034 |
+
sentiment = item.get('sentiment', 0)
|
| 1035 |
+
all_related_reviews.append({'text': text, 'sentiment': sentiment})
|
| 1036 |
+
|
| 1037 |
+
# Get reviews from aspects
|
| 1038 |
+
for aspect in aspect_list:
|
| 1039 |
+
for r in aspect.get('related_reviews', [])[:2]:
|
| 1040 |
+
if isinstance(r, dict):
|
| 1041 |
+
text = r.get('review_text', r.get('text', ''))
|
| 1042 |
+
else:
|
| 1043 |
+
text = str(r)
|
| 1044 |
+
if text and len(text) > 30:
|
| 1045 |
+
sentiment = aspect.get('sentiment', 0)
|
| 1046 |
+
all_related_reviews.append({'text': text, 'sentiment': sentiment})
|
| 1047 |
+
|
| 1048 |
+
# Sort by sentiment to get best positive and worst negative
|
| 1049 |
+
for review in sorted(all_related_reviews, key=lambda x: x['sentiment'], reverse=True):
|
| 1050 |
+
text = review['text']
|
| 1051 |
+
if review['sentiment'] > 0.2 and len(positive_reviews) < 3:
|
| 1052 |
positive_reviews.append(text[:180])
|
| 1053 |
+
elif review['sentiment'] < -0.2 and len(negative_reviews) < 3:
|
| 1054 |
negative_reviews.append(text[:180])
|
| 1055 |
|
| 1056 |
elements.append(Paragraph("β
Positive Feedback", styles['SubHeader']))
|
|
|
|
| 1058 |
for review in positive_reviews:
|
| 1059 |
elements.append(Paragraph(f'"{review}..."', styles['Quote']))
|
| 1060 |
else:
|
| 1061 |
+
elements.append(Paragraph("Detailed positive feedback samples not available.", styles['CustomBody']))
|
| 1062 |
|
| 1063 |
elements.append(Spacer(1, 15))
|
| 1064 |
|
|
|
|
| 1067 |
for review in negative_reviews:
|
| 1068 |
elements.append(Paragraph(f'"{review}..."', styles['Quote']))
|
| 1069 |
else:
|
| 1070 |
+
elements.append(Paragraph("No significant negative feedback identified. Great job!", styles['CustomBody']))
|
| 1071 |
|
| 1072 |
# ==================== FOOTER ====================
|
| 1073 |
elements.append(Spacer(1, 30))
|
| 1074 |
elements.append(HRFlowable(width="100%", thickness=1, color=BORDER, spaceBefore=10, spaceAfter=10))
|
| 1075 |
+
elements.append(Paragraph(f"Report generated for {restaurant_name}", styles['CustomFooter']))
|
| 1076 |
+
elements.append(Paragraph(f"Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}", styles['CustomFooter']))
|
| 1077 |
+
elements.append(Paragraph("Restaurant Intelligence Agent β’ Powered by Claude AI", styles['CustomFooter']))
|
| 1078 |
+
elements.append(Paragraph("Β© 2025 - Built for Anthropic MCP Hackathon", styles['CustomFooter']))
|
| 1079 |
|
| 1080 |
# Build PDF
|
| 1081 |
doc.build(elements)
|
|
|
|
| 1174 |
|
| 1175 |
|
| 1176 |
def find_relevant_reviews(question: str, state: dict, top_k: int = 8) -> List[str]:
|
| 1177 |
+
"""Find relevant reviews for the question using menu/aspect related_reviews."""
|
| 1178 |
if not state:
|
| 1179 |
return []
|
| 1180 |
|
|
|
|
| 1183 |
|
| 1184 |
menu = state.get('menu_analysis', {})
|
| 1185 |
aspects = state.get('aspect_analysis', {})
|
|
|
|
| 1186 |
|
| 1187 |
all_items = menu.get('food_items', []) + menu.get('drinks', [])
|
| 1188 |
all_aspects = aspects.get('aspects', [])
|
|
|
|
| 1224 |
if text not in relevant_reviews:
|
| 1225 |
relevant_reviews.append(text)
|
| 1226 |
|
| 1227 |
+
# Fallback: if still no reviews, gather from all items/aspects
|
| 1228 |
+
if not relevant_reviews:
|
| 1229 |
+
# Collect from top items by mentions
|
| 1230 |
+
sorted_items = sorted(all_items, key=lambda x: x.get('mention_count', 0), reverse=True)
|
| 1231 |
+
for item in sorted_items[:5]:
|
| 1232 |
+
for r in item.get('related_reviews', [])[:2]:
|
| 1233 |
+
text = r.get('review_text', str(r)) if isinstance(r, dict) else str(r)
|
| 1234 |
+
if text and len(text) > 20 and text not in relevant_reviews:
|
|
|
|
|
|
|
| 1235 |
relevant_reviews.append(text)
|
| 1236 |
|
| 1237 |
+
# Also from top aspects
|
| 1238 |
+
sorted_aspects = sorted(all_aspects, key=lambda x: x.get('mention_count', 0), reverse=True)
|
| 1239 |
+
for aspect in sorted_aspects[:5]:
|
| 1240 |
+
for r in aspect.get('related_reviews', [])[:2]:
|
| 1241 |
+
text = r.get('review_text', str(r)) if isinstance(r, dict) else str(r)
|
| 1242 |
+
if text and len(text) > 20 and text not in relevant_reviews:
|
|
|
|
|
|
|
| 1243 |
relevant_reviews.append(text)
|
| 1244 |
|
| 1245 |
return relevant_reviews[:top_k]
|
|
|
|
| 1367 |
# ============================================================================
|
| 1368 |
|
| 1369 |
def analyze_restaurant(url: str, review_count: int):
|
| 1370 |
+
"""Main analysis function - calls Modal API with robust error handling."""
|
| 1371 |
|
| 1372 |
empty = {}
|
| 1373 |
default_summary = "Run analysis to see performance overview."
|
|
|
|
| 1401 |
|
| 1402 |
try:
|
| 1403 |
print(f"[ANALYZE] {platform_emoji} Analyzing {restaurant_name} from {platform}...")
|
| 1404 |
+
print(f"[ANALYZE] Calling Modal API: {MODAL_API_URL}/analyze")
|
| 1405 |
+
|
| 1406 |
+
# Use a session with retry logic
|
| 1407 |
+
import requests
|
| 1408 |
+
from requests.adapters import HTTPAdapter
|
| 1409 |
+
from urllib3.util.retry import Retry
|
| 1410 |
+
|
| 1411 |
+
session = requests.Session()
|
| 1412 |
+
retries = Retry(
|
| 1413 |
+
total=2,
|
| 1414 |
+
backoff_factor=1,
|
| 1415 |
+
status_forcelist=[502, 503, 504],
|
| 1416 |
+
allowed_methods=["POST"]
|
| 1417 |
+
)
|
| 1418 |
+
session.mount("https://", HTTPAdapter(max_retries=retries))
|
| 1419 |
|
| 1420 |
+
# Make request with streaming disabled for stability
|
| 1421 |
+
response = session.post(
|
| 1422 |
f"{MODAL_API_URL}/analyze",
|
| 1423 |
json={"url": url, "max_reviews": review_count},
|
| 1424 |
+
timeout=(30, 600), # 30s connect, 600s read (10 min)
|
| 1425 |
+
headers={"Connection": "keep-alive"}
|
| 1426 |
)
|
| 1427 |
|
| 1428 |
+
print(f"[ANALYZE] Response status: {response.status_code}")
|
| 1429 |
+
|
| 1430 |
if response.status_code != 200:
|
| 1431 |
+
error_text = response.text[:500] if response.text else "No error details"
|
| 1432 |
+
print(f"[ANALYZE] Error response: {error_text}")
|
| 1433 |
return (
|
| 1434 |
+
f"β **API Error ({response.status_code}):** {error_text[:200]}",
|
| 1435 |
None, "No trend data available.",
|
| 1436 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1437 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1438 |
empty
|
| 1439 |
)
|
| 1440 |
|
| 1441 |
+
# Parse response
|
| 1442 |
+
try:
|
| 1443 |
+
data = response.json()
|
| 1444 |
+
print(f"[ANALYZE] Response received, success={data.get('success')}")
|
| 1445 |
+
except Exception as json_err:
|
| 1446 |
+
print(f"[ANALYZE] JSON parse error: {json_err}")
|
| 1447 |
+
return (
|
| 1448 |
+
f"β **Parse Error:** Could not parse API response. Try again.",
|
| 1449 |
+
None, "No trend data available.",
|
| 1450 |
+
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1451 |
+
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1452 |
+
empty
|
| 1453 |
+
)
|
| 1454 |
|
| 1455 |
if not data.get("success"):
|
| 1456 |
return (
|
|
|
|
| 1464 |
menu = data.get('menu_analysis', {})
|
| 1465 |
aspects = data.get('aspect_analysis', {})
|
| 1466 |
insights = data.get('insights', {})
|
| 1467 |
+
|
| 1468 |
+
# Use slim trend_data (pre-calculated sentiment, no text)
|
| 1469 |
+
# Falls back to raw_reviews for backward compatibility
|
| 1470 |
+
trend_data = data.get('trend_data', data.get('raw_reviews', []))
|
| 1471 |
|
| 1472 |
food_items = menu.get('food_items', [])
|
| 1473 |
drinks = menu.get('drinks', [])
|
| 1474 |
aspect_list = aspects.get('aspects', [])
|
| 1475 |
all_menu = food_items + drinks
|
| 1476 |
|
| 1477 |
+
print(f"[ANALYZE] Data extracted: {len(all_menu)} menu items, {len(aspect_list)} aspects, {len(trend_data)} trend points")
|
| 1478 |
+
|
| 1479 |
state = {
|
| 1480 |
"menu_analysis": menu,
|
| 1481 |
"aspect_analysis": aspects,
|
| 1482 |
"insights": insights,
|
| 1483 |
"restaurant_name": restaurant_name,
|
| 1484 |
+
"trend_data": trend_data, # Store for PDF if needed
|
| 1485 |
"source": platform
|
| 1486 |
}
|
| 1487 |
|
| 1488 |
+
trend_chart = generate_trend_chart(trend_data, restaurant_name)
|
| 1489 |
+
trend_insight = generate_trend_insight(trend_data, restaurant_name)
|
| 1490 |
|
| 1491 |
# Use improved detailed summaries
|
| 1492 |
menu_summary = translate_menu_performance(menu, restaurant_name)
|
|
|
|
| 1511 |
|
| 1512 |
**π Summary:**
|
| 1513 |
β’ Source: **{platform.replace('_', ' ').title()}**
|
| 1514 |
+
β’ Reviews analyzed: **{len(trend_data)}**
|
| 1515 |
β’ Menu items found: **{len(all_menu)}** ({len(food_items)} food, {len(drinks)} drinks)
|
| 1516 |
β’ Aspects discovered: **{len(aspect_list)}**
|
| 1517 |
|
| 1518 |
π **Explore the tabs below for detailed insights!**
|
| 1519 |
"""
|
| 1520 |
|
| 1521 |
+
print(f"[ANALYZE] β
Analysis complete for {restaurant_name}")
|
| 1522 |
+
|
| 1523 |
return (
|
| 1524 |
status,
|
| 1525 |
trend_chart, trend_insight,
|
|
|
|
| 1529 |
)
|
| 1530 |
|
| 1531 |
except requests.exceptions.Timeout:
|
| 1532 |
+
print("[ANALYZE] β Timeout error")
|
| 1533 |
return (
|
| 1534 |
"β **Timeout:** Request took too long. Try with fewer reviews (50-100).",
|
| 1535 |
None, "No trend data available.",
|
|
|
|
| 1537 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1538 |
empty
|
| 1539 |
)
|
| 1540 |
+
except requests.exceptions.ConnectionError as ce:
|
| 1541 |
+
print(f"[ANALYZE] β Connection error: {ce}")
|
| 1542 |
+
return (
|
| 1543 |
+
"β **Connection Error:** Could not reach analysis server. Please try again in a moment.",
|
| 1544 |
+
None, "No trend data available.",
|
| 1545 |
+
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1546 |
+
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1547 |
+
empty
|
| 1548 |
+
)
|
| 1549 |
except Exception as e:
|
| 1550 |
import traceback
|
| 1551 |
traceback.print_exc()
|
| 1552 |
+
print(f"[ANALYZE] β Exception: {e}")
|
| 1553 |
return (
|
| 1554 |
+
f"β **Error:** {str(e)[:200]}",
|
| 1555 |
None, "No trend data available.",
|
| 1556 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|
| 1557 |
default_summary, None, default_insight, empty_dropdown, default_detail,
|