File size: 29,112 Bytes
1b9d20b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
"""
AUTOMATION 2 (UPGRADED) β€” Hugging Face Spaces App
==================================================
Improvements over v1:
  βœ“ LLM (GPT-4o-mini) called DIRECTLY from inside the app
  βœ“ Richer interactive visualisations (radar chart, trend bars, gauge)
  βœ“ Side-by-side metric comparison panel
  βœ“ Session history tracker
  βœ“ Automated pipeline trigger button (runs agentic_pipeline.py)
  βœ“ Confidence intervals on predictions
  βœ“ Better UX: loading states, cleaner layout, collapsible AI section

Deploy on Hugging Face Spaces (SDK: Gradio).
Set HF Secret: OPENAI_API_KEY
"""

import os
import json
import time
import subprocess
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import warnings
warnings.filterwarnings("ignore")

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

try:
    import requests
    REQUESTS_OK = True
except ImportError:
    REQUESTS_OK = False

# ── CONFIG ──────────────────────────────────────────────────
OPENAI_KEY = os.environ.get("OPENAI_API_KEY", "")   # Set as HF Secret
GPT_MODEL  = "gpt-4o-mini"

PALETTE = {
    "blue":   "#2E86AB",
    "pink":   "#A23B72",
    "amber":  "#F18F01",
    "red":    "#C73E1D",
    "teal":   "#44BBA4",
    "light":  "#F5F5F5",
    "dark":   "#1A1A2E",
}

# ── STARTUP: TRAIN MODELS ───────────────────────────────────
print("Loading data and training models on startup...")

def _load_and_train_amazon():
    df = pd.read_csv("amazon_synthetic.csv")
    df["log_sales"] = np.log1p(df["rating_count"])
    features = ["actual_price", "discounted_price", "discount_pct", "rating", "sentiment_score"]
    X = df[features].dropna()
    y = df.loc[X.index, "log_sales"]
    rf = RandomForestRegressor(n_estimators=150, random_state=42)
    rf.fit(X, y)
    # Compute prediction std via individual trees for confidence interval
    return rf, features, df

def _load_and_train_spotify():
    df = pd.read_csv("spotify_synthetic.csv")
    df["explicit"] = df["explicit"].astype(int)
    features = ["danceability", "energy", "loudness", "speechiness",
                "acousticness", "instrumentalness", "valence", "tempo", "explicit"]
    X = df[features].dropna()
    y = df.loc[X.index, "popularity"]
    rf = RandomForestRegressor(n_estimators=150, random_state=42)
    rf.fit(X, y)
    return rf, features, df

try:
    rf_amz, features_amz, df_amz = _load_and_train_amazon()
    AMZ_OK = True
    print("βœ“ Amazon model ready")
except Exception as e:
    AMZ_OK = False
    print(f"βœ— Amazon model failed: {e}")

try:
    rf_spot, features_spot, df_spot = _load_and_train_spotify()
    SPOT_OK = True
    print("βœ“ Spotify model ready")
except Exception as e:
    SPOT_OK = False
    print(f"βœ— Spotify model failed: {e}")

analyzer = SentimentIntensityAnalyzer()

# Session history
session_history = []


# ════════════════════════════════════════════════════════════
# GPT HELPER β€” called directly from the app
# ════════════════════════════════════════════════════════════

def call_gpt_in_app(system_prompt: str, user_prompt: str, max_tokens=500) -> str:
    """
    Call GPT-4o-mini directly from within the Gradio app.
    Falls back to a template report if API key is not set.
    """
    if not OPENAI_KEY or not REQUESTS_OK:
        return None   # will use fallback below

    headers = {
        "Authorization": f"Bearer {OPENAI_KEY}",
        "Content-Type": "application/json",
    }
    payload = {
        "model": GPT_MODEL,
        "messages": [
            {"role": "system", "content": system_prompt},
            {"role": "user",   "content": user_prompt},
        ],
        "temperature": 0.4,
        "max_tokens": max_tokens,
    }
    try:
        r = requests.post(
            "https://api.openai.com/v1/chat/completions",
            headers=headers, json=payload, timeout=25
        )
        r.raise_for_status()
        return r.json()["choices"][0]["message"]["content"]
    except Exception as e:
        return f"[GPT unavailable: {e}]"


