Spaces:
Sleeping
Sleeping
feat: Add HTML formatting for prompt visualization
Browse files- Added format_prompt_with_html() function
- Highlights sections with colored headers
- Highlights keywords (IMPORTANT, CRITICAL, etc.)
- Highlights bullet points and numbered lists
- Highlights JSON/code blocks
- Styled status messages with colored boxes
- Better visual hierarchy for prompt structure
170 unmodified lines17117217317417517617717817918018118218318418518618718818919019119219319419556 unmodified lines25225325425525625716 unmodified lines2742752762772782792802812822832842852862872881 unmodified line29029129229329429529620 unmodified lines31731831932032132232332432532632732832933033133233333433510 unmodified lines346347348349350351352170 unmodified linesapply_prompt_btn = gr.Button("β Apply Changes", variant="primary", scale=2)reset_prompt_btn = gr.Button("π Reset to Default", variant="secondary", scale=1)prompt_status = gr.Markdown(value="", visible=True)with gr.Column(scale=1):gr.Markdown("### π Prompt Info")prompt_info_display = gr.Markdown(value="""**Select a prompt to edit**Available prompts:- π Spiritual Monitor- π‘ Triage Questions- π Triage Evaluation- π₯ Medical Assistant- π©Ί Soft Triage**Tips:**- Load prompt first- Edit carefully- Test changes- Reset if needed""")# Instructions tab56 unmodified linesreturn handle_message(example_text, history, session)# Prompt editing handlersdef load_prompt(prompt_name: str):"""Load selected prompt for editing."""from src.core.spiritual_monitor import SYSTEM_PROMPT_SPIRITUAL_MONITOR16 unmodified linesprompt_text = prompts.get(prompt_name, "")info = f"""**Loaded:** {prompt_name}**Length:** {len(prompt_text)} characters**Lines:** {len(prompt_text.split(chr(10)))} lines**Status:** Ready to edit"""return prompt_text, info, "β Prompt loaded successfully"def apply_prompt_changes(prompt_name: str, prompt_text: str, session: SimplifiedSessionData):"""Apply custom prompt changes."""1 unmodified linesession = SimplifiedSessionData()if not prompt_text.strip():return "β Prompt cannot be empty", session# Store custom prompt in sessionif not hasattr(session, 'custom_prompts'):20 unmodified linesimport src.config.prompts as pp.SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE = prompt_textstatus = f"""β **Prompt Applied Successfully****Prompt:** {prompt_name}**Length:** {len(prompt_text)} characters**Session:** {session.session_id[:8]}...β οΈ **Note:** Changes are active for this session only.To revert, use "Reset to Default" button."""return status, sessionexcept Exception as e:return f"β Error applying prompt: {str(e)}", sessiondef reset_prompt(prompt_name: str, session: SimplifiedSessionData):"""Reset prompt to default."""10 unmodified lines# Reapply defaultapply_status, session = apply_prompt_changes(prompt_name, prompt_text, session)return prompt_text, f"π **Reset to Default**\n\n{info}", "β Prompt reset successfully", session# Bind eventsdemo.load(170 unmodified lines17117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020156 unmodified lines25825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632716 unmodified lines3443453463473483493503513523533543553563573583593603613623633643653663673683693703713721 unmodified line37437537637737837938038138238338420 unmodified lines40540640740840941041141241341441541641741841942042142242342442542642742842943043110 unmodified lines442443444445446447448449450451452453170 unmodified linesapply_prompt_btn = gr.Button("β Apply Changes", variant="primary", scale=2)reset_prompt_btn = gr.Button("π Reset to Default", variant="secondary", scale=1)prompt_status = gr.HTML(value="", visible=True)with gr.Column(scale=1):gr.Markdown("### π Prompt Info")prompt_info_display = gr.HTML(value="""<div style="font-family: system-ui; padding: 1em; background-color: #f9fafb; border-radius: 8px;"><p><strong>Select a prompt to edit</strong></p><p><strong>Available prompts:</strong></p><ul style="margin-left: 1em;"><li>π Spiritual Monitor</li><li>π‘ Triage Questions</li><li>π Triage Evaluation</li><li>π₯ Medical Assistant</li><li>π©Ί Soft Triage</li></ul><p><strong>Tips:</strong></p><ul style="margin-left: 1em;"><li>Load prompt first</li><li>Edit carefully</li><li>Test changes</li><li>Reset if needed</li></ul></div>""")# Instructions tab56 unmodified linesreturn handle_message(example_text, history, session)# Prompt editing handlersdef format_prompt_with_html(prompt_text: str) -> str:"""Format prompt with HTML tags for better visualization."""import re# Add HTML structureformatted = prompt_text# Highlight sections (UPPERCASE headers)formatted = re.sub(r'^([A-Z][A-Z\s]+:)$',r'<h3 style="color: #2563eb; margin-top: 1em; margin-bottom: 0.5em;">\1</h3>',formatted,flags=re.MULTILINE)# Highlight subsections (Title Case headers)formatted = re.sub(r'^([A-Z][a-zA-Z\s]+:)$',r'<h4 style="color: #7c3aed; margin-top: 0.8em; margin-bottom: 0.3em;">\1</h4>',formatted,flags=re.MULTILINE)# Highlight bullet pointsformatted = re.sub(r'^([-β’]\s+.+)$',r'<li style="margin-left: 1.5em; color: #059669;">\1</li>',formatted,flags=re.MULTILINE)# Highlight numbered listsformatted = re.sub(r'^(\d+\.\s+.+)$',r'<li style="margin-left: 1.5em; color: #dc2626;">\1</li>',formatted,flags=re.MULTILINE)# Highlight important keywordskeywords = ['IMPORTANT', 'CRITICAL', 'REQUIRED', 'MUST', 'SHALL','WARNING', 'NOTE', 'TASK', 'GOAL', 'OUTPUT']for keyword in keywords:formatted = re.sub(f'\\b({keyword})\\b',r'<strong style="color: #dc2626; background-color: #fef2f2; padding: 2px 4px; border-radius: 3px;">\1</strong>',formatted,flags=re.IGNORECASE)# Highlight JSON/code blocksformatted = re.sub(r'(\{[^}]+\})',r'<code style="background-color: #f3f4f6; padding: 2px 6px; border-radius: 3px; font-family: monospace;">\1</code>',formatted)# Wrap in containerformatted = f'<div style="font-family: system-ui; line-height: 1.6; padding: 1em;">{formatted}</div>'return formatteddef load_prompt(prompt_name: str):"""Load selected prompt for editing."""from src.core.spiritual_monitor import SYSTEM_PROMPT_SPIRITUAL_MONITOR16 unmodified linesprompt_text = prompts.get(prompt_name, "")# Format with HTML for displayformatted_html = format_prompt_with_html(prompt_text)info = f"""**Loaded:** {prompt_name}**Length:** {len(prompt_text)} characters**Lines:** {len(prompt_text.split(chr(10)))} lines**Status:** Ready to edit---### π Formatted Preview:{formatted_html}"""load_status = """<div style="padding: 1em; background-color: #ecfdf5; border-left: 4px solid #10b981; border-radius: 4px;"><h4 style="color: #059669; margin-top: 0;">β Prompt Loaded</h4><p style="margin-bottom: 0;">Ready to edit. Make your changes and click "Apply Changes".</p></div>"""return prompt_text, info, load_statusdef apply_prompt_changes(prompt_name: str, prompt_text: str, session: SimplifiedSessionData):"""Apply custom prompt changes."""1 unmodified linesession = SimplifiedSessionData()if not prompt_text.strip():error_html = """<div style="padding: 1em; background-color: #fef2f2; border-left: 4px solid #dc2626; border-radius: 4px;"><h4 style="color: #dc2626; margin-top: 0;">β Error</h4><p style="margin-bottom: 0;">Prompt cannot be empty</p></div>"""return error_html, session# Store custom prompt in sessionif not hasattr(session, 'custom_prompts'):20 unmodified linesimport src.config.prompts as pp.SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE = prompt_textstatus = f"""<div style="padding: 1em; background-color: #ecfdf5; border-left: 4px solid #10b981; border-radius: 4px;"><h4 style="color: #059669; margin-top: 0;">β Prompt Applied Successfully</h4><p><strong>Prompt:</strong> {prompt_name}</p><p><strong>Length:</strong> {len(prompt_text)} characters</p><p><strong>Session:</strong> <code>{session.session_id[:8]}...</code></p><p style="color: #d97706; margin-bottom: 0;">β οΈ <strong>Note:</strong> Changes are active for this session only.To revert, use "Reset to Default" button.</p></div>"""return status, sessionexcept Exception as e:error_html = f"""<div style="padding: 1em; background-color: #fef2f2; border-left: 4px solid #dc2626; border-radius: 4px;"><h4 style="color: #dc2626; margin-top: 0;">β Error Applying Prompt</h4><p style="margin-bottom: 0;"><code>{str(e)}</code></p></div>"""return error_html, sessiondef reset_prompt(prompt_name: str, session: SimplifiedSessionData):"""Reset prompt to default."""10 unmodified lines# Reapply defaultapply_status, session = apply_prompt_changes(prompt_name, prompt_text, session)reset_status = """<div style="padding: 1em; background-color: #eff6ff; border-left: 4px solid #3b82f6; border-radius: 4px;"><h4 style="color: #2563eb; margin-top: 0;">π Reset to Default</h4><p style="margin-bottom: 0;">Prompt has been restored to its original version.</p></div>"""return prompt_text, info, reset_status, session# Bind eventsdemo.load(
src/interface/simplified_gradio_app.py+125-24