File size: 7,882 Bytes
1292ed1 74a6ec1 8622a36 74a6ec1 b922996 6f37b53 74a6ec1 bad3bd8 74a6ec1 1292ed1 6f37b53 74a6ec1 bad3bd8 74a6ec1 bad3bd8 25f0c4f 74a6ec1 1292ed1 6f37b53 25f0c4f 98a3918 8622a36 98a3918 8622a36 6f37b53 8622a36 6f37b53 25f0c4f 8622a36 6f37b53 8622a36 25f0c4f 6f37b53 25f0c4f 8622a36 25f0c4f 8622a36 6f37b53 1292ed1 74a6ec1 4db3cf5 6f37b53 25f0c4f 6f37b53 6116725 6f37b53 25f0c4f 6f37b53 12df84a 25f0c4f 6f37b53 12df84a 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 12df84a 6f37b53 4db3cf5 25f0c4f 6f37b53 25f0c4f 274fa28 6f37b53 6116725 6f37b53 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 98a3918 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 6f37b53 25f0c4f 1292ed1 6f37b53 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
import streamlit as st
import time
import requests
from streamlit.components.v1 import html
# Inject custom CSS
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;
}
</style>
""", unsafe_allow_html=True)
# Show confetti effect
def show_confetti():
html("""
<canvas id="confetti-canvas" class="confetti"></canvas>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.5.1/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>
""")
# Call Groq API (LLama model)
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-70b-8192",
"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=15)
response.raise_for_status()
return response.json()["choices"][0]["message"]["content"]
except Exception as e:
st.error(f"API Error: {str(e)}")
return None
# Main Game Logic
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)
# Session State
if 'game' not in st.session_state:
st.session_state.game = {
'state': 'start',
'category': None,
'questions': [],
'conversation': [],
'input_buffer': ""
}
game = st.session_state.game
# Start screen
if 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):
game['category'] = 'person'
game['state'] = 'playing'
st.rerun()
with col2:
if st.button("Place", use_container_width=True):
game['category'] = 'place'
game['state'] = 'playing'
st.rerun()
with col3:
if st.button("Object", use_container_width=True):
game['category'] = 'object'
game['state'] = 'playing'
st.rerun()
# Game play screen
elif game['state'] == 'playing':
if not game['questions']:
first_q = ask_llama([{"role": "user", "content": "Ask your first yes/no question."}], game['category'])
if first_q:
game['questions'].append(first_q)
game['conversation'].append({"role": "assistant", "content": first_q})
st.rerun()
else:
st.error("Failed to generate question.")
game['state'] = 'start'
st.rerun()
current_q_index = len(game['questions']) - 1
current_q = game['questions'][current_q_index]
st.markdown(f"""
<div class="question-box">
Question {current_q_index + 1}:<br><br>
<strong>{current_q}</strong>
</div>
""", unsafe_allow_html=True)
with st.form("answer_form"):
user_input = st.text_input("Your answer (Yes, No, or more detailed response):")
submitted = st.form_submit_button("Submit")
if submitted and user_input.strip() != "":
game['conversation'].append({"role": "user", "content": user_input.strip()})
# Ask if ready to guess after 5 questions
if current_q_index >= 4:
can_guess = ask_llama(
game['conversation'] + [{"role": "user", "content": "Can you guess now? Answer only yes or no."}],
game['category']
)
if can_guess and can_guess.strip().lower() == 'yes':
game['state'] = 'result'
st.rerun()
# Generate next question
if current_q_index < 19:
next_q = ask_llama(game['conversation'], game['category'])
if next_q:
game['questions'].append(next_q)
game['conversation'].append({"role": "assistant", "content": next_q})
st.rerun()
else:
st.error("Couldn't get next question.")
else:
game['state'] = 'result'
st.rerun()
# Result Screen
elif game['state'] == 'result':
final_guess = ask_llama(game['conversation'], game['category'], is_final=True)
if final_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;">{final_guess}</div>', unsafe_allow_html=True)
else:
st.error("Sorry, I couldn't make a guess.")
if st.button("Play Again", type="primary"):
st.session_state.clear()
st.rerun()
if __name__ == "__main__":
main()
|