Spaces:
Running
Running
File size: 4,544 Bytes
4241c9e 6841cb0 2bfa4a7 832a096 6841cb0 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 6841cb0 2bfa4a7 832a096 2bfa4a7 832a096 4241c9e 832a096 cda71b5 832a096 2bfa4a7 4241c9e 832a096 2bfa4a7 832a096 2bfa4a7 4241c9e 2bfa4a7 14e31fa 2bfa4a7 913e7f1 2bfa4a7 913e7f1 5028238 913e7f1 2bfa4a7 5028238 2bfa4a7 913e7f1 2bfa4a7 913e7f1 2bfa4a7 5028238 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 2bfa4a7 832a096 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 2bfa4a7 913e7f1 5028238 913e7f1 2bfa4a7 913e7f1 2bfa4a7 |
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 |
import streamlit as st
import os
from groq import Groq
from langdetect import detect, DetectorFactory
import re
# Ensure consistent language detection
DetectorFactory.seed = 0
# Load API key from environment
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
if not GROQ_API_KEY:
st.error("❌ Missing GROQ API Key. Set 'GROQ_API_KEY' in environment variables.")
# Initialize Groq client
groq_client = Groq(api_key=GROQ_API_KEY)
# Define chatbot role as a specialized homeopathic doctor
CHATBOT_ROLE = """You are a highly skilled homeopathic doctor specializing in natural remedies.
You analyze symptoms provided by users and suggest effective homeopathic treatments based on research-backed sources.
Your response must include:
- Best possible remedy
- Proper dosage and administration
- Scientific or historical sources for reference
Ensure accuracy in medical details and maintain professionalism."""
# Improve language detection, especially for Roman Urdu
ROMAN_URDU_PATTERN = re.compile(r'\b(mujhe|kamar|dard|hai|aap|kyun|kaisi|bohot|zyada|masla|kar|raha|rahi|hun|ho|hai)\b', re.IGNORECASE)
def detect_language(text):
"""Detect the language of the input text, with special handling for Roman Urdu."""
try:
if ROMAN_URDU_PATTERN.search(text):
return "roman_ur" # Detect Roman Urdu correctly
return detect(text)
except:
return "en" # Default to English if detection fails
def get_homeopathic_advice(symptoms):
"""Search the web for remedies and generate a professional homeopathic response."""
try:
response = groq_client.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": CHATBOT_ROLE},
{"role": "user", "content": f"Find the best homeopathic remedy for: {symptoms}. Provide remedy details, usage, dose, and sources."}
],
temperature=0.3
)
return response.choices[0].message.content
except Exception as e:
return f"❌ Error fetching homeopathic advice: {str(e)}"
def translate_text(text, target_lang):
"""Translate text using Groq's LLaMA model."""
try:
if target_lang == "roman_ur":
translation_prompt = f"Translate this medical advice into **Roman Urdu**, keeping it in **Latin script**: {text}"
else:
translation_prompt = f"Translate this medical advice into {target_lang}: {text}"
response = groq_client.chat.completions.create(
model="llama3-70b-8192",
messages=[
{"role": "system", "content": "You are a multilingual AI that accurately translates medical advice while preserving technical accuracy."},
{"role": "user", "content": translation_prompt}
],
temperature=0.2
)
return response.choices[0].message.content
except Exception as e:
return f"⚠️ Translation error: {str(e)}"
# 🎨 Streamlit UI
st.set_page_config(page_title="Homeo Doctor AI", page_icon="🌿")
st.title("🌍 AI-Powered Homeopathic Doctor")
# Chat interface
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User input box
if prompt := st.chat_input("Describe your symptoms..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.spinner("🔍 Analyzing symptoms..."):
# Detect user language
detected_lang = detect_language(prompt)
# Get homeopathic remedy
homeo_advice = get_homeopathic_advice(prompt)
# Translate advice if needed
if detected_lang != "en":
translated_advice = translate_text(homeo_advice, detected_lang)
else:
translated_advice = homeo_advice
# Format final response
final_response = f"""
**💡 Expert Homeopathic Advice (English):**
{homeo_advice}
**🌍 Translated Advice ({'Roman Urdu' if detected_lang == 'roman_ur' else detected_lang.upper()}):**
{translated_advice}
"""
# Display response
with st.chat_message("assistant"):
st.markdown(final_response)
# Save chat history
st.session_state.messages.append({"role": "assistant", "content": final_response})
# Disclaimer
st.caption("⚠️ This AI does not replace professional medical consultation.")
|