Spaces:
Runtime error
Runtime error
harikrishnad1997
commited on
Commit
•
03371ed
1
Parent(s):
fb3a99d
Update app.py
Browse files
app.py
CHANGED
@@ -5,53 +5,40 @@ import matplotlib.pyplot as plt
|
|
5 |
from io import StringIO
|
6 |
|
7 |
# Function to plot histogram
|
8 |
-
def plot_histogram(file_contents, column):
|
9 |
# Read the CSV file
|
10 |
custom_df = pd.read_csv(StringIO(file_contents))
|
11 |
|
12 |
# Plot histogram
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
plt.ylabel('Frequency')
|
18 |
-
return plt
|
19 |
|
20 |
# Function to plot scatter plot
|
21 |
-
def plot_scatter(file_contents, x_axis, y_axis):
|
22 |
# Read the CSV file
|
23 |
custom_df = pd.read_csv(StringIO(file_contents))
|
24 |
|
25 |
# Plot scatter plot
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
plt.ylabel(y_axis)
|
31 |
-
return plt
|
32 |
-
|
33 |
-
def layout_fn(inputs):
|
34 |
-
# Get the data from uploaded file
|
35 |
-
data = pd.read_csv(StringIO(inputs.file))
|
36 |
-
|
37 |
-
# Get selected columns from dropdown options
|
38 |
-
column_options = list(data.columns)
|
39 |
-
selected_column = inputs.text if inputs.text else None
|
40 |
-
selected_x_axis = inputs.text_1 if inputs.text_1 else None
|
41 |
-
selected_y_axis = inputs.text_2 if inputs.text_2 else None
|
42 |
|
|
|
43 |
# Create the figure with subplots
|
44 |
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
|
45 |
|
46 |
# Check if data is uploaded and a column is selected for histogram
|
47 |
-
if
|
48 |
-
plot_histogram(
|
49 |
else:
|
50 |
axes[0].text(0.5, 0.5, "Upload a CSV and select a column", ha='center', va='center')
|
51 |
|
52 |
# Check if data is uploaded and both x and y columns are selected for scatter plot
|
53 |
-
if
|
54 |
-
plot_scatter(
|
55 |
else:
|
56 |
axes[1].text(0.5, 0.5, "Upload a CSV, select X and Y columns", ha='center', va='center')
|
57 |
|
@@ -60,32 +47,31 @@ def layout_fn(inputs):
|
|
60 |
plt.tight_layout()
|
61 |
return fig
|
62 |
|
63 |
-
|
64 |
# Create the Gradio interface
|
65 |
interface = gr.Interface(
|
66 |
fn=layout_fn,
|
67 |
inputs=[
|
68 |
-
gr.
|
69 |
-
gr.
|
70 |
-
gr.
|
71 |
-
gr.
|
72 |
],
|
73 |
outputs="plot",
|
74 |
title="Data Visualization Tool",
|
75 |
description="Upload a CSV file, select columns for histogram and scatter plots.",
|
76 |
)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
if inputs.file:
|
82 |
-
data = pd.read_csv(StringIO(inputs.file.getvalue()))
|
83 |
choices = list(data.columns)
|
84 |
-
interface.
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
88 |
)
|
89 |
|
90 |
-
|
91 |
-
interface.launch(share=True,fn_change=update_choices)
|
|
|
5 |
from io import StringIO
|
6 |
|
7 |
# Function to plot histogram
|
8 |
+
def plot_histogram(file_contents, column, ax=None):
|
9 |
# Read the CSV file
|
10 |
custom_df = pd.read_csv(StringIO(file_contents))
|
11 |
|
12 |
# Plot histogram
|
13 |
+
sns.histplot(custom_df[column], ax=ax)
|
14 |
+
ax.set_title(f'Histogram for {column}')
|
15 |
+
ax.set_xlabel(column)
|
16 |
+
ax.set_ylabel('Frequency')
|
|
|
|
|
17 |
|
18 |
# Function to plot scatter plot
|
19 |
+
def plot_scatter(file_contents, x_axis, y_axis, ax=None):
|
20 |
# Read the CSV file
|
21 |
custom_df = pd.read_csv(StringIO(file_contents))
|
22 |
|
23 |
# Plot scatter plot
|
24 |
+
sns.scatterplot(x=x_axis, y=y_axis, data=custom_df, ax=ax)
|
25 |
+
ax.set_title(f'Scatter Plot ({x_axis} vs {y_axis})')
|
26 |
+
ax.set_xlabel(x_axis)
|
27 |
+
ax.set_ylabel(y_axis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
def layout_fn(file, text, text_1, text_2):
|
30 |
# Create the figure with subplots
|
31 |
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
|
32 |
|
33 |
# Check if data is uploaded and a column is selected for histogram
|
34 |
+
if file and text:
|
35 |
+
plot_histogram(file.getvalue(), text, ax=axes[0])
|
36 |
else:
|
37 |
axes[0].text(0.5, 0.5, "Upload a CSV and select a column", ha='center', va='center')
|
38 |
|
39 |
# Check if data is uploaded and both x and y columns are selected for scatter plot
|
40 |
+
if file and text_1 and text_2:
|
41 |
+
plot_scatter(file.getvalue(), text_1, text_2, ax=axes[1])
|
42 |
else:
|
43 |
axes[1].text(0.5, 0.5, "Upload a CSV, select X and Y columns", ha='center', va='center')
|
44 |
|
|
|
47 |
plt.tight_layout()
|
48 |
return fig
|
49 |
|
|
|
50 |
# Create the Gradio interface
|
51 |
interface = gr.Interface(
|
52 |
fn=layout_fn,
|
53 |
inputs=[
|
54 |
+
gr.inputs.File(label="Upload CSV file"),
|
55 |
+
gr.inputs.Dropdown(label="Select Column (Histogram)", choices=[]),
|
56 |
+
gr.inputs.Dropdown(label="Select X-axis (Scatter)", choices=[]),
|
57 |
+
gr.inputs.Dropdown(label="Select Y-axis (Scatter)", choices=[]),
|
58 |
],
|
59 |
outputs="plot",
|
60 |
title="Data Visualization Tool",
|
61 |
description="Upload a CSV file, select columns for histogram and scatter plots.",
|
62 |
)
|
63 |
|
64 |
+
def update_choices(file):
|
65 |
+
if file:
|
66 |
+
data = pd.read_csv(StringIO(file.getvalue()))
|
|
|
|
|
67 |
choices = list(data.columns)
|
68 |
+
interface.set_config(
|
69 |
+
inputs=[
|
70 |
+
gr.inputs.File(label="Upload CSV file"),
|
71 |
+
gr.inputs.Dropdown(label="Select Column (Histogram)", choices=choices),
|
72 |
+
gr.inputs.Dropdown(label="Select X-axis (Scatter)", choices=choices),
|
73 |
+
gr.inputs.Dropdown(label="Select Y-axis (Scatter)", choices=choices),
|
74 |
+
]
|
75 |
)
|
76 |
|
77 |
+
interface.run(share=True,fn_change=update_choices)
|
|