|
import streamlit as st |
|
import time |
|
import requests |
|
from streamlit.components.v1 import html |
|
|
|
|
|
def inject_custom_css(): |
|
st.markdown(""" |
|
<style> |
|
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap'); |
|
|
|
* { |
|
font-family: 'Poppins', sans-serif; |
|
} |
|
|
|
.title { |
|
font-size: 3rem !important; |
|
font-weight: 700 !important; |
|
color: #6C63FF !important; |
|
text-align: center; |
|
margin-bottom: 0.5rem; |
|
} |
|
|
|
.subtitle { |
|
font-size: 1.2rem !important; |
|
text-align: center; |
|
color: #666 !important; |
|
margin-bottom: 2rem; |
|
} |
|
|
|
.question-box { |
|
background: #F8F9FA; |
|
border-radius: 15px; |
|
padding: 2rem; |
|
margin: 1.5rem 0; |
|
box-shadow: 0 4px 6px rgba(0,0,0,0.1); |
|
color: black; |
|
} |
|
|
|
.final-reveal { |
|
animation: fadeIn 2s; |
|
font-size: 2.5rem; |
|
color: #6C63FF; |
|
text-align: center; |
|
margin: 2rem 0; |
|
} |
|
|
|
@keyframes fadeIn { |
|
from { opacity: 0; } |
|
to { opacity: 1; } |
|
} |
|
|
|
.confetti { |
|
position: fixed; |
|
top: 0; |
|
left: 0; |
|
width: 100%; |
|
height: 100%; |
|
pointer-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) |
|
|
|
def show_confetti(): |
|
html(""" |
|
<canvas id="confetti-canvas" class="confetti"></canvas> |
|
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script> |
|
<script> |
|
const canvas = document.getElementById('confetti-canvas'); |
|
const confetti = confetti.create(canvas, { resize: true }); |
|
confetti({ |
|
particleCount: 150, |
|
spread: 70, |
|
origin: { y: 0.6 } |
|
}); |
|
setTimeout(() => canvas.remove(), 5000); |
|
</script> |
|
""") |
|
|
|
def ask_llama(messages, category, is_final=False): |
|
api_url = "https://api.groq.com/openai/v1/chat/completions" |
|
headers = { |
|
"Authorization": "Bearer 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", |
|
"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", |
|
"messages": [{"role": "system", "content": prompt}] + messages, |
|
"temperature": 0.7, |
|
"max_tokens": 100 |
|
} |
|
|
|
try: |
|
with st.spinner("Thinking..."): |
|
response = requests.post(api_url, headers=headers, json=data, timeout=10) |
|
response.raise_for_status() |
|
return response.json()["choices"][0]["message"]["content"] |
|
except Exception as e: |
|
st.error(f"API Error: {str(e)}") |
|
return None |
|
|
|
def main(): |
|
inject_custom_css() |
|
st.markdown('<div class="title">KASOTI</div>', unsafe_allow_html=True) |
|
st.markdown('<div class="subtitle">The Ultimate Guessing Game</div>', unsafe_allow_html=True) |
|
|
|
|
|
if 'game' not in st.session_state: |
|
st.session_state.game = { |
|
'state': 'start', |
|
'category': None, |
|
'questions': [], |
|
'answers': [], |
|
'conversation': [] |
|
} |
|
|
|
|
|
if st.session_state.game['state'] == 'start': |
|
st.markdown(""" |
|
<div class="question-box"> |
|
<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) |
|
|
|
col1, col2, col3 = st.columns(3) |
|
with 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() |
|
|
|
|
|
elif st.session_state.game['state'] == 'playing': |
|
|
|
if 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() |
|
|
|
|
|
current_q = len(st.session_state.game['questions']) - 1 |
|
st.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) |
|
|
|
|
|
with st.form("answer_form"): |
|
answer = st.radio("Your answer:", ["Yes", "No"], horizontal=True) |
|
if st.form_submit_button("Submit"): |
|
|
|
answer_lower = answer.lower() |
|
st.session_state.game['answers'].append(answer_lower) |
|
st.session_state.game['conversation'].append({"role": "user", "content": answer_lower}) |
|
|
|
|
|
if current_q >= 4: |
|
ready = ask_llama( |
|
st.session_state.game['conversation'] + [ |
|
{"role": "user", "content": "Can you guess now? Answer only 'yes' or 'no'."} |
|
], |
|
st.session_state.game['category'] |
|
) |
|
if ready and ready.strip().lower() == 'yes': |
|
st.session_state.game['state'] = 'result' |
|
st.rerun() |
|
|
|
|
|
if 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() |
|
|
|
|
|
elif st.session_state.game['state'] == 'result': |
|
guess = ask_llama( |
|
st.session_state.game['conversation'], |
|
st.session_state.game['category'], |
|
is_final=True |
|
) |
|
|
|
if guess: |
|
show_confetti() |
|
st.markdown('<div class="final-reveal">🎉 I think it\'s...</div>', unsafe_allow_html=True) |
|
time.sleep(1) |
|
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{guess}</div>', |
|
unsafe_allow_html=True) |
|
else: |
|
st.error("Sorry, I couldn't make a guess. Let's try again!") |
|
|
|
if st.button("Play Again", type="primary"): |
|
st.session_state.clear() |
|
st.rerun() |
|
|
|
if __name__ == "__main__": |
|
main() |