Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from rpy2.robjects import pandas2ri, r
|
| 4 |
+
from rpy2.robjects.packages import importr
|
| 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 |
+
# read CSV
|
| 15 |
+
df = pd.read_csv(file.name)
|
| 16 |
+
|
| 17 |
+
# check minimum cols
|
| 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 category columns allowed!"
|
| 25 |
+
|
| 26 |
+
# convert to R dataframe
|
| 27 |
+
rdf = pandas2ri.py2rpy(df[categories])
|
| 28 |
+
|
| 29 |
+
# compute Euler diagram
|
| 30 |
+
fit = eulerr.euler(rdf)
|
| 31 |
+
|
| 32 |
+
# save plot
|
| 33 |
+
out_file = "venn.png"
|
| 34 |
+
r(f"png('{out_file}', width=600, height=600)")
|
| 35 |
+
r("plot")(fit, fills=["#66c2a5", "#fc8d62", "#8da0cb"])
|
| 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="Euler Diagram"),
|
| 48 |
+
title="EulerR Venn Diagram Generator",
|
| 49 |
+
description="Upload a CSV with a primary key column + up to 3 category columns. Generates an Euler diagram."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch()
|