def get_amazon_gpt_insight(category, actual_price, discounted_price, discount_pct,
                           rating, sentiment_score, sentiment_label, sales_pred, score):
    system = (
        "You are a senior e-commerce performance analyst. Given Amazon product metrics, "
        "write a concise 4-section report: (1) Performance verdict in 1 sentence, "
        "(2) Pricing strategy assessment referencing the exact discount%, "
        "(3) Sentiment interpretation referencing the exact score, "
        "(4) Two specific, actionable recommendations. "
        "Be data-driven. Reference every number provided. Keep total response under 200 words."
    )
    user = (
        f"Category: {category} | Actual price: β‚Ή{actual_price:.0f} | "
        f"Discounted price: β‚Ή{discounted_price:.0f} | Discount: {discount_pct}% | "
        f"Rating: {rating}/5 | Sentiment score: {sentiment_score:.3f} ({sentiment_label}) | "
        f"Predicted rating count: ~{sales_pred:,} | Performance score: {score}/100"
    )
    result = call_gpt_in_app(system, user)
    if result and not result.startswith("[GPT"):
        return "πŸ€– AI Analysis (GPT-4o-mini)\n" + "─" * 36 + "\n" + result
    # Fallback
    return (
        "πŸ€– AI Analysis (template fallback β€” set OPENAI_API_KEY for live GPT)\n"
        + "─" * 36 + "\n"
        f"1. Performance: This {category} product scores {score}/100 β€” "
        f"{'strong' if score >= 75 else 'average' if score >= 45 else 'underperforming'}.\n"
        f"2. Pricing: A {discount_pct}% discount brings the price from β‚Ή{actual_price:.0f} to "
        f"β‚Ή{discounted_price:.0f}. {'This aggressive discount may signal lower quality.' if discount_pct > 50 else 'Moderate discount maintains perceived value.'}\n"
        f"3. Sentiment: Score of {sentiment_score:.3f} is {sentiment_label}. "
        f"{'Strong reviews support organic growth.' if sentiment_label == 'Positive' else 'Negative sentiment risks algorithmic deprioritisation.'}\n"
        f"4. Recommendations:\n"
        f"   β€’ {'Leverage positive reviews in sponsored ads' if sentiment_label == 'Positive' else 'Address negative feedback within 48h'}\n"
        f"   β€’ {'Reduce discount to 20–30% to protect margin' if discount_pct > 50 else 'Maintain current pricing strategy'}"
    )


def get_spotify_gpt_insight(genre, danceability, energy, loudness, tempo,
                             valence, acousticness, pop_pred, tier):
    system = (
        "You are a music industry data analyst. Given Spotify audio features, "
        "write a concise 4-section report: (1) Commercial potential verdict in 1 sentence, "
        "(2) Audio profile assessment β€” is it radio-friendly? Reference exact feature values, "
        "(3) Genre fit analysis, "
        "(4) Two specific promotional or production recommendations. "
        "Be data-driven. Reference every number. Under 200 words total."
    )
    user = (
        f"Genre: {genre} | Popularity prediction: {pop_pred:.1f}/100 ({tier}) | "
        f"Danceability: {danceability:.2f} | Energy: {energy:.2f} | Loudness: {loudness:.1f} dB | "
        f"Tempo: {tempo:.0f} BPM | Valence: {valence:.2f} | Acousticness: {acousticness:.2f}"
    )
    result = call_gpt_in_app(system, user)
    if result and not result.startswith("[GPT"):
        return "πŸ€– AI Analysis (GPT-4o-mini)\n" + "─" * 36 + "\n" + result
    return (
        "πŸ€– AI Analysis (template fallback β€” set OPENAI_API_KEY for live GPT)\n"
        + "─" * 36 + "\n"
        f"1. Commercial potential: This {genre} track scores {pop_pred:.1f}/100 β€” {tier}.\n"
        f"2. Audio profile: Danceability {danceability:.2f} + energy {energy:.2f} at {loudness:.1f} dB. "
        f"{'Radio-friendly profile.' if danceability > 0.6 and energy > 0.6 else 'Niche profile β€” limited mainstream appeal.'}\n"
        f"3. Genre fit: {'Aligns with' if pop_pred >= 50 else 'Partially aligns with'} {genre} conventions.\n"
        f"4. Recommendations:\n"
        f"   β€’ {'Pitch to editorial playlists β€” strong commercial profile' if pop_pred >= 60 else 'Consider a remix to boost danceability'}\n"
        f"   β€’ {'Capitalize on high energy for live and sync licensing' if energy >= 0.7 else 'Explore streaming-first promotional strategy'}"
    )


