amitbehura commited on
Commit
cf42baa
·
verified ·
1 Parent(s): 2266dd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -28
app.py CHANGED
@@ -1,52 +1,44 @@
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__":
 
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__":