Spaces:
Sleeping
Sleeping
# 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. | |
""") |