Fernandosr85 commited on
Commit
366a75e
·
verified ·
1 Parent(s): 800a4cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -14
app.py CHANGED
@@ -1066,6 +1066,38 @@ Full raw RAG context
1066
  return str(out_path)
1067
 
1068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1069
  def make_followup_update(followups: list[str], lang: str):
1070
  label = "Explore related questions" if lang == "en" else "Explorar questões relacionadas"
1071
  items = [str(f).strip() for f in followups if str(f).strip()]
@@ -1073,11 +1105,11 @@ def make_followup_update(followups: list[str], lang: str):
1073
 
1074
 
1075
  def analyze(query: str):
1076
- hidden_file = gr.update(value=None, visible=False)
1077
  hidden_followups = gr.update(choices=[], value=None, visible=False)
1078
 
1079
  if not query or not query.strip():
1080
- return render_error("Please enter a compliance query."), "", hidden_file, hidden_followups
1081
 
1082
  query = query.strip()
1083
  lang = detect_language(query)
@@ -1097,7 +1129,7 @@ def analyze(query: str):
1097
  )
1098
 
1099
  if not results:
1100
- return render_error("No relevant regulatory chunks found. Try rephrasing your query."), "", hidden_file, hidden_followups
1101
 
1102
  context = format_context(results)
1103
  report = call_claude(query, context)
@@ -1106,7 +1138,7 @@ def analyze(query: str):
1106
  return (
1107
  render_error("Could not reach Claude API. Check that ANTHROPIC_API_KEY is set as a Space Secret."),
1108
  render_evidence_summary(results),
1109
- gr.update(value=context_path, visible=True),
1110
  hidden_followups,
1111
  )
1112
 
@@ -1128,7 +1160,7 @@ def analyze(query: str):
1128
  return (
1129
  render_report(report, query, results),
1130
  render_evidence_summary(results),
1131
- gr.update(value=context_path, visible=True),
1132
  make_followup_update(followups, lang),
1133
  )
1134
 
@@ -1303,12 +1335,7 @@ with gr.Blocks(css=CSS, title="RegTech BR") as demo:
1303
  elem_classes=["followup-radio"],
1304
  )
1305
 
1306
- context_file = gr.File(
1307
- label="Download full retrieved regulatory context (.txt)",
1308
- visible=False,
1309
- interactive=False,
1310
- elem_classes=["file-output"],
1311
- )
1312
 
1313
  with gr.Accordion("Quick regulatory checks — load an example query", open=False):
1314
  gr.HTML("""
@@ -1340,9 +1367,16 @@ with gr.Blocks(css=CSS, title="RegTech BR") as demo:
1340
 
1341
  followup_radio.change(fn=lambda selected: selected or "", inputs=[followup_radio], outputs=[query_box])
1342
 
1343
- analyze_btn.click(fn=analyze, inputs=[query_box], outputs=[report_html, evidence_html, context_file, followup_radio])
1344
- query_box.submit(fn=analyze, inputs=[query_box], outputs=[report_html, evidence_html, context_file, followup_radio])
1345
 
1346
  if __name__ == "__main__":
1347
  port = int(os.environ.get("PORT", 7860))
1348
- demo.queue().launch(server_name="0.0.0.0", server_port=port, share=False, show_api=False)
 
 
 
 
 
 
 
 
1066
  return str(out_path)
1067
 
1068
 
1069
+ def render_context_download(path: str) -> str:
1070
+ """Render a lightweight download card without gr.File.
1071
+
1072
+ Gradio 4.44.0 can crash while generating API metadata for gr.File
1073
+ on Spaces. A plain HTML link avoids that schema bug.
1074
+ The launch() call exposes REGTECH_CONTEXT_DIR through allowed_paths.
1075
+ """
1076
+ if not path:
1077
+ return ""
1078
+
1079
+ safe_path = esc(path)
1080
+ filename = esc(Path(path).name)
1081
+ return f"""
1082
+ <div style="font-family:'IBM Plex Mono',monospace;background:#0f172a;color:#e2e8f0;
1083
+ padding:0.9rem 1rem;border-radius:10px;border:1px solid #1e3a5f;
1084
+ margin:0.8rem 0">
1085
+ <div style="color:#94a3b8;font-size:0.72rem;text-transform:uppercase;
1086
+ letter-spacing:0.1em;margin-bottom:0.35rem">
1087
+ Download audit context
1088
+ </div>
1089
+ <div style="color:#64748b;font-size:0.74rem;margin-bottom:0.55rem">
1090
+ Full raw retrieved regulatory context exported as a text file.
1091
+ </div>
1092
+ <a href="/file={safe_path}" download="{filename}"
1093
+ style="display:inline-block;background:#1e3a5f;color:#bfdbfe;border:1px solid #2563eb;
1094
+ border-radius:6px;padding:0.45rem 0.7rem;text-decoration:none;font-size:0.78rem">
1095
+ Download {filename}
1096
+ </a>
1097
+ </div>
1098
+ """
1099
+
1100
+
1101
  def make_followup_update(followups: list[str], lang: str):
1102
  label = "Explore related questions" if lang == "en" else "Explorar questões relacionadas"
1103
  items = [str(f).strip() for f in followups if str(f).strip()]
 
1105
 
1106
 
1107
  def analyze(query: str):
1108
+ hidden_download = ""
1109
  hidden_followups = gr.update(choices=[], value=None, visible=False)
1110
 
1111
  if not query or not query.strip():
1112
+ return render_error("Please enter a compliance query."), "", hidden_download, hidden_followups
1113
 
1114
  query = query.strip()
1115
  lang = detect_language(query)
 
1129
  )
1130
 
1131
  if not results:
1132
+ return render_error("No relevant regulatory chunks found. Try rephrasing your query."), "", hidden_download, hidden_followups
1133
 
1134
  context = format_context(results)
1135
  report = call_claude(query, context)
 
1138
  return (
1139
  render_error("Could not reach Claude API. Check that ANTHROPIC_API_KEY is set as a Space Secret."),
1140
  render_evidence_summary(results),
1141
+ render_context_download(context_path),
1142
  hidden_followups,
1143
  )
1144
 
 
1160
  return (
1161
  render_report(report, query, results),
1162
  render_evidence_summary(results),
1163
+ render_context_download(context_path),
1164
  make_followup_update(followups, lang),
1165
  )
1166
 
 
1335
  elem_classes=["followup-radio"],
1336
  )
1337
 
1338
+ context_download_html = gr.HTML(label="Download full retrieved regulatory context")
 
 
 
 
 
1339
 
1340
  with gr.Accordion("Quick regulatory checks — load an example query", open=False):
1341
  gr.HTML("""
 
1367
 
1368
  followup_radio.change(fn=lambda selected: selected or "", inputs=[followup_radio], outputs=[query_box])
1369
 
1370
+ analyze_btn.click(fn=analyze, inputs=[query_box], outputs=[report_html, evidence_html, context_download_html, followup_radio])
1371
+ query_box.submit(fn=analyze, inputs=[query_box], outputs=[report_html, evidence_html, context_download_html, followup_radio])
1372
 
1373
  if __name__ == "__main__":
1374
  port = int(os.environ.get("PORT", 7860))
1375
+ context_dir = os.environ.get("REGTECH_CONTEXT_DIR", "/tmp/regtech_br_contexts")
1376
+ demo.queue().launch(
1377
+ server_name="0.0.0.0",
1378
+ server_port=port,
1379
+ share=True,
1380
+ show_api=False,
1381
+ allowed_paths=[context_dir],
1382
+ )