# ════════════════════════════════════════════════════════════
# VISUALISATION HELPERS
# ════════════════════════════════════════════════════════════

def _radar_chart(labels, values, title, color):
    """Create a radar (spider) chart for audio features."""
    n = len(labels)
    angles = np.linspace(0, 2 * np.pi, n, endpoint=False).tolist()
    values_loop = values + [values[0]]
    angles += angles[:1]

    fig, ax = plt.subplots(figsize=(4.5, 4.5), subplot_kw={"polar": True})
    fig.patch.set_facecolor("#FAFAFA")
    ax.set_facecolor("#F0F4F8")
    ax.plot(angles, values_loop, color=color, linewidth=2)
    ax.fill(angles, values_loop, color=color, alpha=0.25)
    ax.set_xticks(angles[:-1])
    ax.set_xticklabels(labels, fontsize=9)
    ax.set_ylim(0, 1)
    ax.set_yticks([0.25, 0.5, 0.75])
    ax.set_yticklabels(["0.25", "0.50", "0.75"], fontsize=7, color="gray")
    ax.set_title(title, fontsize=11, fontweight="bold", pad=15)
    ax.grid(color="white", linewidth=0.8)
    plt.tight_layout()
    return fig


def make_amazon_chart(rating, sentiment_score, discount_pct, score, sales_pred):
    import tempfile
    fig, axes = plt.subplots(1, 3, figsize=(14, 4.5))
    fig.patch.set_facecolor("#FAFAFA")
    fig.suptitle("Amazon Product β€” Performance Dashboard", fontsize=13, fontweight="bold", y=1.01)

    # Panel 1: Feature bars
    ax = axes[0]
    ax.set_facecolor("#F8F9FA")
    metrics  = ["Rating (/5)", "Sentiment", "Discount (%/100)", "Score (/100)"]
    values   = [rating / 5, (sentiment_score + 1) / 2, discount_pct / 100, score / 100]
    bar_cols = [PALETTE["blue"], PALETTE["teal"], PALETTE["amber"], PALETTE["pink"]]
    bars = ax.bar(metrics, values, color=bar_cols, edgecolor="white", width=0.6)
    ax.set_ylim(0, 1.15)
    ax.set_title("Key Metrics (normalised)", fontweight="bold")
    for bar, val in zip(bars, values):
        ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 0.025,
                f"{val:.2f}", ha="center", fontsize=10, fontweight="bold")
    ax.set_xticklabels(metrics, fontsize=9)

    # Panel 2: Gauge
    ax2 = axes[1]
    ax2.set_facecolor("#F8F9FA")
    tier_color = (PALETTE["teal"] if score >= 75 else
                  PALETTE["amber"] if score >= 45 else PALETTE["red"])
    tier = "Top Performer" if score >= 75 else "Average" if score >= 45 else "Underperformer"
    wedge_colors = [tier_color, "#E8E8E8"]
    ax2.pie([score, 100 - score], colors=wedge_colors, startangle=90,
            wedgeprops={"edgecolor": "white", "linewidth": 2})
    ax2.text(0, 0, f"{score}", ha="center", va="center",
             fontsize=28, fontweight="bold", color=tier_color)
    ax2.set_title(f"Score: {tier}", fontweight="bold")

    # Panel 3: Est. rating count vs category benchmarks (synthetic)
    ax3 = axes[2]
    ax3.set_facecolor("#F8F9FA")
    benchmarks = {
        "This product":     sales_pred,
        "Category avg":    int(df_amz["rating_count"].mean()) if AMZ_OK else 15000,
        "Top 10%":         int(df_amz["rating_count"].quantile(0.9)) if AMZ_OK else 50000,
    }
    bc = [PALETTE["pink"], PALETTE["blue"], PALETTE["blue"]]
    ax3.barh(list(benchmarks.keys()), list(benchmarks.values()),
             color=bc, edgecolor="white")
    ax3.set_title("Est. Sales vs Benchmarks", fontweight="bold")
    ax3.set_xlabel("Predicted Rating Count")

    plt.tight_layout()
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    plt.savefig(tmp.name, dpi=130, bbox_inches="tight", facecolor="#FAFAFA")
    plt.close()
    return tmp.name


