Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
# enable pandas <-> R dataframe conversion
|
| 7 |
-
pandas2ri.activate()
|
| 8 |
-
|
| 9 |
-
# load R package
|
| 10 |
-
eulerr = importr("eulerr")
|
| 11 |
|
| 12 |
def make_venn(file):
|
| 13 |
try:
|
| 14 |
-
#
|
| 15 |
df = pd.read_csv(file.name)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
if df.shape[1] < 2:
|
| 19 |
-
return "CSV must contain: primary key + up to 3 category columns."
|
| 20 |
-
|
| 21 |
-
# drop primary key
|
| 22 |
categories = df.columns[1:]
|
|
|
|
|
|
|
| 23 |
if len(categories) > 3:
|
| 24 |
-
return "Error: Only up to 3
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# save plot
|
| 33 |
out_file = "venn.png"
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
r("dev.off")()
|
| 37 |
-
|
| 38 |
return out_file
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error: {e}"
|
| 42 |
|
| 43 |
-
# gradio interface
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=make_venn,
|
| 46 |
inputs=gr.File(file_types=[".csv"], label="Upload CSV"),
|
| 47 |
-
outputs=gr.Image(type="filepath", label="
|
| 48 |
-
title="
|
| 49 |
-
description="Upload a CSV with a primary key column + up to 3 category columns
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from matplotlib_venn import venn2, venn3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def make_venn(file):
|
| 7 |
try:
|
| 8 |
+
# Load CSV
|
| 9 |
df = pd.read_csv(file.name)
|
| 10 |
|
| 11 |
+
# Drop primary key (first column)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
categories = df.columns[1:]
|
| 13 |
+
if len(categories) < 2:
|
| 14 |
+
return "Error: Need at least 2 category columns!"
|
| 15 |
if len(categories) > 3:
|
| 16 |
+
return "Error: Only up to 3 categories supported!"
|
| 17 |
|
| 18 |
+
# Convert each category column into a set of IDs
|
| 19 |
+
sets = [set(df[df[col] == 1].index) for col in categories]
|
| 20 |
|
| 21 |
+
# Plot Venn
|
| 22 |
+
plt.figure(figsize=(6,6))
|
| 23 |
+
if len(sets) == 2:
|
| 24 |
+
venn2(sets, set_labels=categories)
|
| 25 |
+
elif len(sets) == 3:
|
| 26 |
+
venn3(sets, set_labels=categories)
|
| 27 |
|
|
|
|
| 28 |
out_file = "venn.png"
|
| 29 |
+
plt.savefig(out_file)
|
| 30 |
+
plt.close()
|
|
|
|
|
|
|
| 31 |
return out_file
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {e}"
|
| 35 |
|
|
|
|
| 36 |
demo = gr.Interface(
|
| 37 |
fn=make_venn,
|
| 38 |
inputs=gr.File(file_types=[".csv"], label="Upload CSV"),
|
| 39 |
+
outputs=gr.Image(type="filepath", label="Venn Diagram"),
|
| 40 |
+
title="Venn Diagram Generator",
|
| 41 |
+
description="Upload a CSV with a primary key column + up to 3 category columns (binary flags: 0/1)."
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|