Spaces:
Running
Running
# app.py | |
import streamlit as st | |
from groq import Groq | |
import os | |
from langdetect import detect | |
# Initialize Groq client | |
try: | |
client = Groq(api_key=os.environ.get("GROQ_API_KEY")) | |
except Exception as e: | |
st.error(f"Groq initialization failed: {str(e)}") | |
# Main function to generate bilingual advice | |
def get_bilingual_advice(user_input): | |
try: | |
# Detect input language | |
input_lang = detect(user_input) | |
# Step 1: Generate English medical report | |
medical_prompt = f"""As a homeopathic expert, analyze these symptoms: {user_input} | |
Provide detailed recommendations in this format: | |
**English Recommendation:** | |
1. Medicine (Potency): Explanation | |
- Dosage: ... | |
- Key Symptoms: ... | |
- Source: ... | |
**Translated Recommendation ({input_lang}):** | |
1. Same medicine name in English with translated explanation | |
- Translated dosage instructions | |
- Translated symptoms | |
- Same source links | |
Keep medicine names in English for both sections.""" | |
# Use larger 400B model for better multilingual support | |
response = client.chat.completions.create( | |
messages=[{"role": "user", "content": medical_prompt}], | |
model="deepseek-r1-distill-llama-70b", # Bigger model for better translations | |
temperature=0.3, | |
max_tokens=1000 | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Streamlit UI Setup | |
st.set_page_config(page_title="Homeo Advisor", page_icon="🌿") | |
st.title("🌿 Multilingual Homeopathic Advisor") | |
# Initialize chat history | |
if "messages" not in st.session_state: | |
st.session_state.messages = [{"role": "assistant", "content": "Describe your symptoms in any language"}] | |
# Display chat messages | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# User input handling | |
if prompt := st.chat_input("Type your symptoms..."): | |
st.session_state.messages.append({"role": "user", "content": prompt}) | |
with st.chat_message("assistant"): | |
with st.spinner("Analyzing symptoms..."): | |
response = get_bilingual_advice(prompt) | |
st.markdown(response) | |
st.session_state.messages.append({"role": "assistant", "content": response}) | |
# Disclaimer | |
st.caption("⚠️ This is not medical advice. Consult a professional.") |