def make_spotify_chart(danceability, energy, loudness, tempo, valence,
                       acousticness, speechiness, pop_pred, genre):
    import tempfile
    fig = plt.figure(figsize=(14, 4.5))
    fig.patch.set_facecolor("#FAFAFA")
    fig.suptitle("Spotify Track β€” Audio Profile Dashboard", fontsize=13, fontweight="bold")

    # Panel 1: Radar
    ax1 = fig.add_subplot(1, 3, 1, polar=True)
    labels = ["Dance", "Energy", "Valence", "Acoust.", "Speech"]
    vals   = [danceability, energy, valence, acousticness, speechiness]
    n = len(labels)
    angles = np.linspace(0, 2 * np.pi, n, endpoint=False).tolist()
    vals_loop   = vals + [vals[0]]
    angles_loop = angles + angles[:1]
    ax1.plot(angles_loop, vals_loop, color=PALETTE["blue"], linewidth=2)
    ax1.fill(angles_loop, vals_loop, color=PALETTE["blue"], alpha=0.25)
    ax1.set_xticks(angles)
    ax1.set_xticklabels(labels, fontsize=9)
    ax1.set_ylim(0, 1)
    ax1.set_yticks([0.25, 0.5, 0.75])
    ax1.set_yticklabels(["", "", ""], fontsize=7)
    ax1.set_title("Audio Radar", fontweight="bold", pad=14)
    ax1.set_facecolor("#F0F4F8")
    ax1.grid(color="white")

    # Panel 2: Gauge
    ax2 = fig.add_subplot(1, 3, 2)
    ax2.set_facecolor("#F8F9FA")
    tier = ("Hit πŸ”₯" if pop_pred >= 70 else "Popular" if pop_pred >= 50
            else "Mid-tier" if pop_pred >= 30 else "Niche")
    tier_color = (PALETTE["red"] if pop_pred >= 70 else
                  PALETTE["teal"] if pop_pred >= 50 else
                  PALETTE["amber"] if pop_pred >= 30 else "#888")
    ax2.pie([pop_pred, 100 - pop_pred], colors=[tier_color, "#E8E8E8"],
            startangle=90, wedgeprops={"edgecolor": "white", "linewidth": 2})
    ax2.text(0, 0, f"{pop_pred:.0f}", ha="center", va="center",
             fontsize=28, fontweight="bold", color=tier_color)
    ax2.set_title(f"Popularity: {tier}", fontweight="bold")

    # Panel 3: Feature importance comparison (from model)
    ax3 = fig.add_subplot(1, 3, 3)
    ax3.set_facecolor("#F8F9FA")
    if SPOT_OK:
        imp = pd.Series(rf_spot.feature_importances_, index=features_spot).sort_values()
        ax3.barh(imp.index, imp.values, color=PALETTE["blue"], edgecolor="white")
        ax3.set_title("Feature Importance\n(model weights)", fontweight="bold")
        ax3.set_xlabel("Importance")
    else:
        ax3.text(0.5, 0.5, "Model not loaded", ha="center")

    plt.tight_layout()
    tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    plt.savefig(tmp.name, dpi=130, bbox_inches="tight", facecolor="#FAFAFA")
    plt.close()
    return tmp.name


# ════════════════════════════════════════════════════════════
# AMAZON ANALYSIS FUNCTION
# ════════════════════════════════════════════════════════════

