Update app.py
Browse files
app.py
CHANGED
@@ -119,13 +119,15 @@ def ask_llama(conversation_history, category, is_final_guess=False):
|
|
119 |
}
|
120 |
|
121 |
system_prompt = f"""You're playing 20 questions to guess a {category}. Follow these rules:
|
122 |
-
1. Ask strategic, non-repeating yes/no questions that narrow down possibilities
|
123 |
-
2. Consider all previous answers carefully before asking next question
|
124 |
-
3. If you're very confident (80%+ sure), respond with "Final Guess: [your guess]"
|
125 |
-
4. For places: ask about continent, climate, famous landmarks, country, city or population
|
126 |
-
5. For people: ask about fictional or real, profession, gender, alive/dead, nationality, or fame
|
127 |
-
6. For objects: ask about size, color, usage, material, or where it's found
|
128 |
-
7. Never repeat questions and always make progress toward guessing
|
|
|
|
|
129 |
|
130 |
if is_final_guess:
|
131 |
prompt = f"""Based on these answers about a {category}, provide ONLY your final guess with no extra text:
|
@@ -214,13 +216,14 @@ def main():
|
|
214 |
{"role": "assistant", "content": first_question}
|
215 |
]
|
216 |
st.session_state.game_state = "gameplay"
|
|
|
217 |
st.rerun()
|
218 |
|
219 |
# Gameplay screen
|
220 |
elif st.session_state.game_state == "gameplay":
|
221 |
current_question = st.session_state.questions[st.session_state.current_q]
|
222 |
|
223 |
-
#
|
224 |
if "Final Guess:" in current_question:
|
225 |
st.session_state.final_guess = current_question.split("Final Guess:")[1].strip()
|
226 |
st.session_state.game_state = "confirm_guess"
|
@@ -242,13 +245,12 @@ def main():
|
|
242 |
{"role": "user", "content": answer_input}
|
243 |
)
|
244 |
|
245 |
-
# Generate next response
|
246 |
next_response = ask_llama(
|
247 |
st.session_state.conversation_history,
|
248 |
st.session_state.category
|
249 |
)
|
250 |
|
251 |
-
# Check if AI made a guess
|
252 |
if "Final Guess:" in next_response:
|
253 |
st.session_state.final_guess = next_response.split("Final Guess:")[1].strip()
|
254 |
st.session_state.game_state = "confirm_guess"
|
@@ -279,7 +281,7 @@ def main():
|
|
279 |
st.markdown(f"**You:** {msg['query']}")
|
280 |
st.markdown(f"**Help Assistant:** {msg['response']}")
|
281 |
|
282 |
-
#
|
283 |
elif st.session_state.game_state == "confirm_guess":
|
284 |
st.markdown(f'<div class="question-box">🤖 My Final Guess:<br><br>'
|
285 |
f'<strong>Is it {st.session_state.final_guess}?</strong></div>',
|
@@ -294,9 +296,8 @@ def main():
|
|
294 |
if confirm_input == "yes":
|
295 |
st.session_state.game_state = "result"
|
296 |
st.rerun()
|
297 |
-
st.stop() # Immediately halt further execution
|
298 |
else:
|
299 |
-
# Add negative response to history and continue gameplay
|
300 |
st.session_state.conversation_history.append(
|
301 |
{"role": "user", "content": "no"}
|
302 |
)
|
@@ -333,7 +334,7 @@ def main():
|
|
333 |
time.sleep(1)
|
334 |
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{st.session_state.final_guess}</div>',
|
335 |
unsafe_allow_html=True)
|
336 |
-
st.markdown(f"<p style='text-align:center'>Guessed in {
|
337 |
unsafe_allow_html=True)
|
338 |
|
339 |
if st.button("Play Again", key="play_again"):
|
|
|
119 |
}
|
120 |
|
121 |
system_prompt = f"""You're playing 20 questions to guess a {category}. Follow these rules:
|
122 |
+
1. Ask strategic, non-repeating yes/no questions that narrow down possibilities.
|
123 |
+
2. Consider all previous answers carefully before asking the next question.
|
124 |
+
3. If you're very confident (80%+ sure), respond with "Final Guess: [your guess]".
|
125 |
+
4. For places: ask about continent, climate, famous landmarks, country, city or population.
|
126 |
+
5. For people: ask about fictional or real, profession, gender, alive/dead, nationality, or fame.
|
127 |
+
6. For objects: ask about size, color, usage, material, or where it's found.
|
128 |
+
7. Never repeat questions and always make progress toward guessing.
|
129 |
+
8. If the guess is generic (e.g. "bed") and the player confirms it with "yes", end the game.
|
130 |
+
9. If the guess is generic but the answer is "no", ask additional questions to refine the guess."""
|
131 |
|
132 |
if is_final_guess:
|
133 |
prompt = f"""Based on these answers about a {category}, provide ONLY your final guess with no extra text:
|
|
|
216 |
{"role": "assistant", "content": first_question}
|
217 |
]
|
218 |
st.session_state.game_state = "gameplay"
|
219 |
+
st.session_state.current_q = 0 # start at question 0
|
220 |
st.rerun()
|
221 |
|
222 |
# Gameplay screen
|
223 |
elif st.session_state.game_state == "gameplay":
|
224 |
current_question = st.session_state.questions[st.session_state.current_q]
|
225 |
|
226 |
+
# If the AI made a final guess, switch to confirmation
|
227 |
if "Final Guess:" in current_question:
|
228 |
st.session_state.final_guess = current_question.split("Final Guess:")[1].strip()
|
229 |
st.session_state.game_state = "confirm_guess"
|
|
|
245 |
{"role": "user", "content": answer_input}
|
246 |
)
|
247 |
|
248 |
+
# Generate next response from Llama
|
249 |
next_response = ask_llama(
|
250 |
st.session_state.conversation_history,
|
251 |
st.session_state.category
|
252 |
)
|
253 |
|
|
|
254 |
if "Final Guess:" in next_response:
|
255 |
st.session_state.final_guess = next_response.split("Final Guess:")[1].strip()
|
256 |
st.session_state.game_state = "confirm_guess"
|
|
|
281 |
st.markdown(f"**You:** {msg['query']}")
|
282 |
st.markdown(f"**Help Assistant:** {msg['response']}")
|
283 |
|
284 |
+
# Confirm guess screen
|
285 |
elif st.session_state.game_state == "confirm_guess":
|
286 |
st.markdown(f'<div class="question-box">🤖 My Final Guess:<br><br>'
|
287 |
f'<strong>Is it {st.session_state.final_guess}?</strong></div>',
|
|
|
296 |
if confirm_input == "yes":
|
297 |
st.session_state.game_state = "result"
|
298 |
st.rerun()
|
|
|
299 |
else:
|
300 |
+
# Add the negative response to history and continue gameplay
|
301 |
st.session_state.conversation_history.append(
|
302 |
{"role": "user", "content": "no"}
|
303 |
)
|
|
|
334 |
time.sleep(1)
|
335 |
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{st.session_state.final_guess}</div>',
|
336 |
unsafe_allow_html=True)
|
337 |
+
st.markdown(f"<p style='text-align:center'>Guessed in {st.session_state.current_q + 1} questions</p>",
|
338 |
unsafe_allow_html=True)
|
339 |
|
340 |
if st.button("Play Again", key="play_again"):
|