Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import matplotlib
|
|
| 5 |
matplotlib.use("Agg")
|
| 6 |
import seaborn as sns
|
| 7 |
import requests
|
| 8 |
-
import io
|
| 9 |
from pathlib import Path
|
| 10 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 11 |
|
|
@@ -22,27 +21,8 @@ except Exception:
|
|
| 22 |
df_sales = pd.DataFrame()
|
| 23 |
|
| 24 |
# ─────────────────────────────────────────
|
| 25 |
-
# SECTION 2 — Analyse en temps réel
|
| 26 |
# ─────────────────────────────────────────
|
| 27 |
-
analyzer = SentimentIntensityAnalyzer()
|
| 28 |
-
|
| 29 |
-
def get_sentiment_label(text):
|
| 30 |
-
score = analyzer.polarity_scores(text)["compound"]
|
| 31 |
-
if score >= 0.05:
|
| 32 |
-
return "positive"
|
| 33 |
-
elif score <= -0.05:
|
| 34 |
-
return "negative"
|
| 35 |
-
else:
|
| 36 |
-
return "neutral"
|
| 37 |
-
|
| 38 |
-
def pricing_decision(avg_units, positive_ratio, negative_ratio):
|
| 39 |
-
if avg_units >= 120 and positive_ratio >= 0.6:
|
| 40 |
-
return "📈 Increase Price"
|
| 41 |
-
elif avg_units <= 60 and negative_ratio >= 0.4:
|
| 42 |
-
return "📉 Decrease Price"
|
| 43 |
-
else:
|
| 44 |
-
return "➡️ Keep Price"
|
| 45 |
-
|
| 46 |
def analyze_book(title, reviews_text, avg_units_sold):
|
| 47 |
if not title or not reviews_text:
|
| 48 |
return "⚠️ Please enter a title and at least one review.", "", None
|
|
@@ -58,11 +38,76 @@ def analyze_book(title, reviews_text, avg_units_sold):
|
|
| 58 |
try:
|
| 59 |
response = requests.post(url, json=payload)
|
| 60 |
|
| 61 |
-
summary =
|
| 62 |
-
📚
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
```text
|
| 68 |
-
{response.text}
|
|
|
|
| 5 |
matplotlib.use("Agg")
|
| 6 |
import seaborn as sns
|
| 7 |
import requests
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 10 |
|
|
|
|
| 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
|
|
|
|
| 38 |
try:
|
| 39 |
response = requests.post(url, json=payload)
|
| 40 |
|
| 41 |
+
summary = (
|
| 42 |
+
f"📚 {title}\n\n"
|
| 43 |
+
f"Status code: {response.status_code}\n\n"
|
| 44 |
+
f"Raw response:\n{response.text}"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return summary, "Debug mode", None
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error: {str(e)}", "Request failed", None
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# ─────────────────────────────────────────
|
| 54 |
+
# SECTION 3 — Interface Gradio
|
| 55 |
+
# ─────────────────────────────────────────
|
| 56 |
+
with gr.Blocks(title="📚 Book Price Decider", theme=gr.themes.Soft()) as app:
|
| 57 |
+
|
| 58 |
+
gr.Markdown("# 📚 Book Price Decider — Group A4")
|
| 59 |
+
gr.Markdown("Sentiment analysis + ARIMA-based pricing decisions for books.")
|
| 60 |
+
|
| 61 |
+
with gr.Tabs():
|
| 62 |
+
|
| 63 |
+
# 📊 Dashboard
|
| 64 |
+
with gr.Tab("📊 Dashboard"):
|
| 65 |
+
gr.Markdown("### Pre-computed results from the analysis notebooks")
|
| 66 |
+
|
| 67 |
+
if ARTIFACTS_OK:
|
| 68 |
+
with gr.Row():
|
| 69 |
+
gr.Image("artifacts/sales_trends.png", label="Sales Trends")
|
| 70 |
+
gr.Image("artifacts/sentiment_distribution.png", label="Sentiment Distribution")
|
| 71 |
+
|
| 72 |
+
gr.Dataframe(value=df_pricing, label="Pricing Decisions Table")
|
| 73 |
+
else:
|
| 74 |
+
gr.Markdown("⚠️ No artifacts found yet.")
|
| 75 |
+
|
| 76 |
+
# 🔮 Analyse
|
| 77 |
+
with gr.Tab("🔮 Analyze a New Book"):
|
| 78 |
+
gr.Markdown("### Enter book info to get a live pricing recommendation")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
title_input = gr.Textbox(label="Book Title")
|
| 82 |
+
units_input = gr.Number(label="Avg Monthly Units Sold", value=100)
|
| 83 |
+
|
| 84 |
+
reviews_input = gr.Textbox(label="Reviews (one per line)", lines=6)
|
| 85 |
+
|
| 86 |
+
analyze_btn = gr.Button("🚀 Analyze & Decide")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
summary_output = gr.Textbox(label="Summary")
|
| 90 |
+
details_output = gr.Textbox(label="Details")
|
| 91 |
+
|
| 92 |
+
chart_output = gr.Plot()
|
| 93 |
+
|
| 94 |
+
analyze_btn.click(
|
| 95 |
+
fn=analyze_book,
|
| 96 |
+
inputs=[title_input, reviews_input, units_input],
|
| 97 |
+
outputs=[summary_output, details_output, chart_output]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# ℹ️ About
|
| 101 |
+
with gr.Tab("ℹ️ About"):
|
| 102 |
+
gr.Markdown("""
|
| 103 |
+
## About this app
|
| 104 |
|
| 105 |
+
AI project using:
|
| 106 |
+
- Data scraping
|
| 107 |
+
- Synthetic data
|
| 108 |
+
- Sentiment analysis
|
| 109 |
+
- ARIMA forecasting
|
| 110 |
+
- Pricing decision system
|
| 111 |
+
""")
|
| 112 |
|
| 113 |
+
app.launch()
|
|
|
|
|
|