Matteo08 commited on
Commit
e730ebb
·
verified ·
1 Parent(s): 85b56ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -62
app.py CHANGED
@@ -1,43 +1,41 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
  import matplotlib
5
  matplotlib.use("Agg")
6
- import seaborn as sns
7
  import requests
8
- from pathlib import Path
9
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
10
 
11
- # ─────────────────────────────────────────
12
- # SECTION 1 — Charger les données pré-calculées
13
- # ─────────────────────────────────────────
14
  try:
15
  df_pricing = pd.read_csv("artifacts/pricing_decisions.csv")
16
- df_sales = pd.read_csv("artifacts/dashboard_data.csv")
17
  ARTIFACTS_OK = True
18
  except Exception:
19
- ARTIFACTS_OK = False
20
  df_pricing = pd.DataFrame()
21
- df_sales = pd.DataFrame()
 
22
 
23
- # ─────────────────────────────────────────
24
- # SECTION 2 — Analyse en temps réel (n8n)
25
- # ─────────────────────────────────────────
26
  def analyze_book(title, reviews_text, avg_units_sold):
27
  if not title or not reviews_text:
28
- return "⚠️ Please enter a title and at least one review.", "", None
29
 
30
  url = "https://matteoadam.app.n8n.cloud/webhook/price-decider"
31
 
32
  payload = {
33
  "title": title,
34
  "reviews": reviews_text,
35
- "avg_units_sold": avg_units_sold
36
  }
37
 
38
  try:
39
- response = requests.post(url, json=payload)
40
- data = response.json()
 
 
 
 
 
 
 
 
41
 
42
  if isinstance(data, list) and len(data) > 0:
43
  result = data[0]
@@ -45,7 +43,7 @@ def analyze_book(title, reviews_text, avg_units_sold):
45
  result = data
46
 
47
  summary = (
48
- f"📚 {title}\n\n"
49
  f"Pricing Decision: {result.get('pricing_decision', 'Error')}\n"
50
  f"Sentiment: {result.get('sentiment', 'N/A')}"
51
  )
@@ -56,64 +54,35 @@ def analyze_book(title, reviews_text, avg_units_sold):
56
  return f"Error: {str(e)}", "Request failed", None
57
 
58
 
59
- # ─────────────────────────────────────────
60
- # SECTION 3Interface Gradio
61
- # ─────────────────────────────────────────
62
- with gr.Blocks(title="📚 Book Price Decider", theme=gr.themes.Soft()) as app:
63
-
64
- gr.Markdown("# 📚 Book Price Decider — Group A4")
65
- gr.Markdown("Sentiment analysis + ARIMA-based pricing decisions for books.")
66
 
67
  with gr.Tabs():
68
-
69
- # 📊 Dashboard
70
- with gr.Tab("📊 Dashboard"):
71
- gr.Markdown("### Pre-computed results from the analysis notebooks")
72
-
73
  if ARTIFACTS_OK:
74
- with gr.Row():
75
- gr.Image("artifacts/sales_trends.png", label="Sales Trends")
76
- gr.Image("artifacts/sentiment_distribution.png", label="Sentiment Distribution")
77
-
78
  gr.Dataframe(value=df_pricing, label="Pricing Decisions Table")
79
  else:
80
- gr.Markdown("⚠️ No artifacts found yet.")
81
-
82
- # 🔮 Analyse
83
- with gr.Tab("🔮 Analyze a New Book"):
84
- gr.Markdown("### Enter book info to get a live pricing recommendation")
85
-
86
- with gr.Row():
87
- title_input = gr.Textbox(label="Book Title")
88
- units_input = gr.Number(label="Avg Monthly Units Sold", value=100)
89
 
 
 
 
90
  reviews_input = gr.Textbox(label="Reviews (one per line)", lines=6)
91
 
92
- analyze_btn = gr.Button("🚀 Analyze & Decide")
93
-
94
- with gr.Row():
95
- summary_output = gr.Textbox(label="Summary")
96
- details_output = gr.Textbox(label="Details")
97
 
