AdrienVDS05 commited on
Commit
2a77874
Β·
verified Β·
1 Parent(s): 87cf88a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -34
app.py CHANGED
@@ -95,42 +95,27 @@ def make_chart(row, filtered_df):
95
 
96
 
97
  # ── AI explanation (rule-based, no API key needed) ─────────
 
 
98
  def explain(row):
99
- name = row["restaurant_name"]
100
- label = row["worth_it_label"]
101
- score = row["true_score"]
102
- price = row["price_category"]
103
- sent = row["sentiment_score"]
104
- stars = row["star_rating"]
105
- vols = row["sentiment_volatility"]
106
- pf = row["price_fairness"]
107
-
108
- sentiment_word = "positive" if sent > 0.2 else ("neutral" if sent > -0.2 else "negative")
109
- volatility_note = (
110
- " Reviews are polarised β€” strong opinions on both sides."
111
- if vols > 0.5 else
112
- " Reviews are consistent."
113
- )
114
- value_note = (
115
- f" At {price}, it delivers excellent value for money."
116
- if pf > 0.6 else
117
- f" At {price}, the price-to-experience ratio is fair."
118
- if pf > 0.3 else
119
- f" At {price}, some diners feel it falls short of expectations."
120
- )
121
 
122
- if label == "Worth It":
123
- verdict = f"This restaurant scores {score:.0f}/100 and is genuinely worth a visit."
124
- elif label == "Maybe":
125
- verdict = f"This restaurant scores {score:.0f}/100 β€” a solid option depending on your preferences."
126
- else:
127
- verdict = f"This restaurant scores {score:.0f}/100 β€” consider alternatives before booking."
128
-
129
- return (
130
- f"**{name}** β€” _{label}_\n\n"
131
- f"{verdict} "
132
- f"Customer reviews are broadly {sentiment_word} (raw sentiment: {sent:+.2f}), "
133
- f"and the star rating is {stars:.1f}/5.{volatility_note}{value_note}"
134
  )
135
 
136
 
 
95
 
96
 
97
  # ── AI explanation (rule-based, no API key needed) ─────────
98
+ import requests
99
+
100
  def explain(row):
101
+ payload = {
102
+ "restaurant_name": row["restaurant_name"],
103
+ "true_score": float(row["true_score"]),
104
+ "worth_it_label": row["worth_it_label"],
105
+ "sentiment_score": float(row["sentiment_score"]),
106
+ "star_rating": float(row["star_rating"]),
107
+ "price_category": row["price_category"],
108
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ try:
111
+ r = requests.post(
112
+ "https://scohen3012.app.n8n.cloud/webhook/explain",
113
+ json=payload,
114
+ timeout=8
115
+ )
116
+ return r.json().get("explanation", "No explanation returned.")
117
+ except Exception as e:
118
+ return f"Explanation service unavailable: {e}"
 
 
 
119
  )
120
 
121