import streamlit as st import time import requests import json from streamlit.components.v1 import html # Custom CSS for professional look def inject_custom_css(): st.markdown(""" """, unsafe_allow_html=True) # Confetti animation (for final reveal) def show_confetti(): html(""" """) # Mistral AI API call def ask_mistral(conversation_history, category): api_url = "https://api.mistral.ai/v1/chat/completions" headers = { "Authorization": "Bearer gz6lDXokxgR6cLY72oomALWcm7vhjzQ", "Content-Type": "application/json" } messages = [ { "role": "system", "content": f"You're playing 20 questions to guess a {category}. Ask strategic yes/no questions." } ] + conversation_history data = { "model": "mistral-tiny", "messages": messages, "temperature": 0.7, "max_tokens": 100 } try: response = requests.post(api_url, headers=headers, json=data) response.raise_for_status() return response.json()["choices"][0]["message"]["content"] except Exception as e: st.error(f"Error calling Mistral API: {str(e)}") return "Could not generate question" # Game logic def main(): inject_custom_css() st.markdown('
KASOTI
', unsafe_allow_html=True) st.markdown('
The Ultimate Guessing Game
', unsafe_allow_html=True) if 'game_state' not in st.session_state: st.session_state.game_state = "start" st.session_state.questions = [] st.session_state.current_q = 0 st.session_state.answers = [] st.session_state.conversation_history = [] st.session_state.category = None # Start screen if st.session_state.game_state == "start": st.markdown("""

Welcome to KASOTI 🎯

This is a fun guessing game! You'll think of something, and I'll ask maximum 20 Yes/No questions to guess what it is.

You can choose from three categories:

Type one of these categories below to begin: person, place, or object.

""", unsafe_allow_html=True) with st.form("start_form"): category_input = st.text_input("Enter category (person / place / object):").strip().lower() if st.form_submit_button("Start Game"): if not category_input: st.error("Please enter a category!") elif category_input not in ["person", "place", "object"]: st.error("Please enter either 'person', 'place', or 'object'!") else: st.session_state.category = category_input # Generate first question first_question = ask_mistral([ {"role": "user", "content": "Ask your first yes/no question."} ], category_input) st.session_state.questions = [first_question] st.session_state.conversation_history = [ {"role": "assistant", "content": first_question} ] st.session_state.game_state = "gameplay" st.rerun() # Gameplay screen elif st.session_state.game_state == "gameplay": current_question = st.session_state.questions[st.session_state.current_q] st.markdown(f'
Question {st.session_state.current_q + 1}/20:

' f'{current_question}
', unsafe_allow_html=True) with st.form("answer_form"): answer_input = st.text_input("Your answer (yes/no):").strip().lower() if st.form_submit_button("Submit"): if answer_input not in ["yes", "no"]: st.error("Please answer with 'yes' or 'no'!") else: # Record answer st.session_state.answers.append(answer_input) st.session_state.conversation_history.append( {"role": "user", "content": answer_input} ) # Check if we've reached max questions if st.session_state.current_q >= 19: # 0-based index st.session_state.game_state = "result" else: # Generate next question next_question = ask_mistral( st.session_state.conversation_history, st.session_state.category ) st.session_state.questions.append(next_question) st.session_state.conversation_history.append( {"role": "assistant", "content": next_question} ) st.session_state.current_q += 1 st.rerun() # Result screen elif st.session_state.game_state == "result": # Generate final guess final_guess = ask_mistral( st.session_state.conversation_history + [ {"role": "user", "content": "Based on all my answers, what is your final guess? Just state the guess directly."} ], st.session_state.category ) show_confetti() st.markdown('
🎉 I think it\'s...
', unsafe_allow_html=True) time.sleep(1) st.markdown(f'
{final_guess}
', unsafe_allow_html=True) if st.button("Play Again", key="play_again"): st.session_state.clear() st.rerun() if __name__ == "__main__": main()