Spaces:
Sleeping
Sleeping
File size: 3,749 Bytes
f0144dd 5b3c8c9 f0144dd 256d4fe 5b3c8c9 f0144dd 5b3c8c9 3fdc359 256d4fe 3fdc359 256d4fe 3fdc359 256d4fe 3fdc359 256d4fe 3fdc359 f0144dd 256d4fe 5b3c8c9 256d4fe 5b3c8c9 256d4fe 5b3c8c9 256d4fe 5b3c8c9 256d4fe 5b3c8c9 256d4fe f0144dd 256d4fe 5b3c8c9 256d4fe f0144dd 5b3c8c9 256d4fe f0144dd 256d4fe f0144dd 256d4fe 5b3c8c9 256d4fe f0144dd 5b3c8c9 |
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 |
# app.py
import streamlit as st
from groq import Groq
import os
import requests
from bs4 import BeautifulSoup
import re
from urllib.parse import quote_plus
from langdetect import detect
# Groq API setup
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Web scraping functions
def google_search(query):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
encoded_query = quote_plus(query)
url = f"https://www.google.com/search?q={encoded_query}&gl=us&hl=en"
try:
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
results = []
for g in soup.find_all('div', class_='tF2Cxc'):
link = g.find('a')['href']
title = g.find('h3').text
snippet = g.find('div', class_='VwiC3b')
if snippet:
results.append({
'title': title,
'link': link,
'snippet': snippet.text
})
return results[:3]
except Exception as e:
st.error(f"Search Error: {str(e)}")
return []
# Chatbot processing
def multilingual_chatbot(user_input):
try:
# Detect input language
lang = detect(user_input)
# Step 1: Symptom extraction (English for searching)
symptom_prompt = f"""Extract medical symptoms from this text in English comma-separated format:
{user_input}
Example Output: headache, dry cough, fever"""
symptom_response = client.chat.completions.create(
messages=[{"role": "user", "content": symptom_prompt}],
model="llama3-70b-8192",
temperature=0.2
)
symptoms = symptom_response.choices[0].message.content.split(", ")
# Step 2: Web search
search_query = f"homeopathic remedies for {' '.join(symptoms)} site:.edu OR site:.gov"
results = google_search(search_query)
# Step 3: Generate multilingual response
response_prompt = f"""Translate this medical advice to {lang} while keeping medicine names in English:
Suggested remedies for {', '.join(symptoms)}:
{[r['snippet'] for r in results]}
Include dosage instructions and administration method."""
final_response = client.chat.completions.create(
messages=[{"role": "user", "content": response_prompt}],
model="llama3-70b-8192",
temperature=0.3
)
return final_response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Streamlit UI
st.set_page_config(page_title="Homeo Assistant", page_icon="🌿")
st.title("🌐 Multilingual Homeopathy 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"])
# Chat input
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 = multilingual_chatbot(prompt)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
# Disclaimer
st.markdown("""
---
**⚠️ Disclaimer:**
This is not medical advice. Always consult a qualified practitioner.
""") |