def analyze_amazon(category, actual_price, discount_pct, rating, review_text, use_gpt):
    discounted_price = actual_price * (1 - discount_pct / 100)
    sentiment_score  = analyzer.polarity_scores(review_text)["compound"] if review_text else 0.0
    sentiment_label  = ("Positive" if sentiment_score >= 0.05
                        else "Negative" if sentiment_score <= -0.05 else "Neutral")

    if AMZ_OK:
        X = np.array([[actual_price, discounted_price, discount_pct, rating, sentiment_score]])
        # Confidence interval via individual tree predictions
        tree_preds = np.array([t.predict(X)[0] for t in rf_amz.estimators_])
        log_pred   = tree_preds.mean()
        log_std    = tree_preds.std()
        sales_pred = int(np.expm1(log_pred))
        sales_low  = int(np.expm1(max(0, log_pred - log_std)))
        sales_high = int(np.expm1(log_pred + log_std))
    else:
        sales_pred = int(rating * 1000 * (1 + sentiment_score))
        sales_low  = int(sales_pred * 0.7)
        sales_high = int(sales_pred * 1.3)

    score = min(100, int(
        25 * (rating / 5) +
        25 * ((sentiment_score + 1) / 2) +
        25 * min(sales_pred / 50000, 1) +
        25 * min(discount_pct / 70, 1)
    ))
    tier = ("Top Performer" if score >= 75 else "Average" if score >= 45 else "Underperformer")

    # Chart
    chart_path = make_amazon_chart(rating, sentiment_score, discount_pct, score, sales_pred)

    # Text report
    report = (
        f"πŸ“¦  AMAZON PRODUCT ANALYSIS\n{'═'*42}\n"
        f"Category:          {category}\n"
        f"Actual Price:      β‚Ή{actual_price:.0f}\n"
        f"Discounted Price:  β‚Ή{discounted_price:.0f}  (βˆ’{discount_pct}%)\n"
        f"Rating:            {rating}/5\n"
        f"{'─'*42}\n"
        f"SENTIMENT\n"
        f"  Score:  {sentiment_score:+.3f}   Label: {sentiment_label}\n"
        f"{'─'*42}\n"
        f"PREDICTED SALES\n"
        f"  Est. Reviews: ~{sales_pred:,}\n"
        f"  90% Range:    {sales_low:,} – {sales_high:,}\n"
        f"{'─'*42}\n"
        f"PERFORMANCE SCORE:  {score}/100  ({tier})\n"
    )

    # GPT or fallback
    gpt_section = ""
    if use_gpt:
        gpt_section = "\n" + get_amazon_gpt_insight(
            category, actual_price, discounted_price, discount_pct,
            rating, sentiment_score, sentiment_label, sales_pred, score
        )

    session_history.append({
        "platform": "Amazon", "category": category,
        "score": score, "tier": tier,
        "timestamp": time.strftime("%H:%M:%S"),
    })

    return report.strip() + gpt_section, chart_path


# ════════════════════════════════════════════════════════════
# SPOTIFY ANALYSIS FUNCTION
# ════════════════════════════════════════════════════════════

def analyze_spotify(genre, danceability, energy, loudness, tempo, valence,
                    acousticness, speechiness, instrumentalness, explicit, use_gpt):
    exp = int(explicit)

    if SPOT_OK:
        X = np.array([[danceability, energy, loudness, speechiness, acousticness,
                       instrumentalness, valence, tempo, exp]])
        tree_preds = np.array([t.predict(X)[0] for t in rf_spot.estimators_])
        pop_pred   = float(np.clip(tree_preds.mean(), 0, 100))
        pop_std    = tree_preds.std()
    else:
        pop_pred = float(np.clip(20 + 30*danceability + 15*energy + 0.5*(loudness+20), 0, 100))
        pop_std  = 5.0

    tier = ("Hit πŸ”₯" if pop_pred >= 70 else "Popular" if pop_pred >= 50
            else "Mid-tier" if pop_pred >= 30 else "Niche")
    pop_low  = max(0,   pop_pred - pop_std)
    pop_high = min(100, pop_pred + pop_std)

    chart_path = make_spotify_chart(
        danceability, energy, loudness, tempo, valence,
        acousticness, speechiness, pop_pred, genre
    )

    report = (
        f"🎡  SPOTIFY TRACK ANALYSIS\n{'═'*42}\n"
        f"Genre:          {genre}\n"
        f"Tempo:          {tempo:.0f} BPM\n"
        f"Explicit:       {'Yes' if explicit else 'No'}\n"
        f"{'─'*42}\n"
        f"AUDIO FEATURES\n"
        f"  Danceability:   {danceability:.3f}\n"
        f"  Energy:         {energy:.3f}\n"
        f"  Loudness:       {loudness:.1f} dB\n"
        f"  Valence:        {valence:.3f}\n"
        f"  Acousticness:   {acousticness:.3f}\n"
        f"  Speechiness:    {speechiness:.3f}\n"
        f"{'─'*42}\n"
        f"PREDICTED POPULARITY\n"
        f"  Score:  {pop_pred:.1f}/100  ({tier})\n"
        f"  Range:  {pop_low:.1f} – {pop_high:.1f}  (Β±1 std dev)\n"
    )

    gpt_section = ""
    if use_gpt:
        gpt_section = "\n" + get_spotify_gpt_insight(
            genre, danceability, energy, loudness, tempo,
            valence, acousticness, pop_pred, tier
        )

    session_history.append({
        "platform": "Spotify", "genre": genre,
        "score": round(pop_pred, 1), "tier": tier,
        "timestamp": time.strftime("%H:%M:%S"),
    })

    return report.strip() + gpt_section, chart_path


