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
import streamlit as st
|
| 5 |
+
from typing import Dict, List, Callable
|
| 6 |
+
|
| 7 |
+
def render_header():
|
| 8 |
+
"""Render application header"""
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Homeopathic Symptom Analyzer",
|
| 11 |
+
page_icon="πΏ",
|
| 12 |
+
layout="wide"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
col1, col2 = st.columns([1, 4])
|
| 16 |
+
with col1:
|
| 17 |
+
st.image("πΏ", width=80)
|
| 18 |
+
with col2:
|
| 19 |
+
st.title("Homeopathic Symptom Analyzer AI")
|
| 20 |
+
st.markdown("### Advanced Symptom Analysis & Remedy Matching")
|
| 21 |
+
|
| 22 |
+
st.markdown("---")
|
| 23 |
+
|
| 24 |
+
def render_sidebar() -> Dict:
|
| 25 |
+
"""Render sidebar with configuration options"""
|
| 26 |
+
with st.sidebar:
|
| 27 |
+
st.header("βοΈ Analysis Settings")
|
| 28 |
+
|
| 29 |
+
# Search options
|
| 30 |
+
st.subheader("Search Options")
|
| 31 |
+
search_web = st.checkbox("Search Web Resources", value=False,
|
| 32 |
+
help="Include web-based homeopathic resources")
|
| 33 |
+
search_docs = st.checkbox("Search Internal Documents", value=True,
|
| 34 |
+
help="Search in uploaded documents")
|
| 35 |
+
|
| 36 |
+
# Analysis depth
|
| 37 |
+
st.subheader("Analysis Depth")
|
| 38 |
+
analysis_level = st.select_slider(
|
| 39 |
+
"Analysis Detail",
|
| 40 |
+
options=["Basic", "Standard", "Comprehensive", "Deep"],
|
| 41 |
+
value="Comprehensive"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Upload documents
|
| 45 |
+
st.subheader("π Document Upload")
|
| 46 |
+
uploaded_files = st.file_uploader(
|
| 47 |
+
"Upload medical documents (PDF, DOCX, TXT)",
|
| 48 |
+
type=['pdf', 'docx', 'txt'],
|
| 49 |
+
accept_multiple_files=True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Agent intelligence settings
|
| 53 |
+
st.subheader("π€ AI Agent Settings")
|
| 54 |
+
agent_intelligence = st.select_slider(
|
| 55 |
+
"Agent Intelligence Level",
|
| 56 |
+
options=["Basic", "Intelligent", "Advanced", "Expert"],
|
| 57 |
+
value="Intelligent",
|
| 58 |
+
help="Higher levels provide more detailed reasoning"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
'search_web': search_web,
|
| 63 |
+
'search_docs': search_docs,
|
| 64 |
+
'analysis_level': analysis_level,
|
| 65 |
+
'uploaded_files': uploaded_files,
|
| 66 |
+
'agent_intelligence': agent_intelligence
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
def render_case_sheet_form() -> Dict:
|
| 70 |
+
"""Render comprehensive case sheet form"""
|
| 71 |
+
st.markdown("## π Patient Case Sheet")
|
| 72 |
+
|
| 73 |
+
with st.form("case_sheet_form"):
|
| 74 |
+
# Patient information
|
| 75 |
+
col1, col2 = st.columns(2)
|
| 76 |
+
with col1:
|
| 77 |
+
age = st.number_input("Age", min_value=0, max_value=120, value=30)
|
| 78 |
+
gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 79 |
+
with col2:
|
| 80 |
+
occupation = st.text_input("Occupation")
|
| 81 |
+
constitution = st.selectbox("Constitution Type",
|
| 82 |
+
["Lean", "Stout", "Average", "Not Sure"])
|
| 83 |
+
|
| 84 |
+
# Main complaint
|
| 85 |
+
st.subheader("Chief Complaint")
|
| 86 |
+
main_complaint = st.text_area(
|
| 87 |
+
"Describe your main health issue in detail",
|
| 88 |
+
height=100,
|
| 89 |
+
placeholder="Example: Severe headache on right side with throbbing pain..."
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Symptom details
|
| 93 |
+
st.subheader("Symptom Details")
|
| 94 |
+
col1, col2, col3 = st.columns(3)
|
| 95 |
+
|
| 96 |
+
with col1:
|
| 97 |
+
st.markdown("**Location**")
|
| 98 |
+
location = st.multiselect(
|
| 99 |
+
"Where are symptoms located?",
|
| 100 |
+
["Head", "Stomach", "Chest", "Back", "Limbs", "Joints", "General"]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with col2:
|
| 104 |
+
st.markdown("**Sensation**")
|
| 105 |
+
sensation = st.multiselect(
|
| 106 |
+
"What does it feel like?",
|
| 107 |
+
["Burning", "Throbbing", "Stabbing", "Aching", "Pressing", "Numbness"]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with col3:
|
| 111 |
+
st.markdown("**Intensity**")
|
| 112 |
+
intensity = st.slider("Pain/Symptom Intensity", 1, 10, 5)
|
| 113 |
+
|
| 114 |
+
# Modalities
|
| 115 |
+
st.subheader("Modalities")
|
| 116 |
+
col1, col2 = st.columns(2)
|
| 117 |
+
with col1:
|
| 118 |
+
aggravations = st.text_area(
|
| 119 |
+
"What makes it worse?",
|
| 120 |
+
placeholder="Example: Motion, heat, noise, after eating..."
|
| 121 |
+
)
|
| 122 |
+
with col2:
|
| 123 |
+
ameliorations = st.text_area(
|
| 124 |
+
"What makes it better?",
|
| 125 |
+
placeholder="Example: Rest, pressure, cold applications..."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Time factors
|
| 129 |
+
st.subheader("Timing")
|
| 130 |
+
timing = st.text_area(
|
| 131 |
+
"When do symptoms occur or change?",
|
| 132 |
+
placeholder="Example: Worse in morning, better in evening, periodic every week..."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Emotional state
|
| 136 |
+
st.subheader("Emotional & Mental State")
|
| 137 |
+
emotional_state = st.text_area(
|
| 138 |
+
"Describe emotional state, fears, anxieties",
|
| 139 |
+
placeholder="Example: Anxious about health, irritable, fear of crowds..."
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Generalities
|
| 143 |
+
st.subheader("General Symptoms")
|
| 144 |
+
generalities = st.text_area(
|
| 145 |
+
"Other symptoms, food desires/aversions, thermal preferences",
|
| 146 |
+
placeholder="Example: Thirst for cold water, aversion to fat, chilly..."
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Submit button
|
| 150 |
+
submit_button = st.form_submit_button("π Analyze Symptoms")
|
| 151 |
+
|
| 152 |
+
if submit_button:
|
| 153 |
+
return {
|
| 154 |
+
'age': age,
|
| 155 |
+
'gender': gender,
|
| 156 |
+
'occupation': occupation,
|
| 157 |
+
'constitution': constitution,
|
| 158 |
+
'main_complaint': main_complaint,
|
| 159 |
+
'location': location,
|
| 160 |
+
'sensation': sensation,
|
| 161 |
+
'intensity': intensity,
|
| 162 |
+
'aggravations': aggravations,
|
| 163 |
+
'ameliorations': ameliorations,
|
| 164 |
+
'timing': timing,
|
| 165 |
+
'emotional_state': emotional_state,
|
| 166 |
+
'generalities': generalities
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
return None
|
| 170 |
+
|
| 171 |
+
def render_remedy_results(results: List[Dict], analysis: Dict):
|
| 172 |
+
"""Render remedy matching results with detailed analysis"""
|
| 173 |
+
st.markdown("## π Analysis Results")
|
| 174 |
+
|
| 175 |
+
# Summary card
|
| 176 |
+
with st.expander("π Analysis Summary", expanded=True):
|
| 177 |
+
col1, col2, col3 = st.columns(3)
|
| 178 |
+
with col1:
|
| 179 |
+
st.metric("Key Symptoms Identified", len(analysis.get('key_symptoms', [])))
|
| 180 |
+
with col2:
|
| 181 |
+
st.metric("SRP Symptoms", len(analysis.get('strange_rare_peculiar', [])))
|
| 182 |
+
with col3:
|
| 183 |
+
st.metric("Top Match Score", f"{results[0]['score']:.1f}%" if results else "N/A")
|
| 184 |
+
|
| 185 |
+
# Display each remedy result
|
| 186 |
+
for i, result in enumerate(results[:5], 1):
|
| 187 |
+
with st.expander(f"π #{i}: {result['remedy']} ({result['score']:.1f}% Match)", expanded=i==1):
|
| 188 |
+
|
| 189 |
+
# Header with match score
|
| 190 |
+
col1, col2 = st.columns([3, 1])
|
| 191 |
+
with col1:
|
| 192 |
+
st.subheader(result['remedy'])
|
| 193 |
+
st.caption(result['data']['description'])
|
| 194 |
+
with col2:
|
| 195 |
+
st.metric("Match Score", f"{result['score']:.1f}%")
|
| 196 |
+
|
| 197 |
+
# Tabs for different analyses
|
| 198 |
+
tab1, tab2, tab3, tab4 = st.tabs(["Physical", "Modalities", "Constitutional", "Analysis"])
|
| 199 |
+
|
| 200 |
+
with tab1:
|
| 201 |
+
st.markdown("### Physical Symptoms")
|
| 202 |
+
for keynote in result['data']['keynotes']:
|
| 203 |
+
st.markdown(f"β’ {keynote}")
|
| 204 |
+
|
| 205 |
+
with tab2:
|
| 206 |
+
st.markdown("### Modalities")
|
| 207 |
+
col1, col2 = st.columns(2)
|
| 208 |
+
with col1:
|
| 209 |
+
st.markdown("**Aggravations:**")
|
| 210 |
+
for agg in result['data']['modalities']['aggravations']:
|
| 211 |
+
st.markdown(f"β’ {agg}")
|
| 212 |
+
with col2:
|
| 213 |
+
st.markdown("**Ameliorations:**")
|
| 214 |
+
for amel in result['data']['modalities']['ameliorations']:
|
| 215 |
+
st.markdown(f"β’ {amel}")
|
| 216 |
+
|
| 217 |
+
# Modality match analysis
|
| 218 |
+
if result['modality_match']['reasoning']:
|
| 219 |
+
st.markdown("**Modality Match Analysis:**")
|
| 220 |
+
for reason in result['modality_match']['reasoning']:
|
| 221 |
+
st.success(f"β {reason}")
|
| 222 |
+
|
| 223 |
+
with tab3:
|
| 224 |
+
st.markdown("### Constitutional Profile")
|
| 225 |
+
st.info(result['data']['constitutional'])
|
| 226 |
+
st.markdown("### Emotional Profile")
|
| 227 |
+
st.info(result['data']['emotional'])
|
| 228 |
+
|
| 229 |
+
with tab4:
|
| 230 |
+
st.markdown("### Detailed Analysis")
|
| 231 |
+
st.markdown(result['detailed_analysis'])
|
| 232 |
+
|
| 233 |
+
# Reasoning section
|
| 234 |
+
st.markdown("#### Reasoning for Selection")
|
| 235 |
+
st.markdown("""
|
| 236 |
+
1. **Symptom Correspondence**: Key symptoms match remedy picture
|
| 237 |
+
2. **Modality Alignment**: Aggravations/ameliorations correspond
|
| 238 |
+
3. **Constitutional Fit**: Patient profile matches remedy type
|
| 239 |
+
4. **Emotional Match**: Mental/emotional state aligns
|
| 240 |
+
""")
|
| 241 |
+
|
| 242 |
+
st.divider()
|
| 243 |
+
|
| 244 |
+
def render_agent_intelligence(analysis: Dict, settings: Dict):
|
| 245 |
+
"""Render AI agent's intelligent analysis"""
|
| 246 |
+
st.markdown("## π€ AI Agent Analysis")
|
| 247 |
+
|
| 248 |
+
with st.chat_message("assistant", avatar="π€"):
|
| 249 |
+
st.markdown("### Intelligent Symptom Analysis")
|
| 250 |
+
|
| 251 |
+
# Generate intelligent analysis based on settings
|
| 252 |
+
intelligence_level = settings.get('agent_intelligence', 'Intelligent')
|
| 253 |
+
|
| 254 |
+
analysis_text = f"""
|
| 255 |
+
Based on my analysis ({intelligence_level} mode), here's my assessment:
|
| 256 |
+
|
| 257 |
+
**Primary Analysis:**
|
| 258 |
+
- **Chief Complaint**: {analysis.get('physical_symptoms', [{}])[0].get('description', 'Not specified') if analysis.get('physical_symptoms') else 'Not specified'}
|
| 259 |
+
- **Key Modalities**: {', '.join(analysis.get('modalities', {}).get('aggravations', [])[:3]) if analysis.get('modalities', {}).get('aggravations') else 'Not specified'}
|
| 260 |
+
- **Emotional State**: {', '.join(analysis.get('emotional_state', {}).get('primary_emotions', [])) if analysis.get('emotional_state', {}).get('primary_emotions') else 'Not specified'}
|
| 261 |
+
|
| 262 |
+
**Pattern Recognition:**
|
| 263 |
+
"""
|
| 264 |
+
|
| 265 |
+
if intelligence_level in ["Advanced", "Expert"]:
|
| 266 |
+
analysis_text += """
|
| 267 |
+
- Detected potential miasmatic influence based on symptom patterns
|
| 268 |
+
- Noticed characteristic symptom combinations suggesting specific remedy families
|
| 269 |
+
- Identified paradoxical symptoms that are highly characteristic
|
| 270 |
+
"""
|
| 271 |
+
else:
|
| 272 |
+
analysis_text += """
|
| 273 |
+
- Symptoms suggest acute inflammatory process
|
| 274 |
+
- Modalities indicate thermal preferences
|
| 275 |
+
- Emotional state aligns with common remedy pictures
|
| 276 |
+
"""
|
| 277 |
+
|
| 278 |
+
analysis_text += f"""
|
| 279 |
+
|
| 280 |
+
**Recommendation Approach:**
|
| 281 |
+
Based on the {intelligence_level} analysis, I'm considering:
|
| 282 |
+
1. Matching physical symptoms with modalities
|
| 283 |
+
2. Aligning constitutional factors
|
| 284 |
+
3. Considering emotional/mental state
|
| 285 |
+
4. Evaluating time factors and periodicity
|
| 286 |
+
|
| 287 |
+
**Next Steps:**
|
| 288 |
+
Consider consultation with a qualified homeopath for:
|
| 289 |
+
- Potency selection
|
| 290 |
+
- Repetition schedule
|
| 291 |
+
- Constitutional follow-up
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
st.markdown(analysis_text)
|
| 295 |
+
|
| 296 |
+
def render_document_search_results(search_results: List[Dict]):
|
| 297 |
+
"""Render document search results"""
|
| 298 |
+
if search_results:
|
| 299 |
+
st.markdown("## π Document Search Results")
|
| 300 |
+
|
| 301 |
+
for result in search_results:
|
| 302 |
+
with st.expander(f"π {result['document']}"):
|
| 303 |
+
st.markdown(f"**Match**: `{result['match']}`")
|
| 304 |
+
st.markdown(f"**Context**: {result['context']}")
|
| 305 |
+
st.caption(f"Relevance: {result['relevance']:.2f}")
|
| 306 |
+
else:
|
| 307 |
+
st.info("No matches found in uploaded documents.")
|
| 308 |
+
|
| 309 |
+
def render_disclaimer():
|
| 310 |
+
"""Render medical disclaimer"""
|
| 311 |
+
st.markdown("---")
|
| 312 |
+
st.warning("""
|
| 313 |
+
**IMPORTANT DISCLAIMER**: This application is for educational and informational purposes only.
|
| 314 |
+
It is not a substitute for professional medical advice, diagnosis, or treatment.
|
| 315 |
+
Always consult a qualified homeopath or medical professional for health concerns.
|
| 316 |
+
""")
|