Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
from groq import Groq | |
import os | |
# -------------------------- | |
# 1. Dataset Setup (Updated with Urdu) | |
# -------------------------- | |
data = { | |
"symptoms_en": ["headache", "fever", "insomnia", "anxiety", "cough"], | |
"symptoms_es": ["dolor de cabeza", "fiebre", "insomnio", "ansiedad", "tos"], | |
"symptoms_hi": ["सिरदर्द", "बुखार", "अनिद्रा", "चिंता", "खांसी"], | |
"symptoms_ur": ["سر درد", "بخار", "بے خوابی", "اضطراب", "کھانسی"], | |
"remedy": ["Belladonna", "Aconitum", "Coffea", "Argentum nitricum", "Drosera"], | |
"potency": ["30C"]*5, | |
"usage": ["3 times daily"]*5 | |
} | |
df = pd.DataFrame(data) | |
# -------------------------- | |
# 2. Groq Setup (Multilingual Processing) | |
# -------------------------- | |
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
def analyze_symptoms(text, language): | |
"""Use Groq's Llama 3 for symptom analysis""" | |
lang_map = { | |
'en': 'English', | |
'es': 'Spanish', | |
'hi': 'Hindi', | |
'ur': 'Urdu' | |
} | |
prompt = f"""Task: Extract medical symptoms from this {lang_map[language]} text. | |
Text: {text} | |
Output format: Comma-separated symptoms in {lang_map[language]}. | |
Example: headache, muscle pain, dry cough""" | |
try: | |
chat_completion = client.chat.completions.create( | |
messages=[{"role": "user", "content": prompt}], | |
model="llama3-70b-8192", | |
temperature=0.1 | |
) | |
return [s.strip() for s in chat_completion.choices[0].message.content.split(",")] | |
except Exception as e: | |
st.error(f"Error in Groq API: {str(e)}") | |
return [] | |
# -------------------------- | |
# 3. Streamlit UI (Multilingual Interface) | |
# -------------------------- | |
st.set_page_config(page_title="Homeo Doctor", page_icon="🌿") | |
# Language selection | |
language = st.sidebar.selectbox("भाषा चुनें/Select Language", | |
['English', 'Español', 'हिन्दी', 'اردو'], | |
format_func=lambda x: x) | |
lang_code = { | |
'English': 'en', | |
'Español': 'es', | |
'हिन्दी': 'hi', | |
'اردو': 'ur' | |
}[language] | |
# Header with dynamic language | |
headers = { | |
'en': "🌿 Homeopathic Doctor", | |
'es': "🌿 Doctor Homeopático", | |
'hi': "🌿 होम्योपैथिक डॉक्टर", | |
'ur': "🌿 ہومیوپیتھک ڈاکٹر" | |
} | |
st.header(headers[lang_code]) | |
# MCQ Symptoms | |
symptom_col = f"symptoms_{lang_code}" | |
symptoms = df[symptom_col].tolist() | |
selected = st.multiselect( | |
label={ | |
'en': "Select your symptoms:", | |
'es': "Seleccione sus síntomas:", | |
'hi': "अपने लक्षण चुनें:", | |
'ur': "اپنی علامات منتخب کریں:" | |
}[lang_code], | |
options=symptoms | |
) | |
# Chat Input | |
chat_input = st.text_input( | |
label={ | |
'en': "Describe other symptoms:", | |
'es': "Describa otros síntomas:", | |
'hi': "अन्य लक्षण बताएं:", | |
'ur': "دیگر علامات بیان کریں:" | |
}[lang_code], | |
placeholder={ | |
'en': "Type symptoms not listed above...", | |
'ur': "اوپر درج نہ کی گئی علامات ٹائپ کریں..." | |
}[lang_code] if lang_code == 'ur' else "" | |
) | |
# Diagnosis Logic | |
if st.button({ | |
'en': "Get Recommendation", | |
'es': "Obtener Recomendación", | |
'hi': "सिफारिश प्राप्त करें", | |
'ur': "تجویز حاصل کریں" | |
}[lang_code]): | |
# Process chat input | |
if chat_input: | |
analyzed_symptoms = analyze_symptoms(chat_input, lang_code) | |
selected += [s for s in analyzed_symptoms if s in symptoms] | |
# Find best remedy | |
if not selected: | |
st.warning({ | |
'en': "Please select or describe symptoms!", | |
'ur': "براہ کرم علامات منتخب کریں یا بیان کریں!" | |
}[lang_code] if lang_code == 'ur' else "") | |
else: | |
matches = df[df[symptom_col].isin(selected)] | |
if not matches.empty: | |
remedy = matches.iloc[0] | |
st.success(f""" | |
**{remedy['remedy']}** | |
{{ | |
'en': "Potency: {remedy['potency']}", | |
'ur': "طاقت: {remedy['potency']}" | |
}}[lang_code] | |
{{ | |
'en': "Usage: {remedy['usage']}", | |
'ur': "استعمال: {remedy['usage']}" | |
}}[lang_code] | |
""") | |
else: | |
st.error({ | |
'en': "No matching remedy found. Consult a professional.", | |
'ur': "کوئی مماثل دوا نہیں ملی۔ پیشہ ور سے مشورہ کریں۔" | |
}[lang_code]) | |
# Disclaimer | |
st.markdown(""" | |
--- | |
**⚠️ Notice/نوٹس:** | |
This is not medical advice. Always consult a qualified homeopath. | |
यह चिकित्सीय सलाह नहीं है। हमेशा योग्य होम्योपैथ से सलाह लें۔ | |
Esto no es un consejo médico. Siempre consulte a un homeópata calificado. | |
یہ طبی مشورہ نہیں ہے۔ ہمیشہ کوالیفائڈ ہومیوپیتھک ڈاکٹر سے مشورہ کریں۔ | |
""") |