KutisBayag commited on
Commit
4038880
·
verified ·
1 Parent(s): 57613f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -86
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import gradio as gr
2
  import pandas as pd
3
- import re
4
 
5
- # Mapping dictionary
 
 
 
6
  mapping = {
7
  "obs_commercial_purpose_commercial": "Commercial",
8
  "obs_commercial_purpose_issue": "SIP",
@@ -15,97 +17,77 @@ mapping = {
15
  "obs_immigration_global": "Immigration",
16
  "obs_crime_global": "Crime",
17
  "obs_guns_global": "Guns",
18
- "obs_education_global": "Education",
19
  }
20
 
21
- def process_tags(tags):
22
- if not isinstance(tags, str):
23
- return ""
24
-
25
- raw_tags = [t.strip() for t in tags.split(",") if t.strip()]
26
-
27
- # Rule 1: If Commercial exists → only output Commercial
28
- if "obs_commercial_purpose_commercial" in raw_tags:
29
- return "Commercial"
30
 
31
- # Otherwise map normally
32
- mapped = []
33
- sip_flag = False
34
- for tag in raw_tags:
35
- if tag == "obs_commercial_purpose_issue":
36
- sip_flag = True # store SIP to add later
37
- elif tag in mapping:
38
- mapped.append(mapping[tag])
39
-
40
- # Add SIP at the end if exists
41
- if sip_flag:
42
- mapped.append("SIP")
43
-
44
- return ", ".join(mapped)
45
 
46
- def process_sheet(sheet_url):
47
- # Convert Google Sheets share link to CSV export
48
- match = re.search(r"/d/([a-zA-Z0-9-_]+)", sheet_url)
49
- if not match:
50
- return "❌ Invalid Google Sheets link."
51
-
52
- sheet_id = match.group(1)
53
- csv_url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv"
54
-
55
- try:
56
- df = pd.read_csv(csv_url)
57
- except Exception as e:
58
- return f"❌ Error reading sheet: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- outputs = []
61
- for _, row in df.iterrows():
62
- name = row.get("Full Name", "")
63
- job_id = row.get("job_id", "")
64
-
65
- # TM (from R)
66
- tm = process_tags(row.get("Combined", ""))
67
-
68
- # Auditor: gather all AF–AW columns
69
- auditor_tags = []
70
- for col in df.columns:
71
- if col.startswith("A"): # AF, AG, AH...
72
- auditor_tags.append(str(row[col]))
73
- auditor = process_tags(", ".join(auditor_tags))
74
-
75
- # Rule: if both TM and Auditor are blank → NT
76
- if not tm.strip():
77
- tm = "NT"
78
- if not auditor.strip():
79
- auditor = "NT"
80
-
81
- # Column CC = rationale (fixed index approach)
82
- # CC = 81st column (A=0, B=1, ..., CC=80)
83
- rationale = ""
84
- try:
85
- rationale = row.iloc[80] # 0-based index for CC
86
- except Exception:
87
- rationale = ""
88
-
89
- rationale_text = f"\n\nRationale: {rationale}" if pd.notna(rationale) and str(rationale).strip() else ""
90
-
91
- # Final formatted text
92
- text = f"""{name}
93
- job id: {job_id}
94
- TM: {tm}
95
- Auditor: {auditor}{rationale_text}"""
96
-
97
- outputs.append(text)
98
 
99
- return "\n\n---\n\n".join(outputs)
 
 
 
 
100
 
101
- # Gradio App
102
- demo = gr.Interface(
103
- fn=process_sheet,
104
- inputs=gr.Textbox(label="Google Sheets Link", placeholder="Paste Google Sheets link here"),
105
- outputs=gr.Textbox(label="Formatted Output", lines=25),
106
- title="Google Sheets Formatter",
107
- description="Paste your Google Sheets link (make sure it's shared as 'Anyone with the link can view')."
108
- )
109
 
110
  if __name__ == "__main__":
111
  demo.launch()
 
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",
 
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()