# ════════════════════════════════════════════════════════════
# SESSION HISTORY & PIPELINE TRIGGER
# ════════════════════════════════════════════════════════════

def get_history():
    if not session_history:
        return "No analyses run yet this session."
    lines = [f"{'#':<4} {'Time':<10} {'Platform':<10} {'Detail':<25} {'Score':<8} {'Tier'}"]
    lines.append("─" * 70)
    for i, h in enumerate(session_history[-10:], 1):
        detail = h.get("category", h.get("genre", "β€”"))
        lines.append(f"{i:<4} {h['timestamp']:<10} {h['platform']:<10} {detail:<25} {h['score']:<8} {h['tier']}")
    return "\n".join(lines)


def run_pipeline():
    """Trigger the agentic pipeline from the UI."""
    if not os.path.exists("agentic_pipeline.py"):
        return "agentic_pipeline.py not found in current directory."
    try:
        result = subprocess.run(
            ["python3", "agentic_pipeline.py", "--mode", "both"],
            capture_output=True, text=True, timeout=120
        )
        out = result.stdout[-2000:] if len(result.stdout) > 2000 else result.stdout
        if result.returncode == 0:
            return f"βœ“ Pipeline completed successfully.\n\n{out}"
        else:
            return f"βœ— Pipeline error:\n{result.stderr[:1000]}"
    except subprocess.TimeoutExpired:
        return "βœ— Pipeline timed out after 120s."
    except Exception as e:
        return f"βœ— Could not run pipeline: {e}"


# ════════════════════════════════════════════════════════════
# GRADIO INTERFACE
# ════════════════════════════════════════════════════════════

CUSTOM_CSS = """
.gr-button-primary { background: #2E86AB !important; border: none !important; }
.gr-button-secondary { border: 1px solid #2E86AB !important; color: #2E86AB !important; }
footer { display: none !important; }
"""

