aminahmed78 commited on
Commit
e06b22f
·
verified ·
1 Parent(s): 2d81770

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -54
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
- # Web scraping with error handling
17
- def google_search(query):
18
  try:
19
- headers = {"User-Agent": "Mozilla/5.0"}
20
- encoded_query = quote_plus(query)
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
- soup = BeautifulSoup(response.text, 'html.parser')
26
- return [
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
- # Main chatbot function
39
- def get_homeopathic_advice(user_input):
40
- try:
41
- # Language detection
42
- lang = detect(user_input)
43
-
44
- # Step 1: Symptom extraction
45
- symptom_prompt = f"""Extract homeopathic symptoms from this text as comma-separated list:
46
- {user_input}
47
- Example: headache, dry cough, anxiety"""
 
48
 
49
- symptoms = client.chat.completions.create(
50
- messages=[{"role": "user", "content": symptom_prompt}],
51
- model="llama3-70b-8192"
52
- ).choices[0].message.content.split(", ")
53
 
54
- # Step 2: Homeopathic-specific search
55
- search_results = google_search(
56
- f"homeopathic remedy for {' '.join(symptoms)} "
57
- f"site:.edu OR site:.gov -allopathic"
 
 
58
  )
59
 
60
- # Step 3: Generate response
61
- response_prompt = f"""You're a homeopathic expert. Suggest remedies in {lang} for: {', '.join(symptoms)}
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 Chat Advisor")
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("Enter symptoms..."):
88
  st.session_state.messages.append({"role": "user", "content": prompt})
89
 
90
  with st.chat_message("assistant"):
91
- with st.spinner("Analyzing..."):
92
- response = get_homeopathic_advice(prompt)
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