mlbench123 commited on
Commit
101bca1
·
verified ·
1 Parent(s): e4d7abb

Update gradio_new_rag_app.py

Browse files
Files changed (1) hide show
  1. 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
- HF Spaces usage:
6
- - HF runs app.py which imports make_app() from this module.
7
- - Do NOT call demo.launch() on import.
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
- "procedure": c.get("procedure"),
46
- "similarity": c.get("similarity"),
47
- "type": c.get("type"),
48
- "region": c.get("region"),
49
- "sub_zone": c.get("sub_zone"),
50
- })
51
- return pd.DataFrame(rows)
52
-
53
-
54
- def make_app():
55
- rag = RAGTreatmentSearchApp(
56
- excel_path=os.getenv("DB_XLSX", "database.xlsx"),
57
- embeddings_cache_path=os.getenv("EMB_CACHE", "treatment_embeddings.pkl"),
58
- )
59
-
60
- regions = rag.get_regions()
61
-
62
- def subzones_for_region(region):
63
- if not region:
64
- return []
65
- return rag.get_sub_zones(region)
66
-
67
- def run_search(region, sub_zone, issue, pref, retrieval_k, final_k, show_debug):
68
- out = rag.answer(
69
- region=region,
70
- sub_zone=sub_zone,
71
- type_choice=pref,
72
- issue_text=issue,
73
- retrieval_k=int(retrieval_k),
74
- final_k=int(final_k),
75
- )
76
- md = format_answer_markdown(out)
77
- dbg_df = build_debug_table(out) if show_debug else pd.DataFrame()
78
- raw = json.dumps(out, ensure_ascii=False, indent=2)
79
- return md, dbg_df, raw
80
-
81
- with gr.Blocks(title=APP_TITLE) as demo:
82
- gr.Markdown(f"# {APP_TITLE}")
83
-
84
- with gr.Row():
85
- region = gr.Dropdown(
86
- choices=regions,
87
- label="Region (Body part)",
88
- value=regions[0] if regions else None
89
- )
90
- sub_zone = gr.Dropdown(
91
- choices=subzones_for_region(regions[0] if regions else ""),
92
- label="Sub-Zone",
93
- interactive=True
94
- )
95
-
96
- def _update_subzones(r):
97
- return gr.Dropdown(choices=subzones_for_region(r), value=None)
98
-
99
- region.change(_update_subzones, inputs=[region], outputs=[sub_zone])
100
-
101
- issue = gr.Textbox(lines=3, label="Describe your issue/problem (free text, multilingual supported)")
102
- pref = gr.Radio(
103
- ["Surgical Treatment", "Non-surgical Treatment", "Both"],
104
- value="Both",
105
- label="Treatment preference"
106
- )
107
-
108
- with gr.Row():
109
- retrieval_k = gr.Slider(5, 30, value=12, step=1, label="Retrieval candidates (semantic top-K)")
110
- final_k = gr.Slider(3, 10, value=5, step=1, label="Final recommendations (LLM top-K)")
111
-
112
- show_debug = gr.Checkbox(value=False, label="Show debug (semantic candidates)")
113
- run_btn = gr.Button("Run AI Search")
114
-
115
- md_out = gr.Markdown()
116
- dbg_out = gr.Dataframe(label="Debug: Semantic candidates")
117
- raw_json = gr.Code(label="Raw JSON")
118
-
119
- run_btn.click(
120
- run_search,
121
- inputs=[region, sub_zone, issue, pref, retrieval_k, final_k, show_debug],
122
- outputs=[md_out, dbg_out, raw_json],
123
- )
124
-
125
- gr.Markdown(
126
- """
127
- ### Deployment note (Hugging Face)
128
- This Space runs an embedded open-source Transformers model (CPU).
129
- Web evidence may be unavailable at times due to rate limits or blocked requests; the app will still return database-driven results.
130
- """
131
- )
132
-
133
- return demo
134
-
135
-
136
- if __name__ == "__main__":
137
- demo = make_app()
138
- demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
 
 
 
 
 
 
 
 
 
 
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