-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
192 lines (161 loc) · 5.7 KB
/
Copy pathapp.py
File metadata and controls
192 lines (161 loc) · 5.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import streamlit as st
import nltk
import string
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from faqs import faqs
nltk.download('punkt', quiet=True)
nltk.download('punkt_tab', quiet=True)
nltk.download('stopwords', quiet=True)
st.set_page_config(
page_title="ShredBot — Fitness FAQ Chatbot",
page_icon="💪",
layout="centered"
)
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Syne:wght@400;500;600;700&family=Inter:wght@300;400;500&display=swap');
html, body, [class*="css"] {
font-family: 'Inter', sans-serif;
}
.stApp {
background-color: #0a0a0a;
}
.main-title {
font-family: 'Syne', sans-serif;
font-size: 2.6rem;
font-weight: 700;
color: #f5f5f5;
text-align: center;
margin-bottom: 0.2rem;
letter-spacing: -1px;
}
.accent {
color: #a3e635;
}
.subtitle {
text-align: center;
color: #6b7280;
font-size: 0.95rem;
margin-bottom: 2rem;
font-weight: 300;
}
.suggestion-label {
font-size: 0.75rem;
color: #6b7280;
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 0.5rem;
text-align: center;
}
.stChatMessage {
background: transparent !important;
}
[data-testid="stDecoration"] {
display: none;
}
header[data-testid="stHeader"] {
background: none;
}
.confidence-bar {
height: 3px;
background: #1a1a1a;
border-radius: 2px;
margin-top: 6px;
}
.stButton > button {
background: #1a1a1a !important;
color: #a3e635 !important;
border: 1px solid #2a2a2a !important;
border-radius: 20px !important;
font-size: 0.78rem !important;
font-family: 'Inter', sans-serif !important;
padding: 0.3rem 1rem !important;
transition: all 0.2s ease !important;
}
.stButton > button:hover {
background: #a3e635 !important;
color: #0a0a0a !important;
border-color: #a3e635 !important;
}
[data-testid="stChatInputTextArea"] {
background: #111111 !important;
color: #f5f5f5 !important;
border: 1px solid #2a2a2a !important;
border-radius: 12px !important;
font-family: 'Inter', sans-serif !important;
}
</style>
""", unsafe_allow_html=True)
def preprocess(text):
text = text.lower()
text = re.sub(r'[^\w\s]', '', text)
tokens = word_tokenize(text)
stop_words = set(stopwords.words('english'))
tokens = [t for t in tokens if t not in stop_words]
return ' '.join(tokens)
def get_best_answer(user_question, threshold=0.11):
faq_questions = [f['question'] for f in faqs]
processed_faqs = [preprocess(q) for q in faq_questions]
processed_user = preprocess(user_question)
corpus = processed_faqs + [processed_user]
vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(corpus)
user_vector = tfidf_matrix[-1]
faq_vectors = tfidf_matrix[:-1]
similarities = cosine_similarity(user_vector, faq_vectors)[0]
best_idx = similarities.argmax()
best_score = similarities[best_idx]
if best_score < threshold:
return None, 0
return faqs[best_idx]['answer'], float(best_score)
st.markdown('<div class="main-title">Shred<span class="accent">Bot</span> 💪</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle">Your AI-powered fitness & nutrition assistant</div>', unsafe_allow_html=True)
SUGGESTIONS = [
"How much protein do I need?",
"Best time to work out?",
"Is creatine safe?",
"How do I get abs?",
"What should I eat after gym?",
]
if "messages" not in st.session_state:
st.session_state.messages = [
{
"role": "assistant",
"content": "Hey! I'm ShredBot 💪 Ask me anything about fitness, nutrition, workouts, or supplements."
}
]
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
st.markdown('<div class="suggestion-label">Try asking</div>', unsafe_allow_html=True)
cols = st.columns(len(SUGGESTIONS))
for i, suggestion in enumerate(SUGGESTIONS):
with cols[i]:
if st.button(suggestion, key=f"suggestion_{i}"):
st.session_state.messages.append({"role": "user", "content": suggestion})
answer, score = get_best_answer(suggestion)
if answer:
st.session_state.messages.append({"role": "assistant", "content": answer})
else:
st.session_state.messages.append({
"role": "assistant",
"content": "Hmm, I don't have a good answer for that. Try rephrasing or ask something about workouts, nutrition, or supplements!"
})
st.rerun()
if prompt := st.chat_input("Ask about fitness, nutrition, workouts..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
answer, score = get_best_answer(prompt)
if answer:
st.markdown(answer)
st.session_state.messages.append({"role": "assistant", "content": answer})
else:
fallback = "I don't have a specific answer for that. Try asking about protein intake, workout splits, supplements, fat loss, or muscle building!"
st.markdown(fallback)
st.session_state.messages.append({"role": "assistant", "content": fallback})