Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
from streamlit.components.v1 import html
|
| 4 |
|
| 5 |
# Custom CSS for professional look
|
|
@@ -99,7 +101,37 @@ def show_confetti():
|
|
| 99 |
</script>
|
| 100 |
""")
|
| 101 |
|
| 102 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def main():
|
| 104 |
inject_custom_css()
|
| 105 |
|
|
@@ -108,28 +140,18 @@ def main():
|
|
| 108 |
|
| 109 |
if 'game_state' not in st.session_state:
|
| 110 |
st.session_state.game_state = "start"
|
| 111 |
-
st.session_state.questions = [
|
| 112 |
-
"Is it something you can hold in your hand?",
|
| 113 |
-
"Is it commonly found in households?",
|
| 114 |
-
"Can it be used as a tool?",
|
| 115 |
-
"Is it primarily made of metal?",
|
| 116 |
-
"Does it have moving parts?",
|
| 117 |
-
"Is it electronic?",
|
| 118 |
-
"Is it something you would gift to someone?",
|
| 119 |
-
"Can it be found in an office?",
|
| 120 |
-
"Does it require electricity to function?",
|
| 121 |
-
"Is it typically under $100?"
|
| 122 |
-
]
|
| 123 |
st.session_state.current_q = 0
|
| 124 |
st.session_state.answers = []
|
| 125 |
-
st.session_state.
|
|
|
|
| 126 |
|
| 127 |
# Start screen
|
| 128 |
if st.session_state.game_state == "start":
|
| 129 |
st.markdown("""
|
| 130 |
<div class="question-box">
|
| 131 |
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
| 132 |
-
<p>This is a fun guessing game! You
|
| 133 |
<p>You can choose from three categories:</p>
|
| 134 |
<ul>
|
| 135 |
<li><strong>person</strong> – like a celebrity, fictional character, etc.</li>
|
|
@@ -145,40 +167,73 @@ def main():
|
|
| 145 |
|
| 146 |
if st.form_submit_button("Start Game"):
|
| 147 |
if not category_input:
|
| 148 |
-
st.error("Please
|
| 149 |
elif category_input not in ["person", "place", "object"]:
|
| 150 |
-
st.error("
|
| 151 |
else:
|
| 152 |
st.session_state.category = category_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
st.session_state.game_state = "gameplay"
|
| 154 |
st.rerun()
|
| 155 |
|
| 156 |
# Gameplay screen
|
| 157 |
elif st.session_state.game_state == "gameplay":
|
| 158 |
-
|
| 159 |
-
|
|
|
|
| 160 |
unsafe_allow_html=True)
|
| 161 |
|
| 162 |
with st.form("answer_form"):
|
| 163 |
-
answer_input = st.text_input("
|
| 164 |
|
| 165 |
-
if st.form_submit_button("Submit
|
| 166 |
if answer_input not in ["yes", "no"]:
|
| 167 |
-
st.error("Please
|
| 168 |
else:
|
| 169 |
-
|
| 170 |
-
st.session_state.
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
st.session_state.game_state = "result"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
st.rerun()
|
| 175 |
|
| 176 |
# Result screen
|
| 177 |
elif st.session_state.game_state == "result":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
show_confetti()
|
| 179 |
-
st.markdown('<div class="final-reveal">🎉 I think it\'s
|
| 180 |
time.sleep(1)
|
| 181 |
-
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{
|
| 182 |
unsafe_allow_html=True)
|
| 183 |
|
| 184 |
if st.button("Play Again", key="play_again"):
|
|
@@ -186,5 +241,4 @@ def main():
|
|
| 186 |
st.rerun()
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
-
main()
|
| 190 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import time
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
from streamlit.components.v1 import html
|
| 6 |
|
| 7 |
# Custom CSS for professional look
|
|
|
|
| 101 |
</script>
|
| 102 |
""")
|
| 103 |
|
| 104 |
+
# Mistral AI API call
|
| 105 |
+
def ask_mistral(conversation_history, category):
|
| 106 |
+
api_url = "https://api.mistral.ai/v1/chat/completions"
|
| 107 |
+
headers = {
|
| 108 |
+
"Authorization": "Bearer gz6lDXokxgR6cLY72oomALWcm7vhjzQ",
|
| 109 |
+
"Content-Type": "application/json"
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
messages = [
|
| 113 |
+
{
|
| 114 |
+
"role": "system",
|
| 115 |
+
"content": f"You're playing 20 questions to guess a {category}. Ask strategic yes/no questions."
|
| 116 |
+
}
|
| 117 |
+
] + conversation_history
|
| 118 |
+
|
| 119 |
+
data = {
|
| 120 |
+
"model": "mistral-tiny",
|
| 121 |
+
"messages": messages,
|
| 122 |
+
"temperature": 0.7,
|
| 123 |
+
"max_tokens": 100
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
response = requests.post(api_url, headers=headers, json=data)
|
| 128 |
+
response.raise_for_status()
|
| 129 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 130 |
+
except Exception as e:
|
| 131 |
+
st.error(f"Error calling Mistral API: {str(e)}")
|
| 132 |
+
return "Could not generate question"
|
| 133 |
+
|
| 134 |
+
# Game logic
|
| 135 |
def main():
|
| 136 |
inject_custom_css()
|
| 137 |
|
|
|
|
| 140 |
|
| 141 |
if 'game_state' not in st.session_state:
|
| 142 |
st.session_state.game_state = "start"
|
| 143 |
+
st.session_state.questions = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
st.session_state.current_q = 0
|
| 145 |
st.session_state.answers = []
|
| 146 |
+
st.session_state.conversation_history = []
|
| 147 |
+
st.session_state.category = None
|
| 148 |
|
| 149 |
# Start screen
|
| 150 |
if st.session_state.game_state == "start":
|
| 151 |
st.markdown("""
|
| 152 |
<div class="question-box">
|
| 153 |
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
| 154 |
+
<p>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.</p>
|
| 155 |
<p>You can choose from three categories:</p>
|
| 156 |
<ul>
|
| 157 |
<li><strong>person</strong> – like a celebrity, fictional character, etc.</li>
|
|
|
|
| 167 |
|
| 168 |
if st.form_submit_button("Start Game"):
|
| 169 |
if not category_input:
|
| 170 |
+
st.error("Please enter a category!")
|
| 171 |
elif category_input not in ["person", "place", "object"]:
|
| 172 |
+
st.error("Please enter either 'person', 'place', or 'object'!")
|
| 173 |
else:
|
| 174 |
st.session_state.category = category_input
|
| 175 |
+
# Generate first question
|
| 176 |
+
first_question = ask_mistral([
|
| 177 |
+
{"role": "user", "content": "Ask your first yes/no question."}
|
| 178 |
+
], category_input)
|
| 179 |
+
st.session_state.questions = [first_question]
|
| 180 |
+
st.session_state.conversation_history = [
|
| 181 |
+
{"role": "assistant", "content": first_question}
|
| 182 |
+
]
|
| 183 |
st.session_state.game_state = "gameplay"
|
| 184 |
st.rerun()
|
| 185 |
|
| 186 |
# Gameplay screen
|
| 187 |
elif st.session_state.game_state == "gameplay":
|
| 188 |
+
current_question = st.session_state.questions[st.session_state.current_q]
|
| 189 |
+
st.markdown(f'<div class="question-box">Question {st.session_state.current_q + 1}/20:<br><br>'
|
| 190 |
+
f'<strong>{current_question}</strong></div>',
|
| 191 |
unsafe_allow_html=True)
|
| 192 |
|
| 193 |
with st.form("answer_form"):
|
| 194 |
+
answer_input = st.text_input("Your answer (yes/no):").strip().lower()
|
| 195 |
|
| 196 |
+
if st.form_submit_button("Submit"):
|
| 197 |
if answer_input not in ["yes", "no"]:
|
| 198 |
+
st.error("Please answer with 'yes' or 'no'!")
|
| 199 |
else:
|
| 200 |
+
# Record answer
|
| 201 |
+
st.session_state.answers.append(answer_input)
|
| 202 |
+
st.session_state.conversation_history.append(
|
| 203 |
+
{"role": "user", "content": answer_input}
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
# Check if we've reached max questions
|
| 207 |
+
if st.session_state.current_q >= 19: # 0-based index
|
| 208 |
st.session_state.game_state = "result"
|
| 209 |
+
else:
|
| 210 |
+
# Generate next question
|
| 211 |
+
next_question = ask_mistral(
|
| 212 |
+
st.session_state.conversation_history,
|
| 213 |
+
st.session_state.category
|
| 214 |
+
)
|
| 215 |
+
st.session_state.questions.append(next_question)
|
| 216 |
+
st.session_state.conversation_history.append(
|
| 217 |
+
{"role": "assistant", "content": next_question}
|
| 218 |
+
)
|
| 219 |
+
st.session_state.current_q += 1
|
| 220 |
+
|
| 221 |
st.rerun()
|
| 222 |
|
| 223 |
# Result screen
|
| 224 |
elif st.session_state.game_state == "result":
|
| 225 |
+
# Generate final guess
|
| 226 |
+
final_guess = ask_mistral(
|
| 227 |
+
st.session_state.conversation_history + [
|
| 228 |
+
{"role": "user", "content": "Based on all my answers, what is your final guess? Just state the guess directly."}
|
| 229 |
+
],
|
| 230 |
+
st.session_state.category
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
show_confetti()
|
| 234 |
+
st.markdown('<div class="final-reveal">🎉 I think it\'s...</div>', unsafe_allow_html=True)
|
| 235 |
time.sleep(1)
|
| 236 |
+
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{final_guess}</div>',
|
| 237 |
unsafe_allow_html=True)
|
| 238 |
|
| 239 |
if st.button("Play Again", key="play_again"):
|
|
|
|
| 241 |
st.rerun()
|
| 242 |
|
| 243 |
if __name__ == "__main__":
|
| 244 |
+
main()
|
|
|