98
- chart_output = gr.Plot()
 
 
99
 
100
  analyze_btn.click(
101
  fn=analyze_book,
102
  inputs=[title_input, reviews_input, units_input],
103
- outputs=[summary_output, details_output, chart_output]
104
  )
105
 
106
- # ℹ️ About
107
- with gr.Tab("ℹ️ About"):
108
- gr.Markdown("""
109
- ## About this app
110
-
111
- AI project using:
112
- - Data scraping
113
- - Synthetic data
114
- - Sentiment analysis
115
- - ARIMA forecasting
116
- - Pricing decision system
117
- """)
118
 
119
  app.launch()
 
1
  import gradio as gr
2
  import pandas as pd
 
3
  import matplotlib
4
  matplotlib.use("Agg")
 
5
  import requests
 
 
6
 
7
+ # Charger les artifacts si présents
 
 
8
  try:
9
  df_pricing = pd.read_csv("artifacts/pricing_decisions.csv")
 
10
  ARTIFACTS_OK = True
11
  except Exception:
 
12
  df_pricing = pd.DataFrame()
13
+ ARTIFACTS_OK = False
14
+
15
 
 
 
 
16
  def analyze_book(title, reviews_text, avg_units_sold):
17
  if not title or not reviews_text:
18
+ return "Please enter a title and at least one review.", "", None
19
 
20
  url = "https://matteoadam.app.n8n.cloud/webhook/price-decider"
21
 
22
  payload = {
23
  "title": title,
24
  "reviews": reviews_text,
25
+ "avg_units_sold": avg_units_sold,
26
  }
27
 
28
  try:
29
+ response = requests.post(url, json=payload, timeout=30)
30
+
31
+ try:
32
+ data = response.json()
33
+ except Exception:
34
+ return (
35
+ f"Status code: {response.status_code}\nRaw response:\n{response.text}",
36
+ "Non-JSON response",
37
+ None,
38
+ )
39
 
40
  if isinstance(data, list) and len(data) > 0:
41
  result = data[0]
 
43
  result = data
44
 
45
  summary = (
46
+ f"Book: {title}\n\n"
47
  f"Pricing Decision: {result.get('pricing_decision', 'Error')}\n"
48
  f"Sentiment: {result.get('sentiment', 'N/A')}"
49
  )
 
54
  return f"Error: {str(e)}", "Request failed", None
55
 
56
 
57
+ with gr.Blocks(title="Book Price Decider") as app:
58
+ gr.Markdown("# Book Price Decider Group A4")
59
+ gr.Markdown("Sentiment analysis + pricing recommendation")
 
 
 
 
60
 
61
  with gr.Tabs():
62
+ with gr.Tab("Dashboard"):
 
 
 
 
63
  if ARTIFACTS_OK:
 
 
 
 
64
  gr.Dataframe(value=df_pricing, label="Pricing Decisions Table")
65
  else:
66
+ gr.Markdown("No artifacts found yet.")
 
 
 
 
 
 
 
 
67
 
68
+ with gr.Tab("Analyze a New Book"):
69
+ title_input = gr.Textbox(label="Book Title")
70
+ units_input = gr.Number(label="Avg Monthly Units Sold", value=100)
71
  reviews_input = gr.Textbox(label="Reviews (one per line)", lines=6)
72
 
73
+ analyze_btn = gr.Button("Analyze")
 
 
 
 
74
 
75
+ summary_output = gr.Textbox(label="Summary", lines=6)
76
+ details_output = gr.Textbox(label="Details", lines=2)
77
+ chart_output = gr.Plot(label="Chart")
78
 
79
  analyze_btn.click(
80
  fn=analyze_book,
81
  inputs=[title_input, reviews_input, units_input],
82
+ outputs=[summary_output, details_output, chart_output],
83
  )
84
 
85
+ with gr.Tab("About"):
86
+ gr.Markdown("AI for Big Data Management project app.")
 
 
 
 
 
 
 
 
 
 
87
 
88
  app.launch()