Update app.py
Browse files58 unmodified lines596061626364656667686970717273747576777820 unmodified lines9910010110210310410510610721 unmodified lines1291301311321331341351361373 unmodified lines1411421431441451461471481491501511 unmodified line1531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072083 unmodified lines21221321421521621721821922022122222322422522622722822923058 unmodified linespointer-events: none;z-index: 1000;}.loading {display: inline-block;width: 20px;height: 20px;border: 3px solid rgba(108,99,255,.3);border-radius: 50%;border-top-color: #6C63FF;animation: spin 1s ease-in-out infinite;}@keyframes spin {to { transform: rotate(360deg); }}</style>""", unsafe_allow_html=True)20 unmodified lines"Content-Type": "application/json"}prompt = ("You're playing 20 questions to guess a " + category +". Ask strategic yes/no questions one at a time." +(" Based on the answers, what is your final guess? State only the guess." if is_final else ""))data = {"model": "llama-3.3-70b-versatile",21 unmodified linesst.session_state.game = {'state': 'start','category': None,'questions': [],'answers': [],'conversation': []}# Start Screen3 unmodified lines<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3><p>Think of something and I'll try to guess it with yes/no questions!</p><p>Choose a category:</p><ul><li><strong>person</strong> (celebrity, fictional character)</li><li><strong>place</strong> (city, country, location)</li><li><strong>object</strong> (something you can touch)</li></ul></div>""", unsafe_allow_html=True)1 unmodified linewith col1:if st.button("Person", use_container_width=True):st.session_state.game['category'] = 'person'st.session_state.game['state'] = 'playing'st.rerun()with col2:if st.button("Place", use_container_width=True):st.session_state.game['category'] = 'place'st.session_state.game['state'] = 'playing'st.rerun()with col3:if st.button("Object", use_container_width=True):st.session_state.game['category'] = 'object'st.session_state.game['state'] = 'playing'st.rerun()# Game Play Screenelif st.session_state.game['state'] == 'playing':# Generate first question if none existsif not st.session_state.game['questions']:question = ask_llama([{"role": "user", "content": "Ask your first yes/no question."}],st.session_state.game['category'])if question:st.session_state.game['questions'].append(question)st.session_state.game['conversation'].append({"role": "assistant", "content": question})st.rerun()else:st.error("Failed to generate question. Please try again.")st.session_state.game['state'] = 'start'st.rerun()# Show current questioncurrent_q = len(st.session_state.game['questions']) - 1st.markdown(f"""<div class="question-box">Question {current_q + 1}:<br><br><strong>{st.session_state.game['questions'][current_q]}</strong></div>""", unsafe_allow_html=True)# Answer formwith st.form("answer_form"):answer = st.radio("Your answer:", ["Yes", "No"], horizontal=True)if st.form_submit_button("Submit"):# Record answeranswer_lower = answer.lower()st.session_state.game['answers'].append(answer_lower)st.session_state.game['conversation'].append({"role": "user", "content": answer_lower})# Check if AI can guess nowif current_q >= 4: # Minimum 5 questions before checkingready = ask_llama(st.session_state.game['conversation'] + [{"role": "user", "content": "Can you guess now? Answer only 'yes' or 'no'."}3 unmodified linesif ready and ready.strip().lower() == 'yes':st.session_state.game['state'] = 'result'st.rerun()# Generate next question if under 20if current_q < 19:next_q = ask_llama(st.session_state.game['conversation'],st.session_state.game['category'])if next_q:st.session_state.game['questions'].append(next_q)st.session_state.game['conversation'].append({"role": "assistant", "content": next_q})st.rerun()else:st.error("Failed to generate next question")else:st.session_state.game['state'] = 'result'st.rerun()58 unmodified lines59606162636420 unmodified lines85868788899091929321 unmodified lines1151161171181191201211221231243 unmodified lines1281291301311321331 unmodified line1351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931943 unmodified lines19819920020120220320420520620720858 unmodified linespointer-events: none;z-index: 1000;}</style>""", unsafe_allow_html=True)20 unmodified lines"Content-Type": "application/json"}prompt = (f"You're playing 20 questions to guess a {category}. " +"Ask strategic yes/no questions one at a time." +(" Based on the answers, what is your final guess? State only the guess." if is_final else ""))data = {"model": "llama-3.3-70b-versatile",21 unmodified linesst.session_state.game = {'state': 'start','category': None,'current_question': None,'conversation': [],'question_count': 0,'waiting_for_answer': False}# Start Screen3 unmodified lines<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3><p>Think of something and I'll try to guess it with yes/no questions!</p><p>Choose a category:</p></div>""", unsafe_allow_html=True)1 unmodified linewith col1:if st.button("Person", use_container_width=True):st.session_state.game['category'] = 'person'st.session_state.game['state'] = 'generate_question'st.rerun()with col2:if st.button("Place", use_container_width=True):st.session_state.game['category'] = 'place'st.session_state.game['state'] = 'generate_question'st.rerun()with col3:if st.button("Object", use_container_width=True):st.session_state.game['category'] = 'object'st.session_state.game['state'] = 'generate_question'st.rerun()# Generate Question Stateelif st.session_state.game['state'] == 'generate_question':# Generate new questionquestion = ask_llama(st.session_state.game['conversation'],st.session_state.game['category'])if question:st.session_state.game['current_question'] = questionst.session_state.game['conversation'].append({"role": "assistant", "content": question})st.session_state.game['question_count'] += 1st.session_state.game['state'] = 'wait_for_answer'st.session_state.game['waiting_for_answer'] = Truest.rerun()else:st.error("Failed to generate question. Please try again.")st.session_state.game['state'] = 'start'st.rerun()# Wait for Answer Stateelif st.session_state.game['state'] == 'wait_for_answer':st.markdown(f"""<div class="question-box">Question {st.session_state.game['question_count']}:<br><br><strong>{st.session_state.game['current_question']}</strong></div>""", unsafe_allow_html=True)with st.form("answer_form"):answer = st.radio("Your answer:", ["Yes", "No"], index=None, horizontal=True)submitted = st.form_submit_button("Submit")if submitted and answer:# Record answeranswer_lower = answer.lower()st.session_state.game['conversation'].append({"role": "user", "content": answer_lower})st.session_state.game['waiting_for_answer'] = False# Check if we should guess or ask another questionif st.session_state.game['question_count'] >= 5: # Minimum 5 questions before checkingready = ask_llama(st.session_state.game['conversation'] + [{"role": "user", "content": "Can you guess now? Answer only 'yes' or 'no'."}3 unmodified linesif ready and ready.strip().lower() == 'yes':st.session_state.game['state'] = 'result'st.rerun()# Continue with next question if under 20if st.session_state.game['question_count'] < 20:st.session_state.game['state'] = 'generate_question'st.rerun()else:st.session_state.game['state'] = 'result'st.rerun()
app.py+46-68