Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,65 +8,120 @@ load_dotenv()
|
|
8 |
|
9 |
# Configure Streamlit page settings
|
10 |
st.set_page_config(
|
11 |
-
page_title="
|
12 |
-
page_icon="
|
13 |
-
layout="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
)
|
15 |
|
16 |
# Retrieve the Google API key from the environment
|
17 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
18 |
|
19 |
-
# If API key is not in the environment variable, hard-code it (though not recommended for production)
|
20 |
if not GOOGLE_API_KEY:
|
21 |
-
GOOGLE_API_KEY
|
22 |
-
|
23 |
-
# Check if the API key is loaded
|
24 |
-
if not GOOGLE_API_KEY:
|
25 |
-
st.error("API key not found! Please set the GOOGLE_API_KEY in your .env file.")
|
26 |
st.stop()
|
27 |
|
28 |
# Configure the Generative AI model
|
29 |
try:
|
30 |
gen_ai.configure(api_key=GOOGLE_API_KEY)
|
31 |
-
model = gen_ai.GenerativeModel("gemini-pro")
|
32 |
except Exception as e:
|
33 |
-
st.error(f"Error initializing the Gemini-Pro model: {e}")
|
34 |
st.stop()
|
35 |
|
36 |
-
#
|
37 |
-
def translate_role_for_streamlit(user_role):
|
38 |
-
return "assistant" if user_role == "model" else user_role
|
39 |
-
|
40 |
-
# Initialize the chat session if not already present in session state
|
41 |
if "chat_session" not in st.session_state:
|
42 |
try:
|
43 |
st.session_state.chat_session = model.start_chat(history=[])
|
44 |
except Exception as e:
|
45 |
-
st.error(f"Error initializing chat session: {e}")
|
46 |
st.stop()
|
47 |
|
48 |
-
# Display
|
49 |
-
st.title("
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# Add the user's message to the chat and display it
|
63 |
-
st.chat_message("user").markdown(user_prompt)
|
64 |
|
65 |
-
# Send
|
66 |
try:
|
67 |
-
gemini_response = st.session_state.chat_session.send_message(
|
68 |
-
|
|
|
|
|
|
|
69 |
with st.chat_message("assistant"):
|
70 |
st.markdown(gemini_response.text)
|
71 |
except Exception as e:
|
72 |
-
st.error(f"Error processing your message: {e}")
|
|
|
8 |
|
9 |
# Configure Streamlit page settings
|
10 |
st.set_page_config(
|
11 |
+
page_title="Health Assistant Chatbot",
|
12 |
+
page_icon="π©Ί",
|
13 |
+
layout="wide",
|
14 |
+
)
|
15 |
+
|
16 |
+
# Custom CSS for styling
|
17 |
+
st.markdown(
|
18 |
+
"""
|
19 |
+
<style>
|
20 |
+
body, .stApp {
|
21 |
+
background-color: #E3F2FD;
|
22 |
+
}
|
23 |
+
body {
|
24 |
+
background-color: #ADD8E6;
|
25 |
+
}
|
26 |
+
.stTabs [data-baseweb="tab-list"] {
|
27 |
+
justify-content: center;
|
28 |
+
}
|
29 |
+
.stTabs [data-baseweb="tab"] {
|
30 |
+
background-color: #E3F2FD;
|
31 |
+
border-radius: 20px;
|
32 |
+
font-weight: bold;
|
33 |
+
color: black;
|
34 |
+
padding: 10px;
|
35 |
+
}
|
36 |
+
.stTabs [data-baseweb="tab"][aria-selected="true"] {
|
37 |
+
background-color: #1E90FF;
|
38 |
+
color: white;
|
39 |
+
border-radius: 20px;
|
40 |
+
}
|
41 |
+
h2 {
|
42 |
+
color: #003366;
|
43 |
+
}
|
44 |
+
</style>
|
45 |
+
""",
|
46 |
+
unsafe_allow_html=True,
|
47 |
)
|
48 |
|
49 |
# Retrieve the Google API key from the environment
|
50 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
51 |
|
|
|
52 |
if not GOOGLE_API_KEY:
|
53 |
+
st.error("π¨ API key not found! Please set the GOOGLE_API_KEY in your .env file.")
|
|
|
|
|
|
|
|
|
54 |
st.stop()
|
55 |
|
56 |
# Configure the Generative AI model
|
57 |
try:
|
58 |
gen_ai.configure(api_key=GOOGLE_API_KEY)
|
59 |
+
model = gen_ai.GenerativeModel("gemini-1.5-pro") # Using a model for health-related queries
|
60 |
except Exception as e:
|
61 |
+
st.error(f"β Error initializing the Gemini-Pro model: {e}")
|
62 |
st.stop()
|
63 |
|
64 |
+
# Initialize the chat session
|
|
|
|
|
|
|
|
|
65 |
if "chat_session" not in st.session_state:
|
66 |
try:
|
67 |
st.session_state.chat_session = model.start_chat(history=[])
|
68 |
except Exception as e:
|
69 |
+
st.error(f"β Error initializing chat session: {e}")
|
70 |
st.stop()
|
71 |
|
72 |
+
# Display chatbot title
|
73 |
+
st.title("π©Ί Health Assistant Chatbot")
|
74 |
|
75 |
+
# Symptom categories for the user to select
|
76 |
+
symptom_tabs = ["π€§ Sneezing", "π· Headache", "π€’ Stomach Ache", "π¦ Fever", "πͺ Fatigue"]
|
77 |
+
selected_symptom_tab = st.tabs(symptom_tabs)
|
78 |
+
|
79 |
+
# Sample symptoms and associated treatment information
|
80 |
+
symptom_treatment = {
|
81 |
+
"π€§ Sneezing": [
|
82 |
+
"Possible cause: Allergies or common cold.",
|
83 |
+
"Treatment recommendation: Try antihistamines for allergies or rest and fluids for a cold."
|
84 |
+
],
|
85 |
+
"π· Headache": [
|
86 |
+
"Possible cause: Tension, dehydration, or migraines.",
|
87 |
+
"Treatment recommendation: Drink water, take over-the-counter pain relievers, or rest in a dark room."
|
88 |
+
],
|
89 |
+
"π€’ Stomach Ache": [
|
90 |
+
"Possible cause: Indigestion, food poisoning, or gas.",
|
91 |
+
"Treatment recommendation: Drink ginger tea, rest, and avoid heavy meals."
|
92 |
+
],
|
93 |
+
"π¦ Fever": [
|
94 |
+
"Possible cause: Infection or viral illness.",
|
95 |
+
"Treatment recommendation: Take fever reducers like acetaminophen, stay hydrated, and rest."
|
96 |
+
],
|
97 |
+
"πͺ Fatigue": [
|
98 |
+
"Possible cause: Stress, sleep deprivation, or illness.",
|
99 |
+
"Treatment recommendation: Ensure proper sleep, hydrate, and reduce stress."
|
100 |
+
]
|
101 |
+
}
|
102 |
+
|
103 |
+
# Display symptom recommendations under the selected tab
|
104 |
+
for i, tab in enumerate(selected_symptom_tab):
|
105 |
+
with tab:
|
106 |
+
st.subheader(f"π {symptom_tabs[i]} Symptoms")
|
107 |
+
for recommendation in symptom_treatment[symptom_tabs[i]]:
|
108 |
+
st.write(f"- {recommendation}")
|
109 |
+
|
110 |
+
# Input field for user's symptoms
|
111 |
+
user_symptom = st.chat_input("π¬ Enter your symptom...")
|
112 |
|
113 |
+
if user_symptom:
|
114 |
+
# Display the user's symptom
|
115 |
+
st.chat_message("user").markdown(user_symptom)
|
|
|
|
|
116 |
|
117 |
+
# Send symptom message to Gemini-Pro for response
|
118 |
try:
|
119 |
+
gemini_response = st.session_state.chat_session.send_message(
|
120 |
+
f"Provide possible causes and treatments for the following symptom: {user_symptom}"
|
121 |
+
)
|
122 |
+
|
123 |
+
# Display AI response
|
124 |
with st.chat_message("assistant"):
|
125 |
st.markdown(gemini_response.text)
|
126 |
except Exception as e:
|
127 |
+
st.error(f"β Error processing your message: {e}")
|