Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,18 @@
|
|
1 |
import streamlit as st
|
2 |
-
from groq import Groq
|
3 |
-
from huggingface_hub import InferenceClient
|
4 |
-
from langdetect import detect
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Initialize
|
8 |
-
|
|
|
|
|
9 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
10 |
|
11 |
# Language code mapping for NLLB-200
|
@@ -23,26 +30,20 @@ def translate_text(text, target_lang='eng_Latn'):
|
|
23 |
try:
|
24 |
source_lang = detect(text)
|
25 |
source_code = LANG_CODE_MAP.get(source_lang, 'eng_Latn')
|
26 |
-
|
27 |
-
|
28 |
-
text=text,
|
29 |
-
model="facebook/nllb-200-distilled-600M",
|
30 |
-
src_lang=source_code,
|
31 |
-
tgt_lang=target_lang
|
32 |
-
)
|
33 |
-
return result
|
34 |
except Exception as e:
|
35 |
st.error(f"Translation error: {str(e)}")
|
36 |
return text
|
37 |
|
38 |
def get_homeopathic_advice(symptoms):
|
39 |
-
"""Get medical advice using Groq"""
|
40 |
try:
|
41 |
response = groq_client.chat.completions.create(
|
42 |
model="llama3-70b-8192",
|
43 |
messages=[{
|
44 |
"role": "user",
|
45 |
-
"content": f"Act as homeopathic expert. Suggest remedies for: {symptoms}"
|
46 |
}],
|
47 |
temperature=0.3
|
48 |
)
|
@@ -63,26 +64,25 @@ for message in st.session_state.messages:
|
|
63 |
st.markdown(message["content"])
|
64 |
|
65 |
if prompt := st.chat_input("Describe symptoms in any language"):
|
66 |
-
# User input
|
67 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
68 |
-
|
69 |
# Process input
|
70 |
with st.spinner("Analyzing..."):
|
71 |
-
# Translate to English
|
72 |
-
english_input = translate_text(prompt
|
73 |
-
|
74 |
# Get medical advice
|
75 |
english_advice = get_homeopathic_advice(english_input)
|
76 |
-
|
77 |
# Translate back to original language
|
78 |
source_lang = detect(prompt)
|
79 |
-
translated_advice = translate_text(english_advice
|
80 |
-
|
81 |
# Format response
|
82 |
final_response = f"""
|
83 |
**English Recommendation:**
|
84 |
{english_advice}
|
85 |
-
|
86 |
**Translated Recommendation ({source_lang.upper()}):**
|
87 |
{translated_advice}
|
88 |
"""
|
@@ -93,4 +93,4 @@ if prompt := st.chat_input("Describe symptoms in any language"):
|
|
93 |
st.session_state.messages.append({"role": "assistant", "content": final_response})
|
94 |
|
95 |
# Disclaimer
|
96 |
-
st.caption("⚠️ This is not medical advice. Consult a professional.")
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
import os
|
3 |
+
from transformers import pipeline
|
4 |
+
from langdetect import detect
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
# Load Hugging Face token from environment
|
8 |
+
HF_TOKEN = os.environ.get("homeo_doc")
|
9 |
+
if not HF_TOKEN:
|
10 |
+
st.error("Missing Hugging Face API token. Set 'homeo_doc' in environment variables.")
|
11 |
|
12 |
+
# Initialize translation pipeline
|
13 |
+
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", token=HF_TOKEN)
|
14 |
+
|
15 |
+
# Initialize Groq client for homeopathic advice
|
16 |
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
17 |
|
18 |
# Language code mapping for NLLB-200
|
|
|
30 |
try:
|
31 |
source_lang = detect(text)
|
32 |
source_code = LANG_CODE_MAP.get(source_lang, 'eng_Latn')
|
33 |
+
translation = translator(text)[0]['translation_text']
|
34 |
+
return translation
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
except Exception as e:
|
36 |
st.error(f"Translation error: {str(e)}")
|
37 |
return text
|
38 |
|
39 |
def get_homeopathic_advice(symptoms):
|
40 |
+
"""Get medical advice using Groq API"""
|
41 |
try:
|
42 |
response = groq_client.chat.completions.create(
|
43 |
model="llama3-70b-8192",
|
44 |
messages=[{
|
45 |
"role": "user",
|
46 |
+
"content": f"Act as a homeopathic expert. Suggest remedies for: {symptoms}"
|
47 |
}],
|
48 |
temperature=0.3
|
49 |
)
|
|
|
64 |
st.markdown(message["content"])
|
65 |
|
66 |
if prompt := st.chat_input("Describe symptoms in any language"):
|
|
|
67 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
68 |
+
|
69 |
# Process input
|
70 |
with st.spinner("Analyzing..."):
|
71 |
+
# Translate input to English
|
72 |
+
english_input = translate_text(prompt)
|
73 |
+
|
74 |
# Get medical advice
|
75 |
english_advice = get_homeopathic_advice(english_input)
|
76 |
+
|
77 |
# Translate back to original language
|
78 |
source_lang = detect(prompt)
|
79 |
+
translated_advice = translate_text(english_advice)
|
80 |
+
|
81 |
# Format response
|
82 |
final_response = f"""
|
83 |
**English Recommendation:**
|
84 |
{english_advice}
|
85 |
+
|
86 |
**Translated Recommendation ({source_lang.upper()}):**
|
87 |
{translated_advice}
|
88 |
"""
|
|
|
93 |
st.session_state.messages.append({"role": "assistant", "content": final_response})
|
94 |
|
95 |
# Disclaimer
|
96 |
+
st.caption("⚠️ This is not medical advice. Consult a professional.")
|