mariagrandury commited on
Commit
91f4a96
·
1 Parent(s): ab8868f

feature: calculate overall percentage

Browse files
Files changed (1) hide show
  1. app.py +42 -1
app.py CHANGED
@@ -1,31 +1,72 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
 
 
 
 
4
 
5
  def calculate_percentage(*answers):
 
 
 
 
 
 
 
6
  yes_count = sum(answers)
 
 
7
  percentage = yes_count / len(answers) * 100
8
  return f"{percentage}%"
9
 
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  df = pd.read_csv("fmti_indicators.csv")
12
  grouped = df.groupby(["Category", "Subcategory"])
13
 
 
14
  interfaces = []
15
  tab_names = []
16
  for (category, subcategory), group in grouped:
17
  questions = group["Definition"].tolist()
18
  inputs = [gr.Checkbox(label=question) for question in questions]
19
- output = gr.Textbox()
20
  iface = gr.Interface(
21
  fn=calculate_percentage,
22
  inputs=inputs,
23
  outputs=output,
24
  title=f"{category} - {subcategory}",
 
25
  )
26
  interfaces.append(iface)
27
  tab_names.append(subcategory)
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  tabbed_interface = gr.TabbedInterface(
30
  interface_list=interfaces,
31
  tab_names=tab_names,
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # Initialize total counts
5
+ total_yes_count = 0
6
+ total_count = 0
7
+
8
 
9
  def calculate_percentage(*answers):
10
+ """
11
+ Calculate the percentage of 'yes' answers.
12
+
13
+ :param answers: Iterable containing the answers.
14
+ :returns: Percentage of 'yes' answers.
15
+ """
16
+ global total_yes_count, total_count
17
  yes_count = sum(answers)
18
+ total_yes_count += yes_count
19
+ total_count += len(answers)
20
  percentage = yes_count / len(answers) * 100
21
  return f"{percentage}%"
22
 
23
 
24
+ def calculate_overall_percentage():
25
+ """
26
+ Calculate the overall percentage of 'yes' answers.
27
+
28
+ :returns: Overall percentage of 'yes' answers.
29
+ """
30
+ global total_yes_count, total_count
31
+ if total_count != 100:
32
+ return "Make sure you have submitted your answers in all the tabs."
33
+ overall_percentage = total_yes_count
34
+ return f"{overall_percentage}%"
35
+
36
+
37
+ # Load data
38
  df = pd.read_csv("fmti_indicators.csv")
39
  grouped = df.groupby(["Category", "Subcategory"])
40
 
41
+ # Create an interface per group of indicators
42
  interfaces = []
43
  tab_names = []
44
  for (category, subcategory), group in grouped:
45
  questions = group["Definition"].tolist()
46
  inputs = [gr.Checkbox(label=question) for question in questions]
47
+ output = gr.Textbox(label="Subcategory Percentage")
48
  iface = gr.Interface(
49
  fn=calculate_percentage,
50
  inputs=inputs,
51
  outputs=output,
52
  title=f"{category} - {subcategory}",
53
+ allow_flagging="never",
54
  )
55
  interfaces.append(iface)
56
  tab_names.append(subcategory)
57
 
58
+ # Add overall percentage button
59
+ overall_button = gr.Interface(
60
+ fn=calculate_overall_percentage,
61
+ inputs=[],
62
+ outputs=gr.Textbox(label="Overall Percentage"),
63
+ title="Transparency Score",
64
+ allow_flagging="never",
65
+ )
66
+ interfaces.append(overall_button)
67
+ tab_names.append("Total Transparency Score")
68
+
69
+ # Create the tabbed interface
70
  tabbed_interface = gr.TabbedInterface(
71
  interface_list=interfaces,
72
  tab_names=tab_names,