Update app.py
Browse files
app.py
CHANGED
@@ -8,181 +8,123 @@ import os
|
|
8 |
@st.cache_resource
|
9 |
def get_help_agent():
|
10 |
from transformers import pipeline
|
11 |
-
# Using BlenderBot 400M Distill as the public conversational model
|
12 |
return pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
13 |
|
14 |
-
#
|
15 |
def inject_custom_css():
|
16 |
st.markdown("""
|
17 |
<style>
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
22 |
margin: 0;
|
23 |
padding: 0;
|
24 |
-
|
25 |
}
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
.title {
|
28 |
-
font-size:
|
29 |
-
font-weight:
|
30 |
-
|
31 |
-
-webkit-background-clip: text;
|
32 |
-
-webkit-text-fill-color: transparent;
|
33 |
text-align: center;
|
34 |
-
margin:
|
35 |
-
letter-spacing: -1px;
|
36 |
}
|
37 |
-
|
38 |
.subtitle {
|
39 |
-
font-size: 1.
|
40 |
text-align: center;
|
41 |
-
color: #
|
42 |
-
margin-bottom:
|
43 |
-
font-weight: 500;
|
44 |
}
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
padding: 2rem;
|
50 |
-
margin: 1.5rem
|
51 |
-
box-shadow: 0
|
52 |
-
|
53 |
-
|
54 |
-
}
|
55 |
-
|
56 |
-
.question-container::after {
|
57 |
-
content: '';
|
58 |
-
position: absolute;
|
59 |
-
bottom: -10px;
|
60 |
-
left: 40px;
|
61 |
-
width: 0;
|
62 |
-
height: 0;
|
63 |
-
border-left: 12px solid transparent;
|
64 |
-
border-right: 12px solid transparent;
|
65 |
-
border-top: 12px solid #f7fafc;
|
66 |
-
}
|
67 |
-
|
68 |
-
.question-text {
|
69 |
-
font-size: 1.25rem !important;
|
70 |
-
color: #2d3748 !important;
|
71 |
-
line-height: 1.6;
|
72 |
-
margin-bottom: 0;
|
73 |
}
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
border-radius: 12px !important;
|
78 |
-
|
79 |
-
padding: 0.75rem 1rem !important;
|
80 |
-
font-size: 1rem !important;
|
81 |
-
transition: all 0.3s ease !important;
|
82 |
-
}
|
83 |
-
|
84 |
-
.input-box:focus {
|
85 |
-
border-color: #667eea !important;
|
86 |
-
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2) !important;
|
87 |
-
}
|
88 |
-
|
89 |
-
.submit-btn {
|
90 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
91 |
-
color: white !important;
|
92 |
-
border: none !important;
|
93 |
-
padding: 0.75rem 2rem !important;
|
94 |
-
border-radius: 10px !important;
|
95 |
font-weight: 600 !important;
|
96 |
-
|
|
|
|
|
|
|
97 |
}
|
98 |
-
|
99 |
-
.
|
100 |
-
transform:
|
101 |
-
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3) !important;
|
102 |
}
|
103 |
-
|
104 |
-
.
|
105 |
-
|
106 |
-
|
107 |
-
border-radius: 4px;
|
108 |
-
margin: 1.5rem 0;
|
109 |
-
overflow: hidden;
|
110 |
}
|
111 |
-
|
112 |
-
.
|
113 |
-
|
114 |
-
|
115 |
-
transition: width 0.3s ease;
|
116 |
}
|
117 |
-
|
|
|
118 |
.final-reveal {
|
119 |
-
animation:
|
120 |
font-size: 2.5rem;
|
121 |
-
|
122 |
-
-webkit-background-clip: text;
|
123 |
-
-webkit-text-fill-color: transparent;
|
124 |
text-align: center;
|
125 |
margin: 2rem 0;
|
126 |
-
|
127 |
-
}
|
128 |
-
|
129 |
-
@keyframes scaleUp {
|
130 |
-
from { transform: scale(0.8); opacity: 0; }
|
131 |
-
to { transform: scale(1); opacity: 1; }
|
132 |
}
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
padding: 1.5rem;
|
138 |
-
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
|
139 |
-
border: 1px solid #e2e8f0;
|
140 |
-
}
|
141 |
-
|
142 |
-
.user-message {
|
143 |
-
background: #667eea;
|
144 |
-
color: white;
|
145 |
-
border-radius: 12px 12px 0 12px;
|
146 |
-
padding: 0.75rem 1rem;
|
147 |
-
margin: 0.5rem 0;
|
148 |
-
max-width: 80%;
|
149 |
-
margin-left: auto;
|
150 |
}
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
160 |
}
|
161 |
-
|
|
|
162 |
.confidence-meter {
|
163 |
-
height:
|
164 |
-
background: #
|
165 |
-
border-radius:
|
166 |
-
margin:
|
167 |
-
position: relative;
|
168 |
-
overflow: hidden;
|
169 |
-
}
|
170 |
-
|
171 |
-
.confidence-fill {
|
172 |
-
height: 100%;
|
173 |
-
background: linear-gradient(90deg, #48bb78 0%, #38a169 100%);
|
174 |
-
width: 75%;
|
175 |
-
animation: confidenceAnim 1.5s ease-in-out;
|
176 |
}
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
181 |
}
|
|
|
182 |
</style>
|
183 |
""", unsafe_allow_html=True)
|
184 |
|
185 |
-
# Confetti animation
|
186 |
def show_confetti():
|
187 |
html("""
|
188 |
<canvas id="confetti-canvas" class="confetti"></canvas>
|
@@ -190,36 +132,102 @@ def show_confetti():
|
|
190 |
<script>
|
191 |
const canvas = document.getElementById('confetti-canvas');
|
192 |
const confetti = confetti.create(canvas, { resize: true });
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
spread: 100,
|
199 |
-
startVelocity: 25,
|
200 |
-
origin: { x: x, y: y },
|
201 |
-
colors: ['#667eea', '#764ba2', '#48bb78']
|
202 |
-
});
|
203 |
-
}
|
204 |
-
|
205 |
-
for(let i=0; i<50; i++) {
|
206 |
-
setTimeout(() => {
|
207 |
-
particle(Math.random(), Math.random());
|
208 |
-
}, i*50);
|
209 |
-
}
|
210 |
-
|
211 |
setTimeout(() => { canvas.remove(); }, 5000);
|
212 |
</script>
|
213 |
""")
|
214 |
|
215 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
def main():
|
219 |
-
inject_custom_css()
|
220 |
|
221 |
st.markdown('<div class="title">KASOTI</div>', unsafe_allow_html=True)
|
222 |
-
st.markdown('<div class="subtitle">
|
223 |
|
224 |
if 'game_state' not in st.session_state:
|
225 |
st.session_state.game_state = "start"
|
@@ -229,36 +237,27 @@ def main():
|
|
229 |
st.session_state.conversation_history = []
|
230 |
st.session_state.category = None
|
231 |
st.session_state.final_guess = None
|
232 |
-
st.session_state.help_conversation = []
|
233 |
|
234 |
-
# Start screen
|
235 |
if st.session_state.game_state == "start":
|
236 |
st.markdown("""
|
237 |
-
<div class="question-
|
238 |
-
<h3
|
239 |
-
<p
|
240 |
-
Choose
|
241 |
-
<
|
242 |
-
<
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
<strong>🌍 Place</strong><br>
|
248 |
-
<span style="color: #718096;">City, country, landmark, geographical location</span>
|
249 |
-
</div>
|
250 |
-
<div style="background: #f0f4f8; padding: 1rem; border-radius: 12px; margin: 0.5rem 0;">
|
251 |
-
<strong>📦 Object</strong><br>
|
252 |
-
<span style="color: #718096;">Everyday item, tool, vehicle, etc.</span>
|
253 |
-
</div>
|
254 |
-
</div>
|
255 |
</div>
|
256 |
""", unsafe_allow_html=True)
|
257 |
|
258 |
with st.form("start_form"):
|
259 |
-
category_input = st.text_input("Enter category (person/place/object):"
|
260 |
-
|
261 |
-
if st.form_submit_button("Start Game", use_container_width=True):
|
262 |
if not category_input:
|
263 |
st.error("Please enter a category!")
|
264 |
elif category_input not in ["person", "place", "object"]:
|
@@ -275,48 +274,24 @@ def main():
|
|
275 |
st.session_state.game_state = "gameplay"
|
276 |
st.experimental_rerun()
|
277 |
|
278 |
-
# Gameplay screen
|
279 |
elif st.session_state.game_state == "gameplay":
|
280 |
-
# Add progress bar
|
281 |
-
progress = (st.session_state.current_q + 1) / 20
|
282 |
-
st.markdown(f"""
|
283 |
-
<div class="progress-bar">
|
284 |
-
<div class="progress-fill" style="width: {progress * 100}%"></div>
|
285 |
-
</div>
|
286 |
-
<div style="text-align: center; color: #4a5568; margin-bottom: 1.5rem;">
|
287 |
-
Question {st.session_state.current_q + 1} of 20
|
288 |
-
</div>
|
289 |
-
""", unsafe_allow_html=True)
|
290 |
-
|
291 |
current_question = st.session_state.questions[st.session_state.current_q]
|
292 |
|
|
|
293 |
if "Final Guess:" in current_question:
|
294 |
st.session_state.final_guess = current_question.split("Final Guess:")[1].strip()
|
295 |
st.session_state.game_state = "confirm_guess"
|
296 |
st.experimental_rerun()
|
297 |
|
298 |
-
st.markdown(f""
|
299 |
-
|
300 |
-
|
301 |
-
</div>
|
302 |
-
""", unsafe_allow_html=True)
|
303 |
-
|
304 |
-
# Add confidence meter animation
|
305 |
-
st.markdown("""
|
306 |
-
<div class="confidence-meter">
|
307 |
-
<div class="confidence-fill"></div>
|
308 |
-
</div>
|
309 |
-
<div style="text-align: right; color: #718096; font-size: 0.9rem;">
|
310 |
-
AI Confidence Level: 75%
|
311 |
-
</div>
|
312 |
-
""", unsafe_allow_html=True)
|
313 |
|
314 |
with st.form("answer_form"):
|
315 |
-
answer_input = st.text_input("Your answer (yes/no/both):",
|
316 |
-
key=f"answer_{st.session_state.current_q}"
|
317 |
-
|
318 |
-
if st.form_submit_button("Submit Answer", use_container_width=True):
|
319 |
-
# Original answer handling logic remains unchanged
|
320 |
if answer_input not in ["yes", "no", "both"]:
|
321 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
322 |
else:
|
@@ -331,6 +306,7 @@ def main():
|
|
331 |
st.session_state.category
|
332 |
)
|
333 |
|
|
|
334 |
if "Final Guess:" in next_response:
|
335 |
st.session_state.final_guess = next_response.split("Final Guess:")[1].strip()
|
336 |
st.session_state.game_state = "confirm_guess"
|
@@ -341,19 +317,16 @@ def main():
|
|
341 |
)
|
342 |
st.session_state.current_q += 1
|
343 |
|
|
|
344 |
if st.session_state.current_q >= 20:
|
345 |
st.session_state.game_state = "result"
|
346 |
|
347 |
st.experimental_rerun()
|
348 |
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
st.markdown('<div class="help-chat">', unsafe_allow_html=True)
|
354 |
-
help_query = st.text_input("Type your question:", key="help_query",
|
355 |
-
placeholder="How should I answer this?")
|
356 |
-
if st.button("Send", use_container_width=True, key="send_help"):
|
357 |
if help_query:
|
358 |
help_response = ask_help_agent(help_query)
|
359 |
st.session_state.help_conversation.append({"query": help_query, "response": help_response})
|
@@ -361,8 +334,67 @@ def main():
|
|
361 |
st.error("Please enter a query!")
|
362 |
if st.session_state.help_conversation:
|
363 |
for msg in st.session_state.help_conversation:
|
364 |
-
st.markdown(f
|
365 |
-
st.markdown(f
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
if __name__ == "__main__":
|
368 |
-
main()
|
|
|
8 |
@st.cache_resource
|
9 |
def get_help_agent():
|
10 |
from transformers import pipeline
|
11 |
+
# Using BlenderBot 400M Distill as the public conversational model
|
12 |
return pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
13 |
|
14 |
+
# Custom CSS for enhanced professional look with UI improvements
|
15 |
def inject_custom_css():
|
16 |
st.markdown("""
|
17 |
<style>
|
18 |
+
/* UPDATED: Importing and configuring a modern font with improved responsiveness */
|
19 |
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
|
20 |
|
21 |
+
html, body {
|
22 |
+
background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
|
23 |
+
color: #333;
|
24 |
margin: 0;
|
25 |
padding: 0;
|
26 |
+
height: 100%;
|
27 |
}
|
28 |
+
|
29 |
+
* {
|
30 |
+
font-family: 'Poppins', sans-serif;
|
31 |
+
}
|
32 |
+
|
33 |
.title {
|
34 |
+
font-size: 3rem !important;
|
35 |
+
font-weight: 700 !important;
|
36 |
+
color: #6C63FF !important;
|
|
|
|
|
37 |
text-align: center;
|
38 |
+
margin: 2rem 0 0.5rem 0;
|
|
|
39 |
}
|
40 |
+
|
41 |
.subtitle {
|
42 |
+
font-size: 1.2rem !important;
|
43 |
text-align: center;
|
44 |
+
color: #666 !important;
|
45 |
+
margin-bottom: 2rem;
|
|
|
46 |
}
|
47 |
+
|
48 |
+
/* UPDATED: Improved question box with subtle border-radius & shadow */
|
49 |
+
.question-box {
|
50 |
+
background: #ffffff;
|
51 |
+
border-radius: 15px;
|
52 |
padding: 2rem;
|
53 |
+
margin: 1.5rem auto;
|
54 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
55 |
+
max-width: 650px;
|
56 |
+
color: #333 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
+
|
59 |
+
/* UPDATED: Button styling improvements */
|
60 |
+
.answer-btn {
|
61 |
border-radius: 12px !important;
|
62 |
+
padding: 0.5rem 1.5rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
font-weight: 600 !important;
|
64 |
+
margin: 0.5rem !important;
|
65 |
+
border: none;
|
66 |
+
cursor: pointer;
|
67 |
+
transition: background-color 0.3s ease, transform 0.2s ease;
|
68 |
}
|
69 |
+
|
70 |
+
.answer-btn:hover {
|
71 |
+
transform: scale(1.05);
|
|
|
72 |
}
|
73 |
+
|
74 |
+
.yes-btn {
|
75 |
+
background: #6C63FF !important;
|
76 |
+
color: white !important;
|
|
|
|
|
|
|
77 |
}
|
78 |
+
|
79 |
+
.no-btn {
|
80 |
+
background: #FF6B6B !important;
|
81 |
+
color: white !important;
|
|
|
82 |
}
|
83 |
+
|
84 |
+
/* UPDATED: Enhanced final reveal with glow effect */
|
85 |
.final-reveal {
|
86 |
+
animation: fadeIn 2s;
|
87 |
font-size: 2.5rem;
|
88 |
+
color: #6C63FF;
|
|
|
|
|
89 |
text-align: center;
|
90 |
margin: 2rem 0;
|
91 |
+
text-shadow: 0 0 10px rgba(108, 99, 255, 0.4);
|
|
|
|
|
|
|
|
|
|
|
92 |
}
|
93 |
+
|
94 |
+
@keyframes fadeIn {
|
95 |
+
from { opacity: 0; }
|
96 |
+
to { opacity: 1; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
}
|
98 |
+
|
99 |
+
/* UPDATED: Full screen confetti canvas styling remains the same */
|
100 |
+
.confetti {
|
101 |
+
position: fixed;
|
102 |
+
top: 0;
|
103 |
+
left: 0;
|
104 |
+
width: 100%;
|
105 |
+
height: 100%;
|
106 |
+
pointer-events: none;
|
107 |
+
z-index: 1000;
|
108 |
}
|
109 |
+
|
110 |
+
/* UPDATED: Confidence meter visual improvement */
|
111 |
.confidence-meter {
|
112 |
+
height: 10px;
|
113 |
+
background: linear-gradient(90deg, #FF6B6B 0%, #6C63FF 100%);
|
114 |
+
border-radius: 5px;
|
115 |
+
margin: 10px 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
}
|
117 |
+
|
118 |
+
/* UPDATED: Styling for the forms to center them in the layout */
|
119 |
+
form {
|
120 |
+
max-width: 650px;
|
121 |
+
margin: 0 auto;
|
122 |
}
|
123 |
+
|
124 |
</style>
|
125 |
""", unsafe_allow_html=True)
|
126 |
|
127 |
+
# Confetti animation
|
128 |
def show_confetti():
|
129 |
html("""
|
130 |
<canvas id="confetti-canvas" class="confetti"></canvas>
|
|
|
132 |
<script>
|
133 |
const canvas = document.getElementById('confetti-canvas');
|
134 |
const confetti = confetti.create(canvas, { resize: true });
|
135 |
+
confetti({
|
136 |
+
particleCount: 150,
|
137 |
+
spread: 70,
|
138 |
+
origin: { y: 0.6 }
|
139 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
setTimeout(() => { canvas.remove(); }, 5000);
|
141 |
</script>
|
142 |
""")
|
143 |
|
144 |
+
# Enhanced AI question generation for guessing game using Llama model
|
145 |
+
def ask_llama(conversation_history, category, is_final_guess=False):
|
146 |
+
api_url = "https://api.groq.com/openai/v1/chat/completions"
|
147 |
+
headers = {
|
148 |
+
"Authorization": "Bearer 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",
|
149 |
+
"Content-Type": "application/json"
|
150 |
+
}
|
151 |
+
|
152 |
+
system_prompt = f"""You're playing 20 questions to guess a {category}. Follow these rules:
|
153 |
+
1. Ask strategic, non-repeating yes/no questions that narrow down possibilities
|
154 |
+
2. Consider all previous answers carefully before asking next question
|
155 |
+
3. If you're very confident (80%+ sure), respond with "Final Guess: [your guess]"
|
156 |
+
4. For places: ask about continent, climate, famous landmarks, country, city or population
|
157 |
+
5. For people: ask about fictional or real, profession, gender, alive/dead, nationality, or fame
|
158 |
+
6. For objects: ask about size, color, usage, material, or where it's found
|
159 |
+
7. Never repeat questions and always make progress toward guessing"""
|
160 |
+
|
161 |
+
if is_final_guess:
|
162 |
+
prompt = f"""Based on these answers about a {category}, provide ONLY your final guess with no extra text:
|
163 |
+
{conversation_history}"""
|
164 |
+
else:
|
165 |
+
prompt = "Ask your next strategic yes/no question that will best narrow down the possibilities."
|
166 |
+
|
167 |
+
messages = [
|
168 |
+
{"role": "system", "content": system_prompt},
|
169 |
+
*conversation_history,
|
170 |
+
{"role": "user", "content": prompt}
|
171 |
+
]
|
172 |
+
|
173 |
+
data = {
|
174 |
+
"model": "llama-3.3-70b-versatile",
|
175 |
+
"messages": messages,
|
176 |
+
"temperature": 0.7 if is_final_guess else 0.8,
|
177 |
+
"max_tokens": 100
|
178 |
+
}
|
179 |
|
180 |
+
try:
|
181 |
+
response = requests.post(api_url, headers=headers, json=data)
|
182 |
+
response.raise_for_status()
|
183 |
+
return response.json()["choices"][0]["message"]["content"]
|
184 |
+
except Exception as e:
|
185 |
+
st.error(f"Error calling Llama API: {str(e)}")
|
186 |
+
return "Could not generate question"
|
187 |
+
|
188 |
+
# New function for the help AI assistant using the Hugging Face InferenceClient
|
189 |
+
def ask_help_agent(query):
|
190 |
+
try:
|
191 |
+
from huggingface_hub import InferenceClient
|
192 |
+
# Initialize the client with the provided model
|
193 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.environ.get("HF_HUB_TOKEN"))
|
194 |
+
system_message = "You are a friendly Chatbot."
|
195 |
+
|
196 |
+
# Build history from session state (if any)
|
197 |
+
history = []
|
198 |
+
if "help_conversation" in st.session_state:
|
199 |
+
for msg in st.session_state.help_conversation:
|
200 |
+
history.append((msg.get("query", ""), msg.get("response", "")))
|
201 |
+
|
202 |
+
messages = [{"role": "system", "content": system_message}]
|
203 |
+
for user_msg, bot_msg in history:
|
204 |
+
if user_msg:
|
205 |
+
messages.append({"role": "user", "content": user_msg})
|
206 |
+
if bot_msg:
|
207 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
208 |
+
messages.append({"role": "user", "content": query})
|
209 |
+
|
210 |
+
response_text = ""
|
211 |
+
# Using streaming to collect the entire response from the model
|
212 |
+
for message in client.chat_completion(
|
213 |
+
messages,
|
214 |
+
max_tokens=150,
|
215 |
+
stream=True,
|
216 |
+
temperature=0.7,
|
217 |
+
top_p=0.95,
|
218 |
+
):
|
219 |
+
token = message.choices[0].delta.content
|
220 |
+
response_text += token
|
221 |
+
return response_text
|
222 |
+
except Exception as e:
|
223 |
+
return f"Error in help agent: {str(e)}"
|
224 |
+
|
225 |
+
# Main game logic
|
226 |
def main():
|
227 |
+
inject_custom_css() # UPDATED: Now uses enhanced CSS
|
228 |
|
229 |
st.markdown('<div class="title">KASOTI</div>', unsafe_allow_html=True)
|
230 |
+
st.markdown('<div class="subtitle">The Smart Guessing Game</div>', unsafe_allow_html=True)
|
231 |
|
232 |
if 'game_state' not in st.session_state:
|
233 |
st.session_state.game_state = "start"
|
|
|
237 |
st.session_state.conversation_history = []
|
238 |
st.session_state.category = None
|
239 |
st.session_state.final_guess = None
|
240 |
+
st.session_state.help_conversation = [] # separate history for help agent
|
241 |
|
242 |
+
# Start screen
|
243 |
if st.session_state.game_state == "start":
|
244 |
st.markdown("""
|
245 |
+
<div class="question-box">
|
246 |
+
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
247 |
+
<p>Think of something and I'll try to guess it in 20 questions or less!</p>
|
248 |
+
<p>Choose a category:</p>
|
249 |
+
<ul>
|
250 |
+
<li><strong>Person</strong> - celebrity, fictional character, historical figure</li>
|
251 |
+
<li><strong>Place</strong> - city, country, landmark, geographical location</li>
|
252 |
+
<li><strong>Object</strong> - everyday item, tool, vehicle, etc.</li>
|
253 |
+
</ul>
|
254 |
+
<p>Type your category below to begin:</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
</div>
|
256 |
""", unsafe_allow_html=True)
|
257 |
|
258 |
with st.form("start_form"):
|
259 |
+
category_input = st.text_input("Enter category (person/place/object):").strip().lower()
|
260 |
+
if st.form_submit_button("Start Game"):
|
|
|
261 |
if not category_input:
|
262 |
st.error("Please enter a category!")
|
263 |
elif category_input not in ["person", "place", "object"]:
|
|
|
274 |
st.session_state.game_state = "gameplay"
|
275 |
st.experimental_rerun()
|
276 |
|
277 |
+
# Gameplay screen
|
278 |
elif st.session_state.game_state == "gameplay":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
current_question = st.session_state.questions[st.session_state.current_q]
|
280 |
|
281 |
+
# Check if AI made a guess
|
282 |
if "Final Guess:" in current_question:
|
283 |
st.session_state.final_guess = current_question.split("Final Guess:")[1].strip()
|
284 |
st.session_state.game_state = "confirm_guess"
|
285 |
st.experimental_rerun()
|
286 |
|
287 |
+
st.markdown(f'<div class="question-box">Question {st.session_state.current_q + 1}/20:<br><br>'
|
288 |
+
f'<strong>{current_question}</strong></div>',
|
289 |
+
unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
|
291 |
with st.form("answer_form"):
|
292 |
+
answer_input = st.text_input("Your answer (yes/no/both):",
|
293 |
+
key=f"answer_{st.session_state.current_q}").strip().lower()
|
294 |
+
if st.form_submit_button("Submit"):
|
|
|
|
|
295 |
if answer_input not in ["yes", "no", "both"]:
|
296 |
st.error("Please answer with 'yes', 'no', or 'both'!")
|
297 |
else:
|
|
|
306 |
st.session_state.category
|
307 |
)
|
308 |
|
309 |
+
# Check if AI made a guess
|
310 |
if "Final Guess:" in next_response:
|
311 |
st.session_state.final_guess = next_response.split("Final Guess:")[1].strip()
|
312 |
st.session_state.game_state = "confirm_guess"
|
|
|
317 |
)
|
318 |
st.session_state.current_q += 1
|
319 |
|
320 |
+
# Stop after 20 questions max
|
321 |
if st.session_state.current_q >= 20:
|
322 |
st.session_state.game_state = "result"
|
323 |
|
324 |
st.experimental_rerun()
|
325 |
|
326 |
+
# Side Help Option: independent chat with an AI help assistant using Hugging Face model
|
327 |
+
with st.expander("Need Help? Chat with AI Assistant"):
|
328 |
+
help_query = st.text_input("Enter your help query:", key="help_query")
|
329 |
+
if st.button("Send", key="send_help"):
|
|
|
|
|
|
|
|
|
330 |
if help_query:
|
331 |
help_response = ask_help_agent(help_query)
|
332 |
st.session_state.help_conversation.append({"query": help_query, "response": help_response})
|
|
|
334 |
st.error("Please enter a query!")
|
335 |
if st.session_state.help_conversation:
|
336 |
for msg in st.session_state.help_conversation:
|
337 |
+
st.markdown(f"**You:** {msg['query']}")
|
338 |
+
st.markdown(f"**Help Assistant:** {msg['response']}")
|
339 |
+
|
340 |
+
# Guess confirmation screen using text input response
|
341 |
+
elif st.session_state.game_state == "confirm_guess":
|
342 |
+
st.markdown(f'<div class="question-box">🤖 My Final Guess:<br><br>'
|
343 |
+
f'<strong>Is it {st.session_state.final_guess}?</strong></div>',
|
344 |
+
unsafe_allow_html=True)
|
345 |
+
|
346 |
+
with st.form("confirm_form"):
|
347 |
+
confirm_input = st.text_input("Type your answer (yes/no/both):", key="confirm_input").strip().lower()
|
348 |
+
if st.form_submit_button("Submit"):
|
349 |
+
if confirm_input not in ["yes", "no", "both"]:
|
350 |
+
st.error("Please answer with 'yes', 'no', or 'both'!")
|
351 |
+
else:
|
352 |
+
if confirm_input == "yes":
|
353 |
+
st.session_state.game_state = "result"
|
354 |
+
st.experimental_rerun()
|
355 |
+
st.stop() # Immediately halt further execution
|
356 |
+
else:
|
357 |
+
# Add negative response to history and continue gameplay
|
358 |
+
st.session_state.conversation_history.append(
|
359 |
+
{"role": "user", "content": "no"}
|
360 |
+
)
|
361 |
+
st.session_state.game_state = "gameplay"
|
362 |
+
next_response = ask_llama(
|
363 |
+
st.session_state.conversation_history,
|
364 |
+
st.session_state.category
|
365 |
+
)
|
366 |
+
st.session_state.questions.append(next_response)
|
367 |
+
st.session_state.conversation_history.append(
|
368 |
+
{"role": "assistant", "content": next_response}
|
369 |
+
)
|
370 |
+
st.session_state.current_q += 1
|
371 |
+
st.experimental_rerun()
|
372 |
+
|
373 |
+
# Result screen
|
374 |
+
elif st.session_state.game_state == "result":
|
375 |
+
if not st.session_state.final_guess:
|
376 |
+
qa_history = "\n".join(
|
377 |
+
[f"Q{i+1}: {q}\nA: {a}"
|
378 |
+
for i, (q, a) in enumerate(zip(st.session_state.questions, st.session_state.answers))]
|
379 |
+
)
|
380 |
+
final_guess = ask_llama(
|
381 |
+
[{"role": "user", "content": qa_history}],
|
382 |
+
st.session_state.category,
|
383 |
+
is_final_guess=True
|
384 |
+
)
|
385 |
+
st.session_state.final_guess = final_guess.split("Final Guess:")[-1].strip()
|
386 |
+
|
387 |
+
show_confetti()
|
388 |
+
st.markdown(f'<div class="final-reveal">🎉 It\'s...</div>', unsafe_allow_html=True)
|
389 |
+
time.sleep(1)
|
390 |
+
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{st.session_state.final_guess}</div>',
|
391 |
+
unsafe_allow_html=True)
|
392 |
+
st.markdown(f"<p style='text-align:center'>Guessed in {len(st.session_state.questions)} questions</p>",
|
393 |
+
unsafe_allow_html=True)
|
394 |
+
|
395 |
+
if st.button("Play Again", key="play_again"):
|
396 |
+
st.session_state.clear()
|
397 |
+
st.experimental_rerun()
|
398 |
+
|
399 |
if __name__ == "__main__":
|
400 |
+
main()
|