Spaces:
Sleeping
Sleeping
Update gradio_new_rag_app.py
Browse files- gradio_new_rag_app.py +147 -138
gradio_new_rag_app.py
CHANGED
|
@@ -1,138 +1,147 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Gradio UI for the structured RAG AI Search.
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import json
|
| 11 |
-
import os
|
| 12 |
-
import pandas as pd
|
| 13 |
-
import gradio as gr
|
| 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 |
-
|
| 25 |
-
answer_md = (out.get("answer_md") or "").strip()
|
| 26 |
-
sources = out.get("sources") or []
|
| 27 |
-
|
| 28 |
-
md = []
|
| 29 |
-
md.append(answer_md if answer_md else "No answer generated.")
|
| 30 |
-
|
| 31 |
-
if sources:
|
| 32 |
-
md.append("\n---\n")
|
| 33 |
-
md.append("## Sources")
|
| 34 |
-
for u in sources[:20]:
|
| 35 |
-
md.append(f"- {u}")
|
| 36 |
-
|
| 37 |
-
return "\n".join(md).strip()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def build_debug_table(data: dict):
|
| 41 |
-
rows = []
|
| 42 |
-
dbg = (data or {}).get("_debug") or {}
|
| 43 |
-
for c in dbg.get("candidates") or []:
|
| 44 |
-
rows.append(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
""
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Gradio UI for the structured RAG AI Search.
|
| 4 |
+
|
| 5 |
+
Hugging Face Spaces usage:
|
| 6 |
+
- app.py imports make_app() and exposes `demo` at top-level.
|
| 7 |
+
- DO NOT call demo.launch() in this file.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import json
|
| 11 |
+
import os
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import gradio as gr
|
| 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 |
+
|
| 25 |
+
answer_md = (out.get("answer_md") or "").strip()
|
| 26 |
+
sources = out.get("sources") or []
|
| 27 |
+
|
| 28 |
+
md = []
|
| 29 |
+
md.append(answer_md if answer_md else "No answer generated.")
|
| 30 |
+
|
| 31 |
+
if sources:
|
| 32 |
+
md.append("\n---\n")
|
| 33 |
+
md.append("## Sources")
|
| 34 |
+
for u in sources[:20]:
|
| 35 |
+
md.append(f"- {u}")
|
| 36 |
+
|
| 37 |
+
return "\n".join(md).strip()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def build_debug_table(data: dict):
|
| 41 |
+
rows = []
|
| 42 |
+
dbg = (data or {}).get("_debug") or {}
|
| 43 |
+
for c in dbg.get("candidates") or []:
|
| 44 |
+
rows.append(
|
| 45 |
+
{
|
| 46 |
+
"procedure": c.get("procedure"),
|
| 47 |
+
"similarity": c.get("similarity"),
|
| 48 |
+
"type": c.get("type"),
|
| 49 |
+
"region": c.get("region"),
|
| 50 |
+
"sub_zone": c.get("sub_zone"),
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
return pd.DataFrame(rows)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def make_app():
|
| 57 |
+
# Load paths from env (HF-friendly)
|
| 58 |
+
db_path = os.getenv("DB_XLSX", "database.xlsx")
|
| 59 |
+
emb_path = os.getenv("EMB_CACHE", "treatment_embeddings.pkl")
|
| 60 |
+
|
| 61 |
+
rag = RAGTreatmentSearchApp(
|
| 62 |
+
excel_path=db_path,
|
| 63 |
+
embeddings_cache_path=emb_path,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
regions = rag.get_regions()
|
| 67 |
+
|
| 68 |
+
def subzones_for_region(region):
|
| 69 |
+
if not region:
|
| 70 |
+
return []
|
| 71 |
+
return rag.get_sub_zones(region)
|
| 72 |
+
|
| 73 |
+
def run_search(region, sub_zone, issue, pref, retrieval_k, final_k, show_debug):
|
| 74 |
+
out = rag.answer(
|
| 75 |
+
region=region,
|
| 76 |
+
sub_zone=sub_zone,
|
| 77 |
+
type_choice=pref,
|
| 78 |
+
issue_text=issue,
|
| 79 |
+
retrieval_k=int(retrieval_k),
|
| 80 |
+
final_k=int(final_k),
|
| 81 |
+
)
|
| 82 |
+
md = format_answer_markdown(out)
|
| 83 |
+
dbg_df = build_debug_table(out) if show_debug else pd.DataFrame()
|
| 84 |
+
raw = json.dumps(out, ensure_ascii=False, indent=2)
|
| 85 |
+
return md, dbg_df, raw
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(title=APP_TITLE) as demo:
|
| 88 |
+
gr.Markdown(f"# {APP_TITLE}")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
region = gr.Dropdown(
|
| 92 |
+
choices=regions,
|
| 93 |
+
label="Region (Body part)",
|
| 94 |
+
value=regions[0] if regions else None,
|
| 95 |
+
)
|
| 96 |
+
sub_zone = gr.Dropdown(
|
| 97 |
+
choices=subzones_for_region(regions[0] if regions else ""),
|
| 98 |
+
label="Sub-Zone",
|
| 99 |
+
interactive=True,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
def _update_subzones(r):
|
| 103 |
+
return gr.Dropdown(choices=subzones_for_region(r), value=None)
|
| 104 |
+
|
| 105 |
+
region.change(_update_subzones, inputs=[region], outputs=[sub_zone])
|
| 106 |
+
|
| 107 |
+
issue = gr.Textbox(
|
| 108 |
+
lines=3,
|
| 109 |
+
label="Describe your issue/problem (free text, multilingual supported)",
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
pref = gr.Radio(
|
| 113 |
+
["Surgical Treatment", "Non-surgical Treatment", "Both"],
|
| 114 |
+
value="Both",
|
| 115 |
+
label="Treatment preference",
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
retrieval_k = gr.Slider(
|
| 120 |
+
5, 30, value=12, step=1, label="Retrieval candidates (semantic top-K)"
|
| 121 |
+
)
|
| 122 |
+
final_k = gr.Slider(
|
| 123 |
+
3, 10, value=5, step=1, label="Final recommendations (LLM top-K)"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
show_debug = gr.Checkbox(value=False, label="Show debug (semantic candidates)")
|
| 127 |
+
run_btn = gr.Button("Run AI Search")
|
| 128 |
+
|
| 129 |
+
md_out = gr.Markdown()
|
| 130 |
+
dbg_out = gr.Dataframe(label="Debug: Semantic candidates")
|
| 131 |
+
raw_json = gr.Code(label="Raw JSON")
|
| 132 |
+
|
| 133 |
+
run_btn.click(
|
| 134 |
+
run_search,
|
| 135 |
+
inputs=[region, sub_zone, issue, pref, retrieval_k, final_k, show_debug],
|
| 136 |
+
outputs=[md_out, dbg_out, raw_json],
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
gr.Markdown(
|
| 140 |
+
"""
|
| 141 |
+
### Deployment note (Hugging Face)
|
| 142 |
+
This Space runs an embedded open-source Transformers model (CPU).
|
| 143 |
+
Web evidence may be unavailable at times due to rate limits or blocked requests; the app will still return database-driven results.
|
| 144 |
+
"""
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
return demo
|