with gr.Blocks(
    title="AI Performance Analyzer β€” Amazon Γ— Spotify",
    theme=gr.themes.Soft(primary_hue="blue", secondary_hue="pink"),
    css=CUSTOM_CSS,
) as demo:

    gr.Markdown("""
    # πŸ€– AI Performance Analyzer
    ### Amazon Products Γ— Spotify Tracks
    *Real-time ML predictions + GPT-4o-mini insights from a single interface*
    """)

    with gr.Tabs():

        # ── TAB 1: AMAZON ────────────────────────────────────
        with gr.TabItem("πŸ›’ Amazon Product"):
            gr.Markdown("### Predict product sales performance and get AI-powered strategy insights")
            with gr.Row():
                with gr.Column(scale=1):
                    amz_category = gr.Dropdown(
                        ["Electronics", "Clothing", "HomeKitchen", "Books",
                         "Sports", "Beauty", "Toys", "OfficeProducts", "MusicalInstruments"],
                        label="Product Category", value="Electronics")
                    amz_actual   = gr.Slider(50, 80000, value=999, step=50,
                                             label="Actual Price (β‚Ή)")
                    amz_discount = gr.Slider(0, 80, value=30, step=1,
                                             label="Discount %")
                    amz_rating   = gr.Slider(1.0, 5.0, value=4.2, step=0.1,
                                             label="Star Rating (/5)")
                    amz_review   = gr.Textbox(
                        label="Sample Review Text",
                        value="Great product, works perfectly and arrived on time!",
                        lines=3, placeholder="Enter a customer review for sentiment analysis...")
                    amz_gpt = gr.Checkbox(label="πŸ€– Generate GPT-4o-mini AI insight", value=True)
                    amz_btn = gr.Button("Analyze Product", variant="primary", size="lg")

                with gr.Column(scale=2):
                    amz_output = gr.Textbox(label="Analysis Report", lines=22)
                    amz_plot   = gr.Image(label="Performance Dashboard", type="filepath")

            amz_btn.click(
                analyze_amazon,
                inputs=[amz_category, amz_actual, amz_discount, amz_rating, amz_review, amz_gpt],
                outputs=[amz_output, amz_plot],
            )

        # ── TAB 2: SPOTIFY ───────────────────────────────────
        with gr.TabItem("🎡 Spotify Track"):
            gr.Markdown("### Predict commercial success and get AI-powered music industry insights")
            with gr.Row():
                with gr.Column(scale=1):
                    sp_genre  = gr.Dropdown(
                        ["pop", "hip-hop", "rock", "electronic", "jazz",
                         "r-n-b", "country", "latin", "indie", "classical"],
                        label="Genre", value="pop")
                    sp_dance  = gr.Slider(0.0, 1.0, value=0.70, step=0.01, label="Danceability")
                    sp_energy = gr.Slider(0.0, 1.0, value=0.80, step=0.01, label="Energy")
                    sp_loud   = gr.Slider(-40, 0, value=-7, step=0.5, label="Loudness (dB)")
                    sp_tempo  = gr.Slider(60, 200, value=120, step=1, label="Tempo (BPM)")
                    sp_val    = gr.Slider(0.0, 1.0, value=0.60, step=0.01, label="Valence (mood positivity)")
                    sp_acou   = gr.Slider(0.0, 1.0, value=0.10, step=0.01, label="Acousticness")
                    sp_speech = gr.Slider(0.0, 1.0, value=0.05, step=0.01, label="Speechiness")
                    sp_instr  = gr.Slider(0.0, 1.0, value=0.00, step=0.01, label="Instrumentalness")
                    sp_exp    = gr.Checkbox(label="Explicit content", value=False)
                    sp_gpt    = gr.Checkbox(label="πŸ€– Generate GPT-4o-mini AI insight", value=True)
                    sp_btn    = gr.Button("Analyze Track", variant="primary", size="lg")

                with gr.Column(scale=2):
                    sp_output = gr.Textbox(label="Analysis Report", lines=22)
                    sp_plot   = gr.Image(label="Audio Profile Dashboard", type="filepath")

            sp_btn.click(
                analyze_spotify,
                inputs=[sp_genre, sp_dance, sp_energy, sp_loud, sp_tempo,
                        sp_val, sp_acou, sp_speech, sp_instr, sp_exp, sp_gpt],
                outputs=[sp_output, sp_plot],
            )

        # ── TAB 3: SESSION HISTORY ───────────────────────────
        with gr.TabItem("πŸ“‹ Session History"):
            gr.Markdown("### All analyses run this session")
            hist_output = gr.Textbox(label="Session Log", lines=15)
            hist_btn = gr.Button("Refresh History", variant="secondary")
            hist_btn.click(get_history, inputs=[], outputs=[hist_output])

        # ── TAB 4: PIPELINE ──────────────────────────────────
        with gr.TabItem("βš™οΈ Agentic Pipeline"):
            gr.Markdown("""
            ### Automated End-to-End Pipeline
            Runs the full agentic pipeline: data ingestion β†’ synthetic generation β†’
            model training β†’ inference β†’ report generation. Single-command execution.
            """)
            pipe_btn    = gr.Button("β–Ά Run Agentic Pipeline", variant="primary", size="lg")
            pipe_output = gr.Textbox(label="Pipeline Output", lines=20)
            pipe_btn.click(run_pipeline, inputs=[], outputs=[pipe_output])

    gr.Markdown("""
    ---
    *Built with Gradio Β· Models: Random Forest (sklearn) Β· NLP: VADER Β· AI: GPT-4o-mini*
    *Set `OPENAI_API_KEY` as a Hugging Face Secret to enable live GPT insights*
    """)

if __name__ == "__main__":
    demo.launch(share=True)