Spaces:
Sleeping
Sleeping
feat: Add prompt editing functionality to simplified interface
Browse files- Added 'Edit Prompts' tab with 5 editable prompts
- Spiritual Monitor (Classifier)
- Triage Question Generator
- Triage Response Evaluator
- Medical Assistant
- Soft Medical Triage
- Load, apply, and reset prompts functionality
- Session-scoped custom prompts
- Real-time prompt updates
src/interface/simplified_gradio_app.py
CHANGED
|
@@ -135,6 +135,63 @@ def create_simplified_interface():
|
|
| 135 |
gr.Markdown("### π§ Debug Info")
|
| 136 |
debug_info = gr.Markdown(value="")
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Instructions tab
|
| 139 |
with gr.TabItem("π Help", id="help"):
|
| 140 |
gr.Markdown("""
|
|
@@ -194,6 +251,103 @@ For emergencies, please call emergency services immediately.
|
|
| 194 |
"""Send example message."""
|
| 195 |
return handle_message(example_text, history, session)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
# Bind events
|
| 198 |
demo.load(
|
| 199 |
initialize_session,
|
|
@@ -245,6 +399,32 @@ For emergencies, please call emergency services immediately.
|
|
| 245 |
inputs=[chatbot, session_data],
|
| 246 |
outputs=[chatbot, status_box, session_data, msg]
|
| 247 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
return demo
|
| 250 |
|
|
|
|
| 135 |
gr.Markdown("### π§ Debug Info")
|
| 136 |
debug_info = gr.Markdown(value="")
|
| 137 |
|
| 138 |
+
# Edit Prompts tab
|
| 139 |
+
with gr.TabItem("π§ Edit Prompts", id="edit_prompts"):
|
| 140 |
+
gr.Markdown("## π§ Customize AI Prompts")
|
| 141 |
+
gr.Markdown("β οΈ **Note:** Changes apply only to your current session.")
|
| 142 |
+
|
| 143 |
+
# Prompt selector
|
| 144 |
+
with gr.Row():
|
| 145 |
+
prompt_selector = gr.Dropdown(
|
| 146 |
+
choices=[
|
| 147 |
+
"π Spiritual Monitor (Classifier)",
|
| 148 |
+
"π‘ Triage Question Generator",
|
| 149 |
+
"π Triage Response Evaluator",
|
| 150 |
+
"π₯ Medical Assistant",
|
| 151 |
+
"π©Ί Soft Medical Triage"
|
| 152 |
+
],
|
| 153 |
+
value="π Spiritual Monitor (Classifier)",
|
| 154 |
+
label="Select Prompt to Edit",
|
| 155 |
+
interactive=True
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
with gr.Column(scale=3):
|
| 160 |
+
# Prompt editor
|
| 161 |
+
prompt_editor = gr.Code(
|
| 162 |
+
label="System Prompt",
|
| 163 |
+
value="",
|
| 164 |
+
language="markdown",
|
| 165 |
+
lines=25,
|
| 166 |
+
interactive=True
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
with gr.Row():
|
| 170 |
+
load_prompt_btn = gr.Button("π₯ Load Prompt", scale=1)
|
| 171 |
+
apply_prompt_btn = gr.Button("β
Apply Changes", variant="primary", scale=2)
|
| 172 |
+
reset_prompt_btn = gr.Button("π Reset to Default", variant="secondary", scale=1)
|
| 173 |
+
|
| 174 |
+
prompt_status = gr.Markdown(value="", visible=True)
|
| 175 |
+
|
| 176 |
+
with gr.Column(scale=1):
|
| 177 |
+
gr.Markdown("### π Prompt Info")
|
| 178 |
+
prompt_info_display = gr.Markdown(value="""
|
| 179 |
+
**Select a prompt to edit**
|
| 180 |
+
|
| 181 |
+
Available prompts:
|
| 182 |
+
- π Spiritual Monitor
|
| 183 |
+
- π‘ Triage Questions
|
| 184 |
+
- π Triage Evaluation
|
| 185 |
+
- π₯ Medical Assistant
|
| 186 |
+
- π©Ί Soft Triage
|
| 187 |
+
|
| 188 |
+
**Tips:**
|
| 189 |
+
- Load prompt first
|
| 190 |
+
- Edit carefully
|
| 191 |
+
- Test changes
|
| 192 |
+
- Reset if needed
|
| 193 |
+
""")
|
| 194 |
+
|
| 195 |
# Instructions tab
|
| 196 |
with gr.TabItem("π Help", id="help"):
|
| 197 |
gr.Markdown("""
|
|
|
|
| 251 |
"""Send example message."""
|
| 252 |
return handle_message(example_text, history, session)
|
| 253 |
|
| 254 |
+
# Prompt editing handlers
|
| 255 |
+
def load_prompt(prompt_name: str):
|
| 256 |
+
"""Load selected prompt for editing."""
|
| 257 |
+
from src.core.spiritual_monitor import SYSTEM_PROMPT_SPIRITUAL_MONITOR
|
| 258 |
+
from src.core.soft_triage_manager import (
|
| 259 |
+
SYSTEM_PROMPT_TRIAGE_QUESTION,
|
| 260 |
+
SYSTEM_PROMPT_TRIAGE_EVALUATE
|
| 261 |
+
)
|
| 262 |
+
from src.config.prompts import (
|
| 263 |
+
SYSTEM_PROMPT_MEDICAL_ASSISTANT,
|
| 264 |
+
SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
prompts = {
|
| 268 |
+
"π Spiritual Monitor (Classifier)": SYSTEM_PROMPT_SPIRITUAL_MONITOR,
|
| 269 |
+
"π‘ Triage Question Generator": SYSTEM_PROMPT_TRIAGE_QUESTION,
|
| 270 |
+
"π Triage Response Evaluator": SYSTEM_PROMPT_TRIAGE_EVALUATE,
|
| 271 |
+
"π₯ Medical Assistant": SYSTEM_PROMPT_MEDICAL_ASSISTANT,
|
| 272 |
+
"π©Ί Soft Medical Triage": SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
prompt_text = prompts.get(prompt_name, "")
|
| 276 |
+
|
| 277 |
+
info = f"""**Loaded:** {prompt_name}
|
| 278 |
+
|
| 279 |
+
**Length:** {len(prompt_text)} characters
|
| 280 |
+
**Lines:** {len(prompt_text.split(chr(10)))} lines
|
| 281 |
+
|
| 282 |
+
**Status:** Ready to edit
|
| 283 |
+
"""
|
| 284 |
+
|
| 285 |
+
return prompt_text, info, "β
Prompt loaded successfully"
|
| 286 |
+
|
| 287 |
+
def apply_prompt_changes(prompt_name: str, prompt_text: str, session: SimplifiedSessionData):
|
| 288 |
+
"""Apply custom prompt changes."""
|
| 289 |
+
if session is None:
|
| 290 |
+
session = SimplifiedSessionData()
|
| 291 |
+
|
| 292 |
+
if not prompt_text.strip():
|
| 293 |
+
return "β Prompt cannot be empty", session
|
| 294 |
+
|
| 295 |
+
# Store custom prompt in session
|
| 296 |
+
if not hasattr(session, 'custom_prompts'):
|
| 297 |
+
session.custom_prompts = {}
|
| 298 |
+
|
| 299 |
+
session.custom_prompts[prompt_name] = prompt_text
|
| 300 |
+
|
| 301 |
+
# Apply to app components
|
| 302 |
+
try:
|
| 303 |
+
if prompt_name == "π Spiritual Monitor (Classifier)":
|
| 304 |
+
# Update spiritual monitor prompt
|
| 305 |
+
import src.core.spiritual_monitor as sm
|
| 306 |
+
sm.SYSTEM_PROMPT_SPIRITUAL_MONITOR = prompt_text
|
| 307 |
+
elif prompt_name == "π‘ Triage Question Generator":
|
| 308 |
+
import src.core.soft_triage_manager as stm
|
| 309 |
+
stm.SYSTEM_PROMPT_TRIAGE_QUESTION = prompt_text
|
| 310 |
+
elif prompt_name == "π Triage Response Evaluator":
|
| 311 |
+
import src.core.soft_triage_manager as stm
|
| 312 |
+
stm.SYSTEM_PROMPT_TRIAGE_EVALUATE = prompt_text
|
| 313 |
+
elif prompt_name == "π₯ Medical Assistant":
|
| 314 |
+
import src.config.prompts as p
|
| 315 |
+
p.SYSTEM_PROMPT_MEDICAL_ASSISTANT = prompt_text
|
| 316 |
+
elif prompt_name == "π©Ί Soft Medical Triage":
|
| 317 |
+
import src.config.prompts as p
|
| 318 |
+
p.SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE = prompt_text
|
| 319 |
+
|
| 320 |
+
status = f"""β
**Prompt Applied Successfully**
|
| 321 |
+
|
| 322 |
+
**Prompt:** {prompt_name}
|
| 323 |
+
**Length:** {len(prompt_text)} characters
|
| 324 |
+
**Session:** {session.session_id[:8]}...
|
| 325 |
+
|
| 326 |
+
β οΈ **Note:** Changes are active for this session only.
|
| 327 |
+
To revert, use "Reset to Default" button."""
|
| 328 |
+
|
| 329 |
+
return status, session
|
| 330 |
+
|
| 331 |
+
except Exception as e:
|
| 332 |
+
return f"β Error applying prompt: {str(e)}", session
|
| 333 |
+
|
| 334 |
+
def reset_prompt(prompt_name: str, session: SimplifiedSessionData):
|
| 335 |
+
"""Reset prompt to default."""
|
| 336 |
+
if session is None:
|
| 337 |
+
session = SimplifiedSessionData()
|
| 338 |
+
|
| 339 |
+
# Remove from custom prompts
|
| 340 |
+
if hasattr(session, 'custom_prompts') and prompt_name in session.custom_prompts:
|
| 341 |
+
del session.custom_prompts[prompt_name]
|
| 342 |
+
|
| 343 |
+
# Reload default
|
| 344 |
+
prompt_text, info, status = load_prompt(prompt_name)
|
| 345 |
+
|
| 346 |
+
# Reapply default
|
| 347 |
+
apply_status, session = apply_prompt_changes(prompt_name, prompt_text, session)
|
| 348 |
+
|
| 349 |
+
return prompt_text, f"π **Reset to Default**\n\n{info}", "β
Prompt reset successfully", session
|
| 350 |
+
|
| 351 |
# Bind events
|
| 352 |
demo.load(
|
| 353 |
initialize_session,
|
|
|
|
| 399 |
inputs=[chatbot, session_data],
|
| 400 |
outputs=[chatbot, status_box, session_data, msg]
|
| 401 |
)
|
| 402 |
+
|
| 403 |
+
# Prompt editing events
|
| 404 |
+
load_prompt_btn.click(
|
| 405 |
+
load_prompt,
|
| 406 |
+
inputs=[prompt_selector],
|
| 407 |
+
outputs=[prompt_editor, prompt_info_display, prompt_status]
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
apply_prompt_btn.click(
|
| 411 |
+
apply_prompt_changes,
|
| 412 |
+
inputs=[prompt_selector, prompt_editor, session_data],
|
| 413 |
+
outputs=[prompt_status, session_data]
|
| 414 |
+
)
|
| 415 |
+
|
| 416 |
+
reset_prompt_btn.click(
|
| 417 |
+
reset_prompt,
|
| 418 |
+
inputs=[prompt_selector, session_data],
|
| 419 |
+
outputs=[prompt_editor, prompt_info_display, prompt_status, session_data]
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# Auto-load prompt when selector changes
|
| 423 |
+
prompt_selector.change(
|
| 424 |
+
load_prompt,
|
| 425 |
+
inputs=[prompt_selector],
|
| 426 |
+
outputs=[prompt_editor, prompt_info_display, prompt_status]
|
| 427 |
+
)
|
| 428 |
|
| 429 |
return demo
|
| 430 |
|