File size: 3,460 Bytes
899f23c
 
9db8932
899f23c
 
3ab2d73
899f23c
3ab2d73
899f23c
2ea100e
 
9db8932
 
 
 
 
 
 
3ab2d73
9db8932
 
 
 
 
899f23c
 
96d55af
95dede4
 
96d55af
 
 
3ab2d73
 
 
 
 
96d55af
95dede4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d55af
899f23c
 
2ea100e
9db8932
674b979
 
 
39b3671
 
674b979
 
899f23c
 
 
 
 
674b979
899f23c
 
 
 
 
 
674b979
899f23c
 
 
95dede4
 
 
 
 
 
 
899f23c
95dede4
899f23c
95dede4
899f23c
 
 
 
 
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
import gradio as gr
import pandas as pd
from datetime import datetime

# Placeholder for boba data
boba_data = pd.DataFrame(columns=["Date", "Shop", "Drink", "Toppings", "Size", "Sugar", "Rating", "Price"])

def submit_boba_data(use_today, date, shop, drink, toppings, size, sugar, rating, price):
    global boba_data
    if use_today:
        date = datetime.today().strftime('%Y-%m-%d')
    
    new_entry = pd.DataFrame([{
        "Date": date,
        "Shop": shop,
        "Drink": drink,
        "Toppings": toppings,
        "Size": size,
        "Sugar": sugar,
        "Rating": rating,
        "Price": price
    }])
    
    boba_data = pd.concat([boba_data, new_entry], ignore_index=True)
    return boba_data

def get_leaderboard():
    leaderboard = boba_data.groupby('Shop').agg({'Rating': 'mean', 'Shop': 'count'}).rename(columns={'Shop': 'Number of Entries'}).sort_values(by='Rating', ascending=False)
    return leaderboard.reset_index()

def get_statistics():
    total_drinks = len(boba_data)
    if total_drinks > 0:
        favorite_drink = boba_data['Drink'].mode()[0]
        favorite_topping = boba_data['Toppings'].mode()[0]
        average_sugar = boba_data['Sugar'].mean()
        total_spent = boba_data['Price'].sum()

        # Plotting
        fig, axs = plt.subplots(1, 2, figsize=(12, 5))

        # Plot 1: Sugar Distribution
        sns.histplot(boba_data['Sugar'], bins=10, kde=True, ax=axs[0])
        axs[0].set_title('Sugar Distribution')
        axs[0].set_xlabel('Sugar (%)')
        axs[0].set_ylabel('Frequency')

        # Plot 2: Rating Distribution
        sns.histplot(boba_data['Rating'], bins=5, kde=True, ax=axs[1])
        axs[1].set_title('Rating Distribution')
        axs[1].set_xlabel('Rating')
        axs[1].set_ylabel('Frequency')

        # Save plots to BytesIO and convert to image
        img = BytesIO()
        plt.savefig(img, format='png')
        plt.close(fig)
        img.seek(0)
        
        return (f"Total drinks: {total_drinks}\n"
                f"Favorite drink: {favorite_drink}\n"
                f"Favorite topping: {favorite_topping}\n"
                f"Average sugar level: {average_sugar:.2f}%\n"
                f"Total spent: ${total_spent:.2f}", img.getvalue())
    else:
        return "No data available", None

# Define Gradio inputs and interface
inputs = [
    gr.Checkbox(label="Use today's date?"),
    gr.Textbox(label="Date (YYYY-MM-DD)"),
    gr.Textbox(label="Shop"),
    gr.Textbox(label="Drink"),
    gr.Textbox(label="Toppings"),
    gr.Dropdown(["Medium", "Large"], label="Size"),
    gr.Slider(0, 200, label="Sugar (%)", step=25),
    gr.Slider(1, 5, label="Rating"),
    gr.Number(label="Price")
]

iface = gr.Interface(
    fn=submit_boba_data, 
    inputs=inputs, 
    outputs=gr.Dataframe(),
    title="Boba Tracker Data Entry"
)

leaderboard_iface = gr.Interface(
    fn=get_leaderboard,
    inputs=None,
    outputs=gr.Dataframe(),
    title="Boba Leaderboard"
)

def display_statistics():
    stats_text, stats_img = get_statistics()
    if stats_img:
        return [stats_text, gr.Image(value=stats_img)]
    else:
        return stats_text

stats_iface = gr.Interface(
    fn=display_statistics,
    inputs=None,
    outputs=[gr.Textbox(), gr.Image()],
    title="Boba Consumption Stats"
)

# Combine all interfaces into one app
gr.TabbedInterface([iface, leaderboard_iface, stats_iface], ["Data Entry", "Leaderboard", "Statistics"]).launch()