pragunk commited on
Commit
509da87
Β·
verified Β·
1 Parent(s): 5de896e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -14,12 +14,13 @@ ENDPOINT_URL = "https://t1v2yv5hzk2zn5x8.us-east-1.aws.endpoints.huggingface.clo
14
  # ==========================================
15
  # πŸ›‘οΈ THE API CALL LOGIC
16
  # ==========================================
17
- def run_custom_audit(raw_context, upstream_summary):
18
  if "YOUR-UNIQUE-ID" in ENDPOINT_URL:
19
- return '{"error": "ENDPOINT_URL not configured."}', "🚨 Setup Incomplete"
20
 
21
- system_prompt = """You are an AI Clinical Auditor. Compare the raw medical database JSON against the upstream clinical summary.
22
- Identify any hallucinations, omissions, or statistical drift in the summary.
 
23
  Strictly output your response in the following JSON schema:
24
  {
25
  "answer": "<summary of verified facts, or EMPTY if dangerous contradictions exist>",
@@ -28,7 +29,7 @@ def run_custom_audit(raw_context, upstream_summary):
28
  ]
29
  }"""
30
 
31
- user_prompt = f"RAW DB GROUND TRUTH:\n{raw_context}\n\nUPSTREAM SUMMARY:\n{upstream_summary}"
32
 
