forked from santiago20233/blank-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
176 lines (141 loc) · 7.24 KB
/
streamlit_app.py
File metadata and controls
176 lines (141 loc) · 7.24 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json
import streamlit as st
import firebase_admin
from firebase_admin import credentials, firestore, auth
from openai import OpenAI
import time
# Load Firebase credentials
firebase_config = st.secrets["firebase"]
cred = credentials.Certificate(dict(firebase_config))
# Initialize Firebase if not already initialized
if not firebase_admin._apps:
firebase_admin.initialize_app(cred)
# Firestore Database
db = firestore.client()
# OpenAI API Key
openai_api_key = st.secrets["OPENAI_API_KEY"]
client = OpenAI(api_key=openai_api_key)
# ---------------- UI CUSTOMIZATION ---------------- #
st.markdown("""
<style>
.title-container { text-align: center; margin-bottom: 10px; }
.title { font-size: 50px; font-weight: bold; color: #ff69b4; text-transform: uppercase; } /* BIG & PINK */
.subtitle { font-size: 18px; font-weight: normal; color: #666; }
.chat-container { display: flex; flex-direction: column; align-items: center; max-width: 600px; margin: auto; }
.chat-bubble { padding: 12px; border-radius: 16px; margin: 8px 0; font-size: 16px; width: fit-content; max-width: 80%; }
.user-message { background-color: #4a90e2; color: white; align-self: flex-end; }
.ai-message { background-color: #f1f1f1; border: 1px solid #ddd; color: black; align-self: flex-start; }
.typing-indicator { font-size: 14px; color: #888; font-style: italic; margin-top: 5px; }
.signin-button { position: absolute; top: 15px; right: 15px; background-color: #ff69b4; color: white; padding: 8px 15px; border-radius: 8px; cursor: pointer; }
</style>
""", unsafe_allow_html=True)
# ---------------- SIGN-IN SYSTEM ---------------- #
if "user_logged_in" not in st.session_state:
st.session_state.user_logged_in = False
st.session_state.user_id = None
# Always show sign-in button at the top
if not st.session_state.user_logged_in:
if st.button("Sign in"):
st.session_state.show_login = True
st.rerun()
# ---------------- LOGIN FORM ---------------- #
if "show_login" in st.session_state and st.session_state.show_login:
with st.sidebar:
st.title("Sign Up / Login")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
login_btn = st.button("Login")
signup_btn = st.button("Sign Up")
if login_btn:
try:
user = auth.get_user_by_email(email)
st.session_state.user_id = user.uid
st.session_state.user_logged_in = True
st.success("Logged in successfully!")
st.rerun()
except:
st.error("Invalid credentials.")
if signup_btn:
try:
user = auth.create_user(email=email, password=password)
st.session_state.user_id = user.uid
db.collection("users").document(user.uid).set({
"email": email,
"sign_up_date": firestore.SERVER_TIMESTAMP
})
st.success("Account created! Please log in.")
st.rerun()
except:
st.error("Sign-up failed. Try again.")
# ---------------- CHAT SECTION ---------------- #
# Display title & subtitle
st.markdown("<div class='title-container'><p class='title'>fifi</p><p class='subtitle'>Call me mommy! 🤰</p></div>", unsafe_allow_html=True)
# Retrieve chat history for logged-in users
user_id = st.session_state.user_id if st.session_state.user_logged_in else None
chat_ref = db.collection("chats").document(user_id) if user_id else None
# ---------------- LOAD CHAT HISTORY ---------------- #
if "chat_history" not in st.session_state:
if user_id and chat_ref.get().exists:
st.session_state.chat_history = chat_ref.get().to_dict()["history"]
else:
st.session_state.chat_history = [{"role": "system", "content": "You are Fifi, a pregnancy and baby care assistant who always responds in a warm, supportive, and comforting tone. Your goal is to make users feel heard, validated, and cared for in their motherhood journey."}]
# ---------------- SUGGESTED QUESTIONS (DROPDOWN) ---------------- #
suggested_questions = {
"👶 Baby Care": [
"When does the belly button fall off?",
"When should my baby start doing tummy time?",
"How do I establish a sleep routine for my newborn?",
"When is it recommended to introduce solid foods?"
],
"🤱 Postpartum Recovery": [
"How can I care for my C-section wound?",
"What should I expect during postpartum recovery?"
],
"🤰 Pregnancy": [
"How to avoid stretch marks during my pregnancy?",
"What are the essential vitamins and nutrients I should take?"
]
}
with st.expander("💡 Suggested Questions"):
for category, questions in suggested_questions.items():
st.markdown(f"**{category}**")
for question in questions:
st.markdown(f"- {question}")
# ---------------- DISPLAY FULL CHAT HISTORY ---------------- #
for message in st.session_state.chat_history[1:]:
role_class = "user-message" if message["role"] == "user" else "ai-message"
st.markdown(f"<div class='chat-container'><div class='chat-bubble {role_class}'>{message['content']}</div></div>", unsafe_allow_html=True)
# ---------------- CHAT INPUT ---------------- #
user_input = st.chat_input("Type your question here...")
if user_input:
st.session_state.chat_history.append({"role": "user", "content": user_input})
# Display user message immediately
st.markdown(f"<div class='chat-container'><div class='chat-bubble user-message'>{user_input}</div></div>", unsafe_allow_html=True)
# Show persistent typing indicator
typing_placeholder = st.empty()
with typing_placeholder:
st.markdown("<div class='typing-indicator'>typing...</div>", unsafe_allow_html=True)
# Get response
response = client.chat.completions.create(
model="gpt-4",
messages=st.session_state.chat_history,
temperature=0.4,
max_tokens=600
)
assistant_reply = f"{response.choices[0].message.content}"
# Add medical disclaimer if necessary
if any(word in user_input.lower() for word in ["fever", "sick", "infection", "pain", "rash", "vomiting", "diarrhea"]):
assistant_reply += "\n\n⚠️ **Disclaimer:** I am not a doctor. If this issue is serious or persists, please seek medical attention."
# Add related links with descriptions
assistant_reply += "\n\n**📚 Related articles for further reading:**"
assistant_reply += "\n- **[Baby Belly Button Care](https://example.com/belly-button-care)** – Learn how to properly care for your newborn’s belly button."
assistant_reply += "\n- **[C-Section Recovery Guide](https://example.com/c-section-recovery)** – Tips for healing and taking care of yourself after a C-section."
# Remove typing indicator
typing_placeholder.empty()
# Add response to chat history
st.session_state.chat_history.append({"role": "assistant", "content": assistant_reply})
# Save chat history only if user is logged in
if user_id:
chat_ref.set({"history": st.session_state.chat_history})
# Display Fifi's response
st.markdown(f"<div class='chat-container'><div class='chat-bubble ai-message'>{assistant_reply}</div></div>", unsafe_allow_html=True)