ktara commited on
Commit
ebea030
·
verified ·
1 Parent(s): 10dafba

Upload 5 files

Browse files
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Session5
3
- emoji: 🏃
4
- colorFrom: red
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.9.0
8
  app_file: app.py
9
- pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
1
  ---
2
+ title: Book Analytics Dashboard
3
+ emoji: 📊
 
 
4
  sdk: gradio
 
5
  app_file: app.py
 
6
  ---
7
 
8
+ # Book Analytics Dashboard
9
+
10
+ Run full analytics on fixed datasets.
11
+
12
+ Click "Run Analysis" to generate results.
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI-Assisted Code — Academic Integrity Notice
2
+ # Generated with The App Builder. ESCP coursework.
3
+ # Student must be able to explain all code when asked.
4
+
5
+ import gradio as gr
6
+ import pandas as pd
7
+ import matplotlib.pyplot as plt
8
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
9
+ from statsmodels.tsa.arima.model import ARIMA
10
+
11
+ def load_data():
12
+ reviews = pd.read_csv("synthetic_book_reviews.csv")
13
+ sales = pd.read_csv("synthetic_sales_data.csv")
14
+ return reviews, sales
15
+
16
+ def run_sentiment_analysis(df):
17
+ analyzer = SentimentIntensityAnalyzer()
18
+ def get_sentiment(text):
19
+ score = analyzer.polarity_scores(str(text))["compound"]
20
+ if score >= 0.05:
21
+ return "Positive"
22
+ elif score <= -0.05:
23
+ return "Negative"
24
+ return "Neutral"
25
+ df["sentiment"] = df["review"].apply(get_sentiment)
26
+ return df
27
+
28
+ def create_sentiment_plot(df):
29
+ fig, ax = plt.subplots()
30
+ df["sentiment"].value_counts().plot(kind="bar", ax=ax)
31
+ ax.set_title("Sentiment Distribution")
32
+ filepath = "sentiment_plot.png"
33
+ fig.savefig(filepath)
34
+ plt.close(fig)
35
+ return filepath
36
+
37
+ def forecast_sales(df):
38
+ df["date"] = pd.to_datetime(df["date"])
39
+ df = df.sort_values("date")
40
+ model = ARIMA(df["sales"], order=(1, 1, 1))
41
+ model_fit = model.fit()
42
+ return model_fit.forecast(steps=5)
43
+
44
+ def pricing_decision(sentiment_df, forecast):
45
+ sentiment_score = sentiment_df["sentiment"].value_counts(normalize=True)
46
+ positive_ratio = sentiment_score.get("Positive", 0)
47
+ avg_forecast = forecast.mean()
48
+ decision = "Increase Price" if positive_ratio > 0.6 else "Keep Price"
49
+ result = pd.DataFrame({
50
+ "Positive Sentiment Ratio": [positive_ratio],
51
+ "Avg Forecast Sales": [avg_forecast],
52
+ "Decision": [decision]
53
+ })
54
+ result.to_csv("pricing_decision.csv", index=False)
55
+ return result
56
+
57
+ def run_full_analysis():
58
+ reviews, sales = load_data()
59
+ reviews = run_sentiment_analysis(reviews)
60
+ plot_path = create_sentiment_plot(reviews)
61
+ forecast = forecast_sales(sales)
62
+ decision_df = pricing_decision(reviews, forecast)
63
+ return plot_path, decision_df, "pricing_decision.csv"
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# 📊 Automated Book Analytics Dashboard")
67
+ run_button = gr.Button("Run Analysis")
68
+ plot_output = gr.Image()
69
+ table_output = gr.Dataframe()
70
+ file_output = gr.File()
71
+ run_button.click(fn=run_full_analysis, inputs=[], outputs=[plot_output, table_output, file_output])
72
+
73
+ if __name__ == "__main__":
74
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==4.36.1
2
+ pandas==2.2.2
3
+ matplotlib==3.8.4
4
+ vaderSentiment==3.3.2
5
+ statsmodels==0.14.2
synthetic_book_reviews.csv ADDED
The diff for this file is too large to render. See raw diff
 
synthetic_sales_data.csv ADDED
The diff for this file is too large to render. See raw diff