Spaces:
Sleeping
Sleeping
File size: 12,336 Bytes
17f3ad3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
import logging
import gradio as gr
from typing import Optional
from src.interface.session_manager import SimplifiedSessionData
logger = logging.getLogger(__name__)
def format_prompt_with_html(prompt_text: str) -> str:
"""Format prompt with HTML tags for better visualization."""
import re
import html
# Escape HTML first to prevent injection
formatted = html.escape(prompt_text)
# Highlight XML-like opening tags
formatted = re.sub(
r'<([a-z_]+)(?:\s+[^>]*)?>',
r'<span style="color: #2563eb; font-weight: 600;"><\1></span>',
formatted,
flags=re.IGNORECASE
)
# Highlight XML-like closing tags
formatted = re.sub(
r'</([a-z_]+)>',
r'<span style="color: #7c3aed; font-weight: 600;"></\1></span>',
formatted,
flags=re.IGNORECASE
)
# Highlight XML attributes
formatted = re.sub(
r'([a-z_]+)="([^"]+)"',
r'<span style="color: #059669;">\1</span>=<span style="color: #d97706;">"\2"</span>',
formatted,
flags=re.IGNORECASE
)
# Highlight bullet points
formatted = re.sub(
r'^([-β’]\s+.+)$',
r'<div style="margin-left: 1.5em; color: #059669;">\1</div>',
formatted,
flags=re.MULTILINE
)
# Highlight numbered lists
formatted = re.sub(
r'^(\d+\.\s+.+)$',
r'<div style="margin-left: 1.5em; color: #dc2626;">\1</div>',
formatted,
flags=re.MULTILINE
)
# Highlight important keywords
keywords = [
'IMPORTANT', 'CRITICAL', 'REQUIRED', 'MUST', 'SHALL',
'WARNING', 'NOTE', 'TASK', 'GOAL', 'OUTPUT', 'ANY'
]
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 blocks
formatted = re.sub(
r'(\{[^}]+\})',
r'<code style="background-color: #f3f4f6; padding: 2px 6px; border-radius: 3px; font-family: monospace; font-size: 0.9em;">\1</code>',
formatted
)
# Convert newlines to <br> for proper display
formatted = formatted.replace('\n', '<br>')
# Wrap in container with monospace font for code-like appearance
formatted = f'<div style="font-family: \'Courier New\', monospace; line-height: 1.8; padding: 1em; background-color: #f9fafb; border-radius: 8px; overflow-x: auto;">{formatted}</div>'
return formatted
def _prompt_name_to_agent(prompt_name: str) -> str:
"""Map UI prompt selection to internal agent/prompt key."""
mapping = {
"π Spiritual Monitor (Classifier)": "SpiritualDistressAnalyzer",
"π‘ Soft Spiritual Triage": "SoftSpiritualTriage",
"π Triage Response Evaluator": "TriageResponseEvaluator",
"π₯ Medical Assistant": "MedicalAssistant",
"π©Ί Soft Medical Triage": "SoftMedicalTriage",
}
return mapping.get(prompt_name, prompt_name)
def load_prompt(prompt_name: str, session: Optional[SimplifiedSessionData] = None):
"""Load selected prompt for editing using enhanced prompt editor."""
try:
from src.interface.enhanced_prompt_editor import EnhancedPromptEditor
# Initialize enhanced editor
editor = EnhancedPromptEditor()
# Get session ID
session_id = getattr(session, 'session_id', 'default_session') if session else 'default_session'
# Use enhanced editor to load prompt
prompt_content, info_html, status_html = editor.load_prompt_for_editing(prompt_name, session_id)
return prompt_content, info_html, status_html
except Exception as e:
# Fallback to old system if enhanced editor fails
logger.warning(f"Enhanced prompt editor failed, using fallback: {e}")
from src.core.spiritual_monitor import SYSTEM_PROMPT_SPIRITUAL_MONITOR
from src.core.soft_triage_manager import (
SYSTEM_PROMPT_TRIAGE_QUESTION,
SYSTEM_PROMPT_TRIAGE_EVALUATE
)
from src.config.prompts import (
SYSTEM_PROMPT_MEDICAL_ASSISTANT,
SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
)
prompts = {
"π Spiritual Monitor (Classifier)": SYSTEM_PROMPT_SPIRITUAL_MONITOR,
"π‘ Soft Spiritual Triage": SYSTEM_PROMPT_TRIAGE_QUESTION,
"π Triage Response Evaluator": SYSTEM_PROMPT_TRIAGE_EVALUATE,
"π₯ Medical Assistant": SYSTEM_PROMPT_MEDICAL_ASSISTANT,
"π©Ί Soft Medical Triage": SYSTEM_PROMPT_SOFT_MEDICAL_TRIAGE
}
prompt_text = prompts.get(prompt_name, "")
info = f"""**Loaded:** {prompt_name}
**Length:** {len(prompt_text)} characters
**Status:** Fallback mode (enhanced editor unavailable)"""
status = """<div style="padding: 1em; background-color: #fffbeb; border-left: 4px solid #f59e0b; border-radius: 4px;">
<h4 style="color: #d97706; margin-top: 0;">β οΈ Fallback Mode</h4>
<p style="margin-bottom: 0;">Using basic prompt editor. Enhanced features unavailable.</p>
</div>"""
return prompt_text, info, status
def apply_prompt_changes(prompt_name: str, prompt_text: str, session: SimplifiedSessionData):
"""Apply custom prompt changes using enhanced prompt editor."""
try:
from src.interface.enhanced_prompt_editor import EnhancedPromptEditor
if session is None:
session = SimplifiedSessionData()
# Initialize enhanced editor
editor = EnhancedPromptEditor()
# Get session ID
session_id = getattr(session, 'session_id', 'default_session')
# Use enhanced editor to apply changes
status_html, success = editor.apply_prompt_changes(prompt_name, prompt_text, session_id)
if success:
# Also store in session for backward compatibility
if not hasattr(session, 'custom_prompts'):
session.custom_prompts = {}
agent_key = _prompt_name_to_agent(prompt_name)
session.custom_prompts[agent_key] = prompt_text
# Apply to session app instance if available
if hasattr(session, 'app_instance') and hasattr(session.app_instance, 'set_prompt_overrides'):
session.app_instance.set_prompt_overrides(session.custom_prompts)
return status_html, session
except Exception as e:
# Fallback to old system
logger.warning(f"Enhanced prompt editor failed, using fallback: {e}")
if session is None:
session = 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 session (session-scoped)
if not hasattr(session, 'custom_prompts'):
session.custom_prompts = {}
agent_key = _prompt_name_to_agent(prompt_name)
session.custom_prompts[agent_key] = prompt_text
status = f"""<div style="padding: 1em; background-color: #fffbeb; border-left: 4px solid #f59e0b; border-radius: 4px;">
<h4 style="color: #d97706; margin-top: 0;">β οΈ Fallback Mode - Changes Applied</h4>
<p><strong>Prompt:</strong> {prompt_name}</p>
<p><strong>Length:</strong> {len(prompt_text)} characters</p>
<p style="margin-bottom: 0;">Enhanced features unavailable, using basic session storage.</p>
</div>"""
return status, session
def reset_prompt(prompt_name: str, session: SimplifiedSessionData):
"""Reset prompt to default using enhanced prompt editor."""
try:
from src.interface.enhanced_prompt_editor import EnhancedPromptEditor
if session is None:
session = SimplifiedSessionData()
# Initialize enhanced editor
editor = EnhancedPromptEditor()
# Get session ID
session_id = getattr(session, 'session_id', 'default_session')
# Use enhanced editor to reset prompt
prompt_content, info_html, status_html = editor.reset_prompt_to_default(prompt_name, session_id)
# Also remove from session for backward compatibility
agent_key = _prompt_name_to_agent(prompt_name)
if hasattr(session, 'custom_prompts') and agent_key in session.custom_prompts:
del session.custom_prompts[agent_key]
# Apply to session app instance if available
if hasattr(session, 'app_instance') and hasattr(session.app_instance, 'set_prompt_overrides'):
session.app_instance.set_prompt_overrides(getattr(session, 'custom_prompts', {}))
return prompt_content, info_html, status_html, session
except Exception as e:
# Fallback to old system
logger.warning(f"Enhanced prompt editor failed, using fallback: {e}")
if session is None:
session = SimplifiedSessionData()
# Remove from custom prompts
agent_key = _prompt_name_to_agent(prompt_name)
if hasattr(session, 'custom_prompts') and agent_key in session.custom_prompts:
del session.custom_prompts[agent_key]
# Reload default
prompt_text, info, status = load_prompt(prompt_name, session)
reset_status = """<div style="padding: 1em; background-color: #fffbeb; border-left: 4px solid #f59e0b; border-radius: 4px;">
<h4 style="color: #d97706; margin-top: 0;">π Fallback Mode - Reset Complete</h4>
<p style="margin-bottom: 0;">Prompt restored using basic system. Enhanced features unavailable.</p>
</div>"""
return prompt_text, info, reset_status, session
def promote_prompt_to_file(prompt_name: str, session: SimplifiedSessionData):
"""Promote session prompt override to permanent file."""
try:
from src.interface.enhanced_prompt_editor import EnhancedPromptEditor
if session is None:
return """<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;">No session data available</p>
</div>""", session
# Initialize enhanced editor
editor = EnhancedPromptEditor()
# Get session ID
session_id = getattr(session, 'session_id', 'default_session')
# Use enhanced editor to promote prompt
status_html, success = editor.promote_session_to_file(prompt_name, session_id)
return status_html, session
except Exception as e:
logger.warning(f"Enhanced prompt editor failed: {e}")
return f"""<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;">Failed to promote prompt: {str(e)}</p>
</div>""", session
def validate_prompt_syntax(prompt_text: str):
"""Validate prompt syntax and structure."""
try:
from src.interface.enhanced_prompt_editor import EnhancedPromptEditor
# Initialize enhanced editor
editor = EnhancedPromptEditor()
# Use enhanced editor to validate prompt
validation_html, is_valid = editor.validate_prompt_syntax(prompt_text)
return validation_html
except Exception as e:
logger.warning(f"Enhanced prompt editor failed: {e}")
return f"""<div style="padding: 1em; background-color: #fef2f2; border-left: 4px solid #dc2626; border-radius: 4px;">
<h4 style="color: #dc2626; margin-top: 0;">β Validation Error</h4>
<p style="margin-bottom: 0;">Failed to validate prompt: {str(e)}</p>
</div>"""
|