Spaces:
Sleeping
Sleeping
Update gradio_new_rag_app.py
Browse files- gradio_new_rag_app.py +94 -19
gradio_new_rag_app.py
CHANGED
|
@@ -1,4 +1,12 @@
|
|
| 1 |
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
|
@@ -6,18 +14,16 @@ import gradio as gr
|
|
| 6 |
|
| 7 |
from rag_treatment_app import RAGTreatmentSearchApp
|
| 8 |
|
|
|
|
| 9 |
APP_TITLE = "Aesthetic AI Search (Structured RAG - Region → Sub-Zone → Issue → Type)"
|
| 10 |
|
|
|
|
| 11 |
def format_answer_markdown(out: dict) -> str:
|
| 12 |
if not isinstance(out, dict):
|
| 13 |
return "No output."
|
| 14 |
answer_md = (out.get("answer_md") or "").strip()
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
if sources:
|
| 18 |
-
md.append("\n---\n## Sources")
|
| 19 |
-
md.extend([f"- {u}" for u in sources[:20]])
|
| 20 |
-
return "\n".join(md).strip()
|
| 21 |
|
| 22 |
def build_debug_table(data: dict):
|
| 23 |
rows = []
|
|
@@ -32,6 +38,7 @@ def build_debug_table(data: dict):
|
|
| 32 |
})
|
| 33 |
return pd.DataFrame(rows)
|
| 34 |
|
|
|
|
| 35 |
def make_app():
|
| 36 |
rag = RAGTreatmentSearchApp(
|
| 37 |
excel_path=os.getenv("DB_XLSX", "database.xlsx"),
|
|
@@ -45,6 +52,14 @@ def make_app():
|
|
| 45 |
return []
|
| 46 |
return rag.get_sub_zones(region)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def run_search(region, sub_zone, issue, pref, retrieval_k, final_k, show_debug):
|
| 49 |
out = rag.answer(
|
| 50 |
region=region,
|
|
@@ -57,22 +72,37 @@ def make_app():
|
|
| 57 |
md = format_answer_markdown(out)
|
| 58 |
dbg_df = build_debug_table(out) if show_debug else pd.DataFrame()
|
| 59 |
raw = json.dumps(out, ensure_ascii=False, indent=2)
|
| 60 |
-
return md, dbg_df, raw
|
| 61 |
|
| 62 |
with gr.Blocks(title=APP_TITLE) as demo:
|
| 63 |
gr.Markdown(f"# {APP_TITLE}")
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
-
region = gr.Dropdown(
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
issue = gr.Textbox(lines=3, label="Describe your issue/problem (free text, multilingual supported)")
|
| 75 |
-
pref = gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
retrieval_k = gr.Slider(5, 30, value=12, step=1, label="Retrieval candidates (semantic top-K)")
|
|
@@ -82,15 +112,60 @@ def make_app():
|
|
| 82 |
run_btn = gr.Button("Run AI Search")
|
| 83 |
|
| 84 |
md_out = gr.Markdown()
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
run_btn.click(
|
| 91 |
run_search,
|
| 92 |
inputs=[region, sub_zone, issue, pref, retrieval_k, final_k, show_debug],
|
| 93 |
-
outputs=[md_out, dbg_out, raw_json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Gradio UI for the structured RAG AI Search.
|
| 4 |
+
|
| 5 |
+
NEW FEATURE:
|
| 6 |
+
- After Region + Sub-Zone selection, show 1-4 "Common concerns" (internet -> fallback DB).
|
| 7 |
+
- Clicking a concern auto-fills the Issue textbox.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
import json
|
| 11 |
import os
|
| 12 |
import pandas as pd
|
|
|
|
| 14 |
|
| 15 |
from rag_treatment_app import RAGTreatmentSearchApp
|
| 16 |
|
| 17 |
+
|
| 18 |
APP_TITLE = "Aesthetic AI Search (Structured RAG - Region → Sub-Zone → Issue → Type)"
|
| 19 |
|
| 20 |
+
|
| 21 |
def format_answer_markdown(out: dict) -> str:
|
| 22 |
if not isinstance(out, dict):
|
| 23 |
return "No output."
|
| 24 |
answer_md = (out.get("answer_md") or "").strip()
|
| 25 |
+
return answer_md if answer_md else "No answer generated."
|
| 26 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def build_debug_table(data: dict):
|
| 29 |
rows = []
|
|
|
|
| 38 |
})
|
| 39 |
return pd.DataFrame(rows)
|
| 40 |
|
| 41 |
+
|
| 42 |
def make_app():
|
| 43 |
rag = RAGTreatmentSearchApp(
|
| 44 |
excel_path=os.getenv("DB_XLSX", "database.xlsx"),
|
|
|
|
| 52 |
return []
|
| 53 |
return rag.get_sub_zones(region)
|
| 54 |
|
| 55 |
+
def concerns_for(region, sub_zone):
|
| 56 |
+
if not region or not sub_zone:
|
| 57 |
+
return []
|
| 58 |
+
try:
|
| 59 |
+
return rag.get_common_concerns(region, sub_zone, n=4)
|
| 60 |
+
except Exception:
|
| 61 |
+
return []
|
| 62 |
+
|
| 63 |
def run_search(region, sub_zone, issue, pref, retrieval_k, final_k, show_debug):
|
| 64 |
out = rag.answer(
|
| 65 |
region=region,
|
|
|
|
| 72 |
md = format_answer_markdown(out)
|
| 73 |
dbg_df = build_debug_table(out) if show_debug else pd.DataFrame()
|
| 74 |
raw = json.dumps(out, ensure_ascii=False, indent=2)
|
| 75 |
+
return md, dbg_df, raw
|
| 76 |
|
| 77 |
with gr.Blocks(title=APP_TITLE) as demo:
|
| 78 |
gr.Markdown(f"# {APP_TITLE}")
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
+
region = gr.Dropdown(
|
| 82 |
+
choices=regions,
|
| 83 |
+
label="Region (Body part)",
|
| 84 |
+
value=regions[0] if regions else None
|
| 85 |
+
)
|
| 86 |
+
sub_zone = gr.Dropdown(
|
| 87 |
+
choices=subzones_for_region(regions[0] if regions else ""),
|
| 88 |
+
label="Sub-Zone",
|
| 89 |
+
interactive=True
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# NEW: common concerns selector
|
| 93 |
+
common_concerns = gr.Radio(
|
| 94 |
+
choices=[],
|
| 95 |
+
value=None,
|
| 96 |
+
label="Common concerns (optional) — click one to auto-fill Issue",
|
| 97 |
+
interactive=True
|
| 98 |
+
)
|
| 99 |
|
| 100 |
issue = gr.Textbox(lines=3, label="Describe your issue/problem (free text, multilingual supported)")
|
| 101 |
+
pref = gr.Radio(
|
| 102 |
+
["Surgical Treatment", "Non-surgical Treatment", "Both"],
|
| 103 |
+
value="Both",
|
| 104 |
+
label="Treatment preference"
|
| 105 |
+
)
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
retrieval_k = gr.Slider(5, 30, value=12, step=1, label="Retrieval candidates (semantic top-K)")
|
|
|
|
| 112 |
run_btn = gr.Button("Run AI Search")
|
| 113 |
|
| 114 |
md_out = gr.Markdown()
|
| 115 |
+
dbg_out = gr.Dataframe(label="Debug: Semantic candidates")
|
| 116 |
+
raw_json = gr.Code(label="Raw JSON")
|
| 117 |
|
| 118 |
+
# --- events ---
|
| 119 |
+
|
| 120 |
+
def _update_subzones_and_concerns(r):
|
| 121 |
+
subz = subzones_for_region(r)
|
| 122 |
+
# reset subzone selection, and clear concerns until subzone is picked
|
| 123 |
+
return gr.Dropdown(choices=subz, value=None), gr.Radio(choices=[], value=None)
|
| 124 |
+
|
| 125 |
+
region.change(
|
| 126 |
+
_update_subzones_and_concerns,
|
| 127 |
+
inputs=[region],
|
| 128 |
+
outputs=[sub_zone, common_concerns]
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
def _update_concerns(r, sz):
|
| 132 |
+
opts = concerns_for(r, sz)
|
| 133 |
+
return gr.Radio(choices=opts, value=None)
|
| 134 |
+
|
| 135 |
+
sub_zone.change(
|
| 136 |
+
_update_concerns,
|
| 137 |
+
inputs=[region, sub_zone],
|
| 138 |
+
outputs=[common_concerns]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# click concern -> fill issue text
|
| 142 |
+
def _fill_issue_from_concern(c):
|
| 143 |
+
if not c:
|
| 144 |
+
return gr.update()
|
| 145 |
+
return c
|
| 146 |
+
|
| 147 |
+
common_concerns.change(
|
| 148 |
+
_fill_issue_from_concern,
|
| 149 |
+
inputs=[common_concerns],
|
| 150 |
+
outputs=[issue]
|
| 151 |
+
)
|
| 152 |
|
| 153 |
run_btn.click(
|
| 154 |
run_search,
|
| 155 |
inputs=[region, sub_zone, issue, pref, retrieval_k, final_k, show_debug],
|
| 156 |
+
outputs=[md_out, dbg_out, raw_json],
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
gr.Markdown(
|
| 160 |
+
"""
|
| 161 |
+
### Note
|
| 162 |
+
If internet access is blocked/rate-limited on Hugging Face, the app automatically falls back to your DB to suggest common concerns.
|
| 163 |
+
"""
|
| 164 |
)
|
| 165 |
|
| 166 |
return demo
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
demo = make_app()
|
| 171 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|