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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
else:
|
24 |
-
|
25 |
-
if safety.get(
|
26 |
-
|
27 |
-
if safety.get(
|
28 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
|
3 |
+
def get_custom_css():
|
4 |
+
"""Enhanced CSS with medical theme"""
|
5 |
+
css = """
|
6 |
+
@import url(\'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap\');
|
7 |
|
8 |
+
* {
|
9 |
+
font-family: \'Inter\', sans-serif !important;
|
10 |
+
}
|
11 |
|
12 |
+
.gradio-container {
|
13 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
14 |
+
min-height: 100vh;
|
15 |
+
}
|
16 |
+
|
17 |
+
.main-container {
|
18 |
+
background: rgba(255, 255, 255, 0.95);
|
19 |
+
backdrop-filter: blur(10px);
|
20 |
+
border-radius: 20px;
|
21 |
+
padding: 30px;
|
22 |
+
margin: 20px;
|
23 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
24 |
+
border: 1px solid rgba(255,255,255,0.2);
|
25 |
+
}
|
26 |
+
|
27 |
+
.header-section {
|
28 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
29 |
+
color: white;
|
30 |
+
border-radius: 15px;
|
31 |
+
padding: 25px;
|
32 |
+
margin-bottom: 25px;
|
33 |
+
text-align: center;
|
34 |
+
box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
|
35 |
+
}
|
36 |
+
|
37 |
+
.query-container {
|
38 |
+
background: linear-gradient(135deg, #f8f9ff 0%, #e8f2ff 100%);
|
39 |
+
border-radius: 15px;
|
40 |
+
padding: 20px;
|
41 |
+
margin: 15px 0;
|
42 |
+
border: 2px solid #667eea;
|
43 |
+
transition: all 0.3s ease;
|
44 |
+
}
|
45 |
+
|
46 |
+
.response-container {
|
47 |
+
background: linear-gradient(135deg, #fff 0%, #f8f9ff 100%);
|
48 |
+
border-radius: 15px;
|
49 |
+
padding: 20px;
|
50 |
+
margin: 15px 0;
|
51 |
+
border: 2px solid #4CAF50;
|
52 |
+
min-height: 300px;
|
53 |
+
}
|
54 |
+
|
55 |
+
.submit-btn {
|
56 |
+
background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%) !important;
|
57 |
+
color: white !important;
|
58 |
+
border: none !important;
|
59 |
+
border-radius: 12px !important;
|
60 |
+
padding: 15px 30px !important;
|
61 |
+
font-size: 16px !important;
|
62 |
+
font-weight: 600 !important;
|
63 |
+
cursor: pointer !important;
|
64 |
+
transition: all 0.3s ease !important;
|
65 |
+
box-shadow: 0 6px 20px rgba(76, 175, 80, 0.3) !important;
|
66 |
+
}
|
67 |
+
|
68 |
+
.submit-btn:hover {
|
69 |
+
transform: translateY(-3px) !important;
|
70 |
+
box-shadow: 0 10px 30px rgba(76, 175, 80, 0.4) !important;
|
71 |
+
}
|
72 |
+
|
73 |
+
.stats-container {
|
74 |
+
background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
|
75 |
+
border-radius: 12px;
|
76 |
+
padding: 15px;
|
77 |
+
margin: 10px 0;
|
78 |
+
border-left: 5px solid #2196F3;
|
79 |
+
font-size: 14px;
|
80 |
+
}
|
81 |
+
"""
|
82 |
+
return css
|
83 |
|
84 |
+
def create_header_section():
|
85 |
+
return gr.HTML("""
|
86 |
+
<div class="header-section">
|
87 |
+
<h1 style="margin: 0; font-size: 2.5em; font-weight: 700;">
|
88 |
+
π₯ Optimized Gaza First Aid Assistant
|
89 |
+
</h1>
|
90 |
+
<h2 style="margin: 10px 0 0 0; font-size: 1.2em; font-weight: 400; opacity: 0.9;">
|
91 |
+
Powered by Pre-computed FAISS Index & 768-dim Embeddings
|
92 |
+
</h2>
|
93 |
+
<p style="margin: 15px 0 0 0; font-size: 1em; opacity: 0.8;">
|
94 |
+
Lightning-fast medical guidance using pre-processed knowledge base
|
95 |
+
</p>
|
96 |
+
</div>
|
97 |
+
""")
|
98 |
|
99 |
+
def create_query_input_section():
|
100 |
+
with gr.Group(elem_classes=["query-container"]):
|
101 |
+
gr.Markdown("### π©Ί Medical Query Input")
|
102 |
+
query_input = gr.Textbox(
|
103 |
+
label="Describe your medical situation",
|
104 |
+
placeholder="Enter your first aid question or describe the medical emergency...",
|
105 |
+
lines=4
|
106 |
+
)
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
submit_btn = gr.Button(
|
110 |
+
"π Get Medical Guidance",
|
111 |
+
variant="primary",
|
112 |
+
elem_classes=["submit-btn"],
|
113 |
+
scale=3
|
114 |
+
)
|
115 |
+
clear_btn = gr.Button(
|
116 |
+
"ποΈ Clear",
|
117 |
+
variant="secondary",
|
118 |
+
scale=1
|
119 |
+
)
|
120 |
+
return query_input, submit_btn, clear_btn
|
121 |
|
122 |
+
def create_response_output_section():
|
123 |
+
with gr.Group(elem_classes=["response-container"]):
|
124 |
+
gr.Markdown("### π©Ή Medical Guidance Response")
|
125 |
+
response_output = gr.Textbox(
|
126 |
+
label="AI Medical Guidance",
|
127 |
+
lines=15,
|
128 |
+
interactive=False,
|
129 |
+
placeholder="Your medical guidance will appear here..."
|
130 |
+
)
|
131 |
+
|
132 |
+
with gr.Row():
|
133 |
+
with gr.Column(scale=1):
|
134 |
+
metadata_output = gr.Textbox(
|
135 |
+
label="π Response Metadata",
|
136 |
+
lines=2,
|
137 |
+
interactive=False,
|
138 |
+
placeholder="Response metadata will appear here..."
|
139 |
+
)
|
140 |
+
|
141 |
+
with gr.Column(scale=1):
|
142 |
+
status_output = gr.Textbox(
|
143 |
+
label="π‘οΈ Safety Status",
|
144 |
+
lines=2,
|
145 |
+
interactive=False,
|
146 |
+
placeholder="Safety validation status will appear here..."
|
147 |
+
)
|
148 |
+
return response_output, metadata_output, status_output
|
149 |
+
|
150 |
+
def create_quick_access_section():
|
151 |
+
return gr.Markdown("""
|
152 |
+
### β‘ Optimized Features
|
153 |
+
|
154 |
+
**π Performance:**
|
155 |
+
- Pre-computed FAISS index
|
156 |
+
- 768-dimensional embeddings
|
157 |
+
- Lightning-fast search
|
158 |
+
- Optimized for Gaza context
|
159 |
+
|
160 |
+
**π Knowledge Base:**
|
161 |
+
- WHO medical protocols
|
162 |
+
- ICRC war surgery guides
|
163 |
+
- MSF field manuals
|
164 |
+
- Gaza-specific adaptations
|
165 |
+
|
166 |
+
**π‘οΈ Safety Features:**
|
167 |
+
- Real-time fact checking
|
168 |
+
- Contraindication detection
|
169 |
+
- Gaza resource warnings
|
170 |
+
- Professional disclaimers
|
171 |
+
""")
|
172 |
+
|
173 |
+
def create_example_scenarios(query_input):
|
174 |
+
example_queries = [
|
175 |
+
"How to treat severe burns when clean water is extremely limited?",
|
176 |
+
"Managing gunshot wounds with only basic household supplies",
|
177 |
+
"Recognizing and treating infection in wounds without antibiotics",
|
178 |
+
"Emergency care for children during extended power outages",
|
179 |
+
"Treating compound fractures without proper medical equipment"
|
180 |
+
]
|
181 |
+
|
182 |
+
gr.Markdown("### π‘ Example Medical Scenarios")
|
183 |
+
gr.Examples(
|
184 |
+
examples=example_queries,
|
185 |
+
inputs=query_input,
|
186 |
+
label="Click any example to try it:",
|
187 |
+
examples_per_page=5
|
188 |
+
)
|
189 |
+
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
import gradio as gr
|
194 |
+
|
195 |
+
def gradio_user_selector():
|
196 |
+
"""Gradio equivalent of Streamlit's user_selector"""
|
197 |
+
return gr.Dropdown(
|
198 |
+
["Volunteer", "Event Organizer", "Hospital Staff"],
|
199 |
+
label="Select your role",
|
200 |
+
value="Volunteer",
|
201 |
+
interactive=True
|
202 |
+
)
|
203 |
+
|
204 |
+
def gradio_sidebar_controls():
|
205 |
+
"""Gradio equivalent of Streamlit's sidebar_controls (adapted for main layout)"""
|
206 |
+
with gr.Column(scale=1):
|
207 |
+
gr.Markdown("### βοΈ Settings")
|
208 |
+
language_dropdown = gr.Dropdown(
|
209 |
+
["English", "Arabic"],
|
210 |
+
label="Language",
|
211 |
+
value="English",
|
212 |
+
interactive=True
|
213 |
+
)
|
214 |
+
return language_dropdown
|
215 |
+
|
216 |
+
def gradio_show_response(response: str, metadata: dict, safety: dict):
|
217 |
+
"""Gradio equivalent of Streamlit's show_response"""
|
218 |
+
# This function will return Gradio components that will be updated
|
219 |
+
# It's not directly a 'show' function but rather a 'return values for update'
|
220 |
+
# The actual display will be handled by gr.Markdown or gr.Textbox components
|
221 |
+
|
222 |
+
response_text = gr.Markdown(f"### AI Response\n{response}")
|
223 |
+
|
224 |
+
metadata_text = f"### Metadata\nConfidence: {metadata.get(\'confidence\', 0)}\nResponse Time: {metadata.get(\'response_time\', 0)}s"
|
225 |
+
metadata_display = gr.Markdown(metadata_text)
|
226 |
+
|
227 |
+
safety_status = ""
|
228 |
+
if safety.get(\'is_safe\', False):
|
229 |
+
safety_status += "β
Safe Response\n"
|
230 |
else:
|
231 |
+
safety_status += "β οΈ Review Needed\n"
|
232 |
+
if safety.get(\'warnings\'):
|
233 |
+
safety_status += "Warnings: " + ", ".join(safety[\'warnings\']) + "\n"
|
234 |
+
if safety.get(\'issues\'):
|
235 |
+
safety_status += "Issues: " + ", ".join(safety[\'issues\']) + "\n"
|
236 |
+
safety_display = gr.Markdown(f"### Safety Check\n{safety_status}")
|
237 |
+
|
238 |
+
return response_text, metadata_display, safety_display
|
239 |
+
|