Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,7 @@
|
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
4 |
import os
|
5 |
-
import requests
|
6 |
-
from bs4 import BeautifulSoup
|
7 |
from langdetect import detect
|
8 |
-
from urllib.parse import quote_plus
|
9 |
|
10 |
# Initialize Groq client
|
11 |
try:
|
@@ -13,66 +10,46 @@ try:
|
|
13 |
except Exception as e:
|
14 |
st.error(f"Groq initialization failed: {str(e)}")
|
15 |
|
16 |
-
#
|
17 |
-
def
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
url = f"https://www.google.com/search?q={encoded_query}&gl=us&hl=en"
|
22 |
-
response = requests.get(url, headers=headers)
|
23 |
-
response.raise_for_status()
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
'title': result.find('h3').text,
|
29 |
-
'link': result.find('a')['href'],
|
30 |
-
'snippet': result.find('div', class_='VwiC3b').text
|
31 |
-
}
|
32 |
-
for result in soup.find_all('div', class_='tF2Cxc')[:3]
|
33 |
-
]
|
34 |
-
except Exception as e:
|
35 |
-
st.error(f"Google search failed: {str(e)}")
|
36 |
-
return []
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
-
messages=[{"role": "user", "content": symptom_prompt}],
|
51 |
-
model="llama3-70b-8192"
|
52 |
-
).choices[0].message.content.split(", ")
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
58 |
)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
Use only this data: {[r['snippet'] for r in search_results]}
|
63 |
-
Include medicine name (English), potency, dosage and source."""
|
64 |
-
|
65 |
-
return client.chat.completions.create(
|
66 |
-
messages=[{"role": "user", "content": response_prompt}],
|
67 |
-
model="llama3-70b-8192"
|
68 |
-
).choices[0].message.content
|
69 |
-
|
70 |
except Exception as e:
|
71 |
return f"Error: {str(e)}"
|
72 |
|
73 |
# Streamlit UI Setup
|
74 |
st.set_page_config(page_title="Homeo Advisor", page_icon="🌿")
|
75 |
-
st.title("🌿 Homeopathic
|
76 |
|
77 |
# Initialize chat history
|
78 |
if "messages" not in st.session_state:
|
@@ -84,12 +61,12 @@ for message in st.session_state.messages:
|
|
84 |
st.markdown(message["content"])
|
85 |
|
86 |
# User input handling
|
87 |
-
if prompt := st.chat_input("
|
88 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
89 |
|
90 |
with st.chat_message("assistant"):
|
91 |
-
with st.spinner("Analyzing..."):
|
92 |
-
response =
|
93 |
st.markdown(response)
|
94 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
95 |
|
|
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
4 |
import os
|
|
|
|
|
5 |
from langdetect import detect
|
|
|
6 |
|
7 |
# Initialize Groq client
|
8 |
try:
|
|
|
10 |
except Exception as e:
|
11 |
st.error(f"Groq initialization failed: {str(e)}")
|
12 |
|
13 |
+
# Main function to generate bilingual advice
|
14 |
+
def get_bilingual_advice(user_input):
|
15 |
try:
|
16 |
+
# Detect input language
|
17 |
+
input_lang = detect(user_input)
|
|
|
|
|
|
|
18 |
|
19 |
+
# Step 1: Generate English medical report
|
20 |
+
medical_prompt = f"""As a homeopathic expert, analyze these symptoms: {user_input}
|
21 |
+
Provide detailed recommendations in this format:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
**English Recommendation:**
|
24 |
+
1. Medicine (Potency): Explanation
|
25 |
+
- Dosage: ...
|
26 |
+
- Key Symptoms: ...
|
27 |
+
- Source: ...
|
28 |
+
|
29 |
+
**Translated Recommendation ({input_lang}):**
|
30 |
+
1. Same medicine name in English with translated explanation
|
31 |
+
- Translated dosage instructions
|
32 |
+
- Translated symptoms
|
33 |
+
- Same source links
|
34 |
|
35 |
+
Keep medicine names in English for both sections."""
|
|
|
|
|
|
|
36 |
|
37 |
+
# Use larger 400B model for better multilingual support
|
38 |
+
response = client.chat.completions.create(
|
39 |
+
messages=[{"role": "user", "content": medical_prompt}],
|
40 |
+
model="llama3-400b-8192", # Bigger model for better translations
|
41 |
+
temperature=0.3,
|
42 |
+
max_tokens=1000
|
43 |
)
|
44 |
|
45 |
+
return response.choices[0].message.content
|
46 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
48 |
return f"Error: {str(e)}"
|
49 |
|
50 |
# Streamlit UI Setup
|
51 |
st.set_page_config(page_title="Homeo Advisor", page_icon="🌿")
|
52 |
+
st.title("🌿 Multilingual Homeopathic Advisor")
|
53 |
|
54 |
# Initialize chat history
|
55 |
if "messages" not in st.session_state:
|
|
|
61 |
st.markdown(message["content"])
|
62 |
|
63 |
# User input handling
|
64 |
+
if prompt := st.chat_input("Type your symptoms..."):
|
65 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
66 |
|
67 |
with st.chat_message("assistant"):
|
68 |
+
with st.spinner("Analyzing symptoms..."):
|
69 |
+
response = get_bilingual_advice(prompt)
|
70 |
st.markdown(response)
|
71 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
72 |
|