Spaces:
Runtime error
Runtime error
import gradio as gr | |
import supabase | |
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
client = supabase.create_client( | |
"https://tmjhrfjckqnlvqnsspnr.supabase.co", | |
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InRtamhyZmpja3FubHZxbnNzcG5yIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjExMjE1NTgsImV4cCI6MjAzNjY5NzU1OH0.E34R6qPWavp2uRWKinZQICgdEqRjov46VnE38F24Al8" | |
) | |
def read_data(): | |
response = client.table('Customer_purchase_dataset').select("*").execute() | |
df = pd.DataFrame(response.data) | |
return df | |
df= read_data() | |
#print(df.head) | |
#print(df.dtypes) | |
# Convert Gender to categorical | |
df['Gender'] = df['Gender'].map({0: 'Female', 1: 'Male'}) | |
# Convert LoyaltyProgram to categorical | |
df['LoyaltyProgram'] = df['LoyaltyProgram'].map({0: 'No', 1: 'Yes'}) | |
# Convert PurchaseStatus to categorical | |
df['PurchaseStatus'] = df['PurchaseStatus'].map({0: 'Not Purchased', 1: 'Purchased'}) | |
# Function to create histogram | |
def create_histogram(column): | |
plt.figure(figsize=(10, 6)) | |
sns.histplot(data=df, x=column, kde=True) | |
plt.title(f'Histogram of {column}') | |
plt.xlabel(column) | |
plt.ylabel('Count') | |
return plt | |
# Function to create scatter plot | |
def create_scatter(x_column, y_column, hue_column): | |
plt.figure(figsize=(10, 6)) | |
sns.scatterplot(data=df, x=x_column, y=y_column, hue=hue_column) | |
plt.title(f'{x_column} vs {y_column} (colored by {hue_column})') | |
plt.xlabel(x_column) | |
plt.ylabel(y_column) | |
return plt | |
# Function to create box plot | |
def create_boxplot(x_column, y_column): | |
plt.figure(figsize=(10, 6)) | |
sns.boxplot(data=df, x=x_column, y=y_column) | |
plt.title(f'Box Plot of {y_column} by {x_column}') | |
plt.xlabel(x_column) | |
plt.ylabel(y_column) | |
return plt | |
# Function to create bar plot | |
def create_barplot(x_column, y_column): | |
plt.figure(figsize=(10, 6)) | |
sns.barplot(data=df, x=x_column, y=y_column) | |
plt.title(f'Bar Plot of {y_column} by {x_column}') | |
plt.xlabel(x_column) | |
plt.ylabel(y_column) | |
plt.xticks(rotation=45) | |
return plt | |
# Gradio interface | |
def visualize(plot_type, x_column, y_column, hue_column): | |
if plot_type == "Histogram": | |
return create_histogram(x_column) | |
elif plot_type == "Scatter Plot": | |
return create_scatter(x_column, y_column, hue_column) | |
elif plot_type == "Box Plot": | |
return create_boxplot(x_column, y_column) | |
elif plot_type == "Bar Plot": | |
return create_barplot(x_column, y_column) | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=visualize, | |
inputs=[ | |
gr.Dropdown(["Histogram", "Scatter Plot", "Box Plot", "Bar Plot"], label="Plot Type"), | |
gr.Dropdown(df.columns.tolist(), label="X-axis"), | |
gr.Dropdown(df.columns.tolist(), label="Y-axis"), | |
gr.Dropdown(df.columns.tolist(), label="Hue (for Scatter Plot)") | |
], | |
outputs="plot", | |
title="Customer Purchase Data Visualization Dashboard", | |
description="Explore the customer purchase dataset through various visualizations." | |
) | |
# Launch the interface | |
iface.launch() | |