Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import requests | |
| WEBHOOK_URL = "https://eliabigdata.app.n8n.cloud/webhook/restaurant-analyzer" | |
| restaurants = ['PastaPlace', 'BurgerHub', 'SushiBar', 'TacoLoco', 'CafeBleu'] | |
| np.random.seed(42) | |
| df_sales = pd.DataFrame({ | |
| 'restaurant': np.repeat(restaurants, 20), | |
| 'week': list(range(1, 21)) * 5, | |
| 'revenue': np.random.randint(3000, 15000, 100), | |
| 'avg_price': np.random.uniform(12, 45, 100).round(2) | |
| }) | |
| def analyze_restaurant(restaurant, review): | |
| try: | |
| # Send to n8n webhook | |
| response = requests.post(WEBHOOK_URL, json={ | |
| "restaurant": restaurant, | |
| "review": review | |
| }, timeout=10) | |
| if response.status_code == 200: | |
| data = response.json() | |
| sentiment = data.get('sentiment', 'Neutral') | |
| recommendation = data.get('recommendation', 'Keep current strategy') | |
| else: | |
| raise Exception("Webhook error") | |
| except: | |
| # Fallback local analysis | |
| positive_words = ['amazing', 'great', 'fantastic', 'loved', 'best', 'delicious'] | |
| negative_words = ['terrible', 'bad', 'disappointing', 'never', 'worst', 'expensive'] | |
| review_lower = review.lower() | |
| pos = sum(w in review_lower for w in positive_words) | |
| neg = sum(w in review_lower for w in negative_words) | |
| if pos > neg: | |
| sentiment = "Positive" | |
| recommendation = "Increase prices by 10%" | |
| elif neg > pos: | |
| sentiment = "Negative" | |
| recommendation = "Improve service first" | |
| else: | |
| sentiment = "Neutral" | |
| recommendation = "Keep current strategy" | |
| stats = df_sales[df_sales['restaurant'] == restaurant]['revenue'] | |
| avg_rev = stats.mean().round(2) | |
| result = f""" | |
| π½οΈ Restaurant: {restaurant} | |
| π Sentiment: {sentiment} | |
| π° Average Weekly Revenue: β¬{avg_rev} | |
| π‘ Recommendation: {recommendation} | |
| """ | |
| return result | |
| iface = gr.Interface( | |
| fn=analyze_restaurant, | |
| inputs=[ | |
| gr.Dropdown(choices=restaurants, label="Select Restaurant"), | |
| gr.Textbox(label="Enter a customer review", | |
| placeholder="e.g. Amazing food, loved it!") | |
| ], | |
| outputs=gr.Textbox(label="Analysis & Recommendation"), | |
| title="π½οΈ Restaurant Performance Analyzer", | |
| description="Analyze customer sentiment via n8n webhook and get pricing recommendations" | |
| ) | |
| iface.launch() |