Spaces:
Running
Running
File size: 4,026 Bytes
8f5f39b b16ad37 8f5f39b 59c6938 8f5f39b 4898a36 8f5f39b 8c16c2f 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 8f5f39b 4898a36 |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import os
import streamlit as st
from dotenv import load_dotenv
import google.generativeai as gen_ai
# Load environment variables
load_dotenv()
# Configure Streamlit page settings
st.set_page_config(
page_title="Health Assistant Chatbot",
page_icon="π©Ί",
layout="wide",
)
# Custom CSS for styling
st.markdown(
"""
<style>
body, .stApp {
background-color: #E3F2FD;
}
body {
background-color: #ADD8E6;
}
.stTabs [data-baseweb="tab-list"] {
justify-content: center;
}
.stTabs [data-baseweb="tab"] {
background-color: #E3F2FD;
border-radius: 20px;
font-weight: bold;
color: black;
padding: 10px;
}
.stTabs [data-baseweb="tab"][aria-selected="true"] {
background-color: #1E90FF;
color: white;
border-radius: 20px;
}
h2 {
color: #003366;
}
</style>
""",
unsafe_allow_html=True,
)
# Retrieve the Google API key from the environment
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GOOGLE_API_KEY:
st.error("π¨ API key not found! Please set the GOOGLE_API_KEY in your .env file.")
st.stop()
# Configure the Generative AI model
try:
gen_ai.configure(api_key=GOOGLE_API_KEY)
model = gen_ai.GenerativeModel("gemini-1.5-pro") # Using a model for health-related queries
except Exception as e:
st.error(f"β Error initializing the Gemini-Pro model: {e}")
st.stop()
# Initialize the chat session
if "chat_session" not in st.session_state:
try:
st.session_state.chat_session = model.start_chat(history=[])
except Exception as e:
st.error(f"β Error initializing chat session: {e}")
st.stop()
# Display chatbot title
st.title("π©Ί Health Assistant Chatbot")
# Symptom categories for the user to select
symptom_tabs = ["π€§ Sneezing", "π· Headache", "π€’ Stomach Ache", "π¦ Fever", "πͺ Fatigue"]
selected_symptom_tab = st.tabs(symptom_tabs)
# Sample symptoms and associated treatment information
symptom_treatment = {
"π€§ Sneezing": [
"Possible cause: Allergies or common cold.",
"Treatment recommendation: Try antihistamines for allergies or rest and fluids for a cold."
],
"π· Headache": [
"Possible cause: Tension, dehydration, or migraines.",
"Treatment recommendation: Drink water, take over-the-counter pain relievers, or rest in a dark room."
],
"π€’ Stomach Ache": [
"Possible cause: Indigestion, food poisoning, or gas.",
"Treatment recommendation: Drink ginger tea, rest, and avoid heavy meals."
],
"π¦ Fever": [
"Possible cause: Infection or viral illness.",
"Treatment recommendation: Take fever reducers like acetaminophen, stay hydrated, and rest."
],
"πͺ Fatigue": [
"Possible cause: Stress, sleep deprivation, or illness.",
"Treatment recommendation: Ensure proper sleep, hydrate, and reduce stress."
]
}
# Display symptom recommendations under the selected tab
for i, tab in enumerate(selected_symptom_tab):
with tab:
st.subheader(f"π {symptom_tabs[i]} Symptoms")
for recommendation in symptom_treatment[symptom_tabs[i]]:
st.write(f"- {recommendation}")
# Input field for user's symptoms
user_symptom = st.chat_input("π¬ Enter your symptom...")
if user_symptom:
# Display the user's symptom
st.chat_message("user").markdown(user_symptom)
# Send symptom message to Gemini-Pro for response
try:
gemini_response = st.session_state.chat_session.send_message(
f"Provide possible causes and treatments for the following symptom: {user_symptom}"
)
# Display AI response
with st.chat_message("assistant"):
st.markdown(gemini_response.text)
except Exception as e:
st.error(f"β Error processing your message: {e}")
|