-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
177 lines (118 loc) · 3.53 KB
/
Copy pathapp.py
File metadata and controls
177 lines (118 loc) · 3.53 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
177
import streamlit as st
import time
from datetime import datetime
from chatbot import FAQChatbot
# ------------------ Page Settings ------------------
st.set_page_config(
page_title="StudyBuddy FAQ Chatbot",
page_icon="🤖",
layout="centered"
)
# ------------------ Load Chatbot ------------------
bot = FAQChatbot("faq.json")
# ------------------ Session State ------------------
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "assistant",
"content": "👋 Hello! I'm your StudyBuddy Assistant.\n\nAsk me anything related to StudyBuddy!"
}
]
# ------------------ Title ------------------
st.title("🤖 StudyBuddy FAQ Chatbot")
st.write(
"This chatbot answers frequently asked questions related to StudyBuddy."
)
st.info(
"""
### 💡 Try asking
• What is StudyBuddy?
• How do I create a study plan?
• Can I chat with the AI?
• Does StudyBuddy work offline?
• Who made you?
"""
)
# ------------------ Sidebar ------------------
with st.sidebar:
st.header("📚 StudyBuddy")
st.success("🟢 Online")
st.write("### Frequently Asked Questions")
with st.expander("Click to view FAQs"):
for question in bot.get_questions():
st.markdown(f"• {question}")
st.divider()
st.subheader("📊 Chat Statistics")
st.metric(
"Messages",
len(st.session_state.messages)
)
st.metric(
"Available FAQs",
len(bot.get_questions())
)
st.divider()
if st.button("🗑 Clear Chat"):
st.session_state.messages = [
{
"role": "assistant",
"content": "👋 Hello! I'm your StudyBuddy Assistant.\n\nAsk me anything related to StudyBuddy!"
}
]
st.rerun()
# ------------------ Display Chat ------------------
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if "time" in msg:
st.caption(msg["time"])
# ------------------ User Input ------------------
question = st.chat_input("Type your question here...")
if question:
st.session_state.messages.append(
{
"role": "user",
"content": question,
"time": datetime.now().strftime("%I:%M %p")
}
)
with st.chat_message("user"):
st.markdown(question)
st.caption(datetime.now().strftime("%I:%M %p"))
with st.chat_message("assistant"):
loading = st.empty()
loading.markdown("🤖 *Typing...*")
time.sleep(1)
try:
reply = bot.get_response(question)
except Exception:
reply = "Sorry! Something went wrong. Please try again."
loading.markdown(reply)
current_time = datetime.now().strftime("%I:%M %p")
st.caption(current_time)
st.session_state.messages.append(
{
"role": "assistant",
"content": reply,
"time": current_time
}
)
# ------------------ Footer ------------------
st.markdown(
"""
<style>
.footer{
position:fixed;
bottom:12px;
right:18px;
color:gray;
font-size:13px;
opacity:0.8;
}
</style>
<div class="footer">
❤️ Made by Harsh Garg
</div>
""",
unsafe_allow_html=True
)