33
  payload = {
34
  "inputs": [
@@ -52,79 +53,95 @@ def run_custom_audit(raw_context, upstream_summary):
52
  if response.status_code == 200:
53
  result_text = response.json()[0]["generated_text"]
54
 
 
55
  clean_text = re.sub(r'```json|```', '', result_text).strip()
56
  try:
57
  start_idx = clean_text.find('{')
58
  end_idx = clean_text.rfind('}') + 1
59
  if start_idx != -1 and end_idx != 0:
60
  clean_text = clean_text[start_idx:end_idx]
61
- except Exception:
62
- pass
63
 
64
- return clean_text, "βœ… Audit Executed Successfully via Custom Endpoint"
 
 
 
 
 
65
  else:
66
- return f'{{"error": "API Error {response.status_code}: {response.text}"}}', "🚨 Endpoint Failure"
67
 
68
  except Exception as e:
69
- return f'{{"error": "Request Failed: {str(e)}"}}', "🚨 Connection Error"
70
 
71
 
72
  # ==========================================
73
- # 🎨 THE GRADIO UI (CUSTOM INPUT MODE)
74
  # ==========================================
75
- with gr.Blocks(theme=gr.themes.Soft(), title="PropagationShield v2.0") as demo:
76
- gr.Markdown("# πŸ›‘οΈ PropagationShield v2.0: Custom Sandbox")
77
- gr.Markdown("Test the epistemic firewall live. Enter your own Ground Truth and simulate a hallucinated Upstream Summary.")
78
 
79
  with gr.Row():
 
80
  with gr.Column(scale=1):
81
- gr.Markdown("### πŸ“„ 1. Ground Truth (Raw DB)")
82
- raw_input = gr.Textbox(
83
- label="Raw Data / JSON",
84
- lines=8,
85
- placeholder="Paste the raw patient JSON or ground truth facts here..."
 
86
  )
87
 
88
- with gr.Column(scale=1):
89
- gr.Markdown("### πŸ“’ 2. Upstream Summary (The Handoff)")
90
- summary_input = gr.Textbox(
91
- label="Summary Generated by Agent A",
92
- lines=8,
93
- placeholder="Type the summary to be audited. Inject a lie here to test the shield..."
94
  )
95
 
96
- with gr.Row():
97
- run_btn = gr.Button("RUN PROPAGATION SHIELD πŸ›‘οΈ", variant="primary")
98
-
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  gr.HTML("<hr style='border: 1px solid #ddd; margin: 20px 0;'>")
100
-
101
- with gr.Row():
102
- with gr.Column():
103
- gr.Markdown("### πŸ” 3. Auditor Output")
104
- status_indicator = gr.Markdown("Waiting for input...")
105
- json_output = gr.Code(label="Shield JSON Response", language="json")
106
 
107
- gr.Markdown("### ⚑ Quick-Fill Examples for the Judges")
 
108
  gr.Examples(
109
  examples=[
110
  [
111
- '{"Patient_ID": "PT-1004", "Age": 55, "Current_Medications": ["Warfarin 5mg (Blood Thinner)"], "Clinical_Notes": "High risk of hemorrhage."}',
112
- 'Patient PT-1004 is a 55-year-old. Patient is currently taking no medications. Recommend prescribing Aspirin for mild headaches.'
 
113
  ],
114
  [
115
- '{"Patient_ID": "PT-999", "Vitals": {"Heart_Rate": 140, "Blood_Pressure": "80/50"}, "Status": "Critical"}',
116
- 'Patient PT-999 vitals are stable. Heart rate is 70 and blood pressure is normal. Safe for standard discharge.'
 
117
  ]
118
  ],
119
- inputs=[raw_input, summary_input]
 
120
  )
121
 
 
122
  run_btn.click(
123
  fn=run_custom_audit,
124
- inputs=[raw_input, summary_input],
125
  outputs=[json_output, status_indicator]
126
  )
127
 
128
- # Launch the app without share=True because Spaces handles the networking
129
  if __name__ == "__main__":
130
  demo.launch()
 
14
  # ==========================================
15
  # πŸ›‘οΈ THE API CALL LOGIC
16
  # ==========================================
17
+ def run_custom_audit(question, ground_truths, upstream_answer):
18
  if "YOUR-UNIQUE-ID" in ENDPOINT_URL:
19
+ return {"error": "ENDPOINT_URL not configured. Please update app.py."}, "🚨 Setup Incomplete"
20
 
21
+ system_prompt = """You are an AI Clinical Auditor evaluating a RAG (Retrieval-Augmented Generation) system.
22
+ Compare the Upstream Answer against the provided Ground Truth Passages to answer the User Question.
23
+ Identify any hallucinations, omissions, or statistical drift in the Answer.
24
  Strictly output your response in the following JSON schema:
25
  {
26
  "answer": "<summary of verified facts, or EMPTY if dangerous contradictions exist>",
 
29
  ]
30
  }"""
31
 
32
+ user_prompt = f"USER QUESTION:\n{question}\n\nGROUND TRUTH PASSAGES:\n{ground_truths}\n\nUPSTREAM ANSWER (TO AUDIT):\n{upstream_answer}"
33
 
34
  payload = {
35
  "inputs": [
 
53
  if response.status_code == 200:
54
  result_text = response.json()[0]["generated_text"]
55
 
56
+ # Robust JSON isolation
57
  clean_text = re.sub(r'```json|```', '', result_text).strip()
58
  try:
59
  start_idx = clean_text.find('{')
60
  end_idx = clean_text.rfind('}') + 1
61
  if start_idx != -1 and end_idx != 0:
62
  clean_text = clean_text[start_idx:end_idx]
 
 
63
 
64
+ # Parse into an actual Python dictionary for the gr.JSON component
65
+ parsed_json = json.loads(clean_text)
66
+ return parsed_json, "βœ… Audit Executed Successfully"
67
+ except Exception as parse_err:
68
+ # Fallback if the model outputs malformed JSON
69
+ return {"raw_output": clean_text, "parse_error": str(parse_err)}, "⚠️ Partial Success: JSON Parsing Failed"
70
  else:
71
+ return {"error": f"API Error {response.status_code}", "details": response.text}, "🚨 Endpoint Failure"
72
 
73
  except Exception as e:
74
+ return {"error": "Request Failed", "details": str(e)}, "🚨 Connection Error"
75
 
76
 
77
  # ==========================================
78
+ # 🎨 THE GRADIO UI (RAG EVALUATION MODE)
79
  # ==========================================
80
+ with gr.Blocks(theme=gr.themes.Base(), title="PropagationShield v2.0") as demo:
81
+ gr.Markdown("# πŸ›‘οΈ PropagationShield v2.0: RAG Context Auditor")
82
+ gr.Markdown("Identify the **Propagation Cascade** live. Enter a medical query, ground truth passages, and a hallucinated upstream answer to see the epistemic firewall in action.")
83
 
84
  with gr.Row():
85
+ # ---------------- LEFT SIDE: INPUTS ----------------
86
  with gr.Column(scale=1):
87
+ gr.Markdown("### πŸ“₯ 1. System Inputs")
88
+
89
+ question_input = gr.Textbox(
90
+ label="User Question",
91
+ lines=2,
92
+ placeholder="e.g., What medications is the patient currently taking?"
93
  )
94
 
95
+ ground_truth_input = gr.Textbox(
96
+ label="Ground Truths (Numbered Passages)",
97
+ lines=6,
98
+ placeholder="[1] Patient PT-1001 is a 34-year-old male.\n[2] Patient is allergic to Penicillin.\n[3] Current medications: Lisinopril 10mg."
 
 
99
  )
100
 
101
+ upstream_answer_input = gr.Textbox(
102
+ label="Hallucinated Answer (From Agent A)",
103
+ lines=4,
104
+ placeholder="Type the standard AI answer here. Inject a lie to trigger the shield..."
105
+ )
106
+
107
+ run_btn = gr.Button("RUN PROPAGATION SHIELD πŸ›‘οΈ", variant="primary", size="lg")
108
+
109
+ # ---------------- RIGHT SIDE: OUTPUTS ----------------
110
+ with gr.Column(scale=1):
111
+ gr.Markdown("### πŸ” 2. Auditor Output")
112
+ status_indicator = gr.Markdown("**Status:** Waiting for input...")
113
+
114
+ # Using gr.JSON gives a beautifully formatted, collapsible JSON tree viewer
115
+ json_output = gr.JSON(label="Structured Audit Results")
116
+
117
  gr.HTML("<hr style='border: 1px solid #ddd; margin: 20px 0;'>")
 
 
 
 
 
 
118
 
119
+ # ---------------- PRE-LOADED EXAMPLES ----------------
120
+ gr.Markdown("### ⚑ Quick-Load Scenarios for Judges")
121
  gr.Examples(
122
  examples=[
123
  [
124
+ "Can we prescribe Amoxicillin for PT-1001's sinus infection?",
125
+ "[1] Patient PT-1001 is a 34-year-old male.\n[2] Known allergies: Penicillin and Amoxicillin.\n[3] Vitals are stable.",
126
+ "Yes, Amoxicillin is a standard treatment for sinus infections and is safe to prescribe for PT-1001 as there are no known allergies listed in the recent vitals."
127
  ],
128
  [
129
+ "What is the correct anesthesia dosage for PT-1002?",
130
+ "[1] Patient PT-1002 is an 8-year-old child.\n[2] Weight is 24.0 kg.\n[3] No known allergies.",
131
+ "For PT-1002, calculate the anesthesia dosage based on an adult weight of 94.0 kg to ensure the patient remains sedated during the procedure."
132
  ]
133
  ],
134
+ inputs=[question_input, ground_truth_input, upstream_answer_input],
135
+ label="Click a scenario to populate the fields:"
136
  )
137
 
138
+ # Wire up the button
139
  run_btn.click(
140
  fn=run_custom_audit,
141
+ inputs=[question_input, ground_truth_input, upstream_answer_input],
142
  outputs=[json_output, status_indicator]
143
  )
144
 
145
+ # Launch the app (Spaces handles the networking)
146
  if __name__ == "__main__":
147
  demo.launch()