KutisBayag commited on
Commit
448068e
·
verified ·
1 Parent(s): a75deb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -81
app.py CHANGED
@@ -1,93 +1,57 @@
1
  import gradio as gr
2
  import pandas as pd
3
 
4
- # Load your data (replace with your actual file path if local or URL if remote)
5
- df = pd.read_excel("your_file.xlsx")
 
 
 
 
 
 
 
 
 
6
 
7
- # Mapping for AG column
8
- mapping = {
9
- "obs_commercial_purpose_commercial": "Commercial",
10
- "obs_commercial_purpose_issue": "SIP",
11
- "obs_environmental_politics_global": "Environmental Politics",
12
- "obs_civil_social_rights_global": "Civil and Social Rights",
13
- "obs_economy_global": "Economy",
14
- "obs_security_foreign_policy_global": "Security and Foreign Policy",
15
- "obs_political_values_global": "Political Values and Governance",
16
- "obs_health_global": "Health",
17
- "obs_immigration_global": "Immigration",
18
- "obs_crime_global": "Crime",
19
- "obs_guns_global": "Guns",
20
- "obs_education_global": "Education"
21
- }
22
-
23
- def process_row(name, job_id, col_r, col_af, col_ag, col_cc):
24
- # Handle TM (col_r)
25
- tm_value = mapping.get(col_r, "NT") if pd.notna(col_r) else "NT"
26
-
27
- # Handle Auditor (col_af + col_ag)
28
- auditor_tags = []
29
- if pd.notna(col_af):
30
- auditor_tags.append(mapping.get(col_af, "NT"))
31
- else:
32
- auditor_tags.append("NT")
33
- if pd.notna(col_ag):
34
- if col_ag == "obs_commercial_purpose_commercial":
35
- auditor_tags = [mapping[col_ag]] # only "Commercial"
36
- else:
37
- auditor_tags.append(mapping.get(col_ag, ""))
38
- auditor_value = ", ".join([x for x in auditor_tags if x])
39
-
40
- # Special rule: SIP always at end
41
- if "SIP" in auditor_tags and len(auditor_tags) > 1:
42
- auditor_tags = [x for x in auditor_tags if x != "SIP"] + ["SIP"]
43
- auditor_value = ", ".join(auditor_tags)
44
-
45
- # Highlighting logic
46
- if tm_value == auditor_value:
47
- tm_display = f'<span style="color:green;font-weight:bold">{tm_value}</span>'
48
- auditor_display = f'<span style="color:green;font-weight:bold">{auditor_value}</span>'
49
- else:
50
- tm_display = f'<span style="color:red;font-weight:bold">{tm_value}</span>'
51
- auditor_display = f'<span style="color:red;font-weight:bold">{auditor_value}</span>'
52
-
53
- # Rationale (col_cc)
54
- rationale = col_cc if pd.notna(col_cc) else ""
55
-
56
- # Format final output
57
- output = (
58
- f"{name}<br>"
59
- f"job id: #{job_id}<br>"
60
- f"TM: {tm_display}<br>"
61
- f"Auditor: {auditor_display}<br><br>"
62
- f"Rationale: {rationale}"
63
- )
64
- return output
65
-
66
- def generate_output(selected_name):
67
- person_data = df[df["BX"] == selected_name] # column BX = Names
68
  results = []
69
- for _, row in person_data.iterrows():
70
- result = process_row(
71
- row["BX"], # Name
72
- row["A"], # Job ID
73
- row["R"], # TM column
74
- row["AF"], # Auditor column
75
- row["AG"], # Auditor issue/commercial
76
- row["CC"] # Rationale
77
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  results.append(result)
79
- return "<br><br>".join(results)
80
 
81
- # Gradio Interface
82
- names = df["BX"].dropna().unique().tolist()
83
 
84
  with gr.Blocks() as demo:
85
- gr.Markdown("### Job Review Tool")
 
86
  with gr.Row():
87
- name_dropdown = gr.Dropdown(choices=names, label="Select Name")
88
- output_html = gr.HTML()
 
89
 
90
- name_dropdown.change(fn=generate_output, inputs=name_dropdown, outputs=output_html)
91
 
92
- if __name__ == "__main__":
93
- demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ def process_google_sheet(sheet_url):
5
+ # Convert Google Sheets URL to CSV export
6
+ if "/edit" in sheet_url:
7
+ sheet_url = sheet_url.replace("/edit", "/export?format=csv")
8
+ elif not sheet_url.endswith("/export?format=csv"):
9
+ sheet_url += "/export?format=csv"
10
+
11
+ try:
12
+ df = pd.read_csv(sheet_url)
13
+ except Exception as e:
14
+ return f"<span style='color:red'>Error reading sheet: {e}</span>"
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  results = []
17
+ for _, row in df.iterrows():
18
+ name = row.get("Name", "")
19
+ job_id = row.get("Job ID", "")
20
+
21
+ tm_answer = str(row.get("R", "NT")).strip() if pd.notna(row.get("R")) else "NT"
22
+ auditor_answer = str(row.get("AF", "NT")).strip() if pd.notna(row.get("AF")) else "NT"
23
+ rationale = str(row.get("CC", "")).strip()
24
+
25
+ # Compare TM vs Auditor
26
+ if tm_answer == auditor_answer:
27
+ tm_display = f"<span style='color:green;font-weight:bold'>{tm_answer}</span>"
28
+ auditor_display = f"<span style='color:green;font-weight:bold'>{auditor_answer}</span>"
29
+ else:
30
+ tm_display = f"<span style='color:red;font-weight:bold'>{tm_answer}</span>"
31
+ auditor_display = f"<span style='color:red;font-weight:bold'>{auditor_answer}</span>"
32
+
33
+ result = f"""
34
+ <div style='margin-bottom:20px'>
35
+ <b>{name}</b><br>
36
+ job id: #{job_id}<br>
37
+ TM: {tm_display}<br>
38
+ Auditor: {auditor_display}<br><br>
39
+ Rationale: {rationale}
40
+ </div>
41
+ """
42
  results.append(result)
 
43
 
44
+ return "<br>".join(results)
45
+
46
 
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("## Google Sheets Report Formatter with Highlighted Comparison")
49
+
50
  with gr.Row():
51
+ sheet_url = gr.Textbox(label="Paste Google Sheet Link")
52
+
53
+ output = gr.HTML()
54
 
55
+ sheet_url.change(process_google_sheet, inputs=sheet_url, outputs=output)
56
 
57
+ demo.launch()