Update app.py
Browse files
app.py
CHANGED
@@ -59,20 +59,6 @@ def inject_custom_css():
|
|
59 |
pointer-events: none;
|
60 |
z-index: 1000;
|
61 |
}
|
62 |
-
|
63 |
-
.loading {
|
64 |
-
display: inline-block;
|
65 |
-
width: 20px;
|
66 |
-
height: 20px;
|
67 |
-
border: 3px solid rgba(108,99,255,.3);
|
68 |
-
border-radius: 50%;
|
69 |
-
border-top-color: #6C63FF;
|
70 |
-
animation: spin 1s ease-in-out infinite;
|
71 |
-
}
|
72 |
-
|
73 |
-
@keyframes spin {
|
74 |
-
to { transform: rotate(360deg); }
|
75 |
-
}
|
76 |
</style>
|
77 |
""", unsafe_allow_html=True)
|
78 |
|
@@ -99,9 +85,9 @@ def ask_llama(messages, category, is_final=False):
|
|
99 |
"Content-Type": "application/json"
|
100 |
}
|
101 |
|
102 |
-
prompt = ("You're playing 20 questions to guess a " +
|
103 |
-
|
104 |
-
|
105 |
|
106 |
data = {
|
107 |
"model": "llama-3.3-70b-versatile",
|
@@ -129,9 +115,10 @@ def main():
|
|
129 |
st.session_state.game = {
|
130 |
'state': 'start',
|
131 |
'category': None,
|
132 |
-
'
|
133 |
-
'
|
134 |
-
'
|
|
|
135 |
}
|
136 |
|
137 |
# Start Screen
|
@@ -141,11 +128,6 @@ def main():
|
|
141 |
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
142 |
<p>Think of something and I'll try to guess it with yes/no questions!</p>
|
143 |
<p>Choose a category:</p>
|
144 |
-
<ul>
|
145 |
-
<li><strong>person</strong> (celebrity, fictional character)</li>
|
146 |
-
<li><strong>place</strong> (city, country, location)</li>
|
147 |
-
<li><strong>object</strong> (something you can touch)</li>
|
148 |
-
</ul>
|
149 |
</div>
|
150 |
""", unsafe_allow_html=True)
|
151 |
|
@@ -153,56 +135,60 @@ def main():
|
|
153 |
with col1:
|
154 |
if st.button("Person", use_container_width=True):
|
155 |
st.session_state.game['category'] = 'person'
|
156 |
-
st.session_state.game['state'] = '
|
157 |
st.rerun()
|
158 |
with col2:
|
159 |
if st.button("Place", use_container_width=True):
|
160 |
st.session_state.game['category'] = 'place'
|
161 |
-
st.session_state.game['state'] = '
|
162 |
st.rerun()
|
163 |
with col3:
|
164 |
if st.button("Object", use_container_width=True):
|
165 |
st.session_state.game['category'] = 'object'
|
166 |
-
st.session_state.game['state'] = '
|
167 |
st.rerun()
|
168 |
|
169 |
-
#
|
170 |
-
elif st.session_state.game['state'] == '
|
171 |
-
# Generate
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
|
|
|
|
185 |
|
186 |
-
|
187 |
-
|
188 |
st.markdown(f"""
|
189 |
<div class="question-box">
|
190 |
-
Question {
|
191 |
-
<strong>{st.session_state.game['
|
192 |
</div>
|
193 |
""", unsafe_allow_html=True)
|
194 |
|
195 |
-
# Answer form
|
196 |
with st.form("answer_form"):
|
197 |
-
answer = st.radio("Your answer:", ["Yes", "No"], horizontal=True)
|
198 |
-
|
|
|
|
|
199 |
# Record answer
|
200 |
answer_lower = answer.lower()
|
201 |
-
st.session_state.game['answers'].append(answer_lower)
|
202 |
st.session_state.game['conversation'].append({"role": "user", "content": answer_lower})
|
203 |
-
|
204 |
-
|
205 |
-
if
|
|
|
206 |
ready = ask_llama(
|
207 |
st.session_state.game['conversation'] + [
|
208 |
{"role": "user", "content": "Can you guess now? Answer only 'yes' or 'no'."}
|
@@ -212,19 +198,11 @@ def main():
|
|
212 |
if ready and ready.strip().lower() == 'yes':
|
213 |
st.session_state.game['state'] = 'result'
|
214 |
st.rerun()
|
215 |
-
|
216 |
-
#
|
217 |
-
if
|
218 |
-
|
219 |
-
|
220 |
-
st.session_state.game['category']
|
221 |
-
)
|
222 |
-
if next_q:
|
223 |
-
st.session_state.game['questions'].append(next_q)
|
224 |
-
st.session_state.game['conversation'].append({"role": "assistant", "content": next_q})
|
225 |
-
st.rerun()
|
226 |
-
else:
|
227 |
-
st.error("Failed to generate next question")
|
228 |
else:
|
229 |
st.session_state.game['state'] = 'result'
|
230 |
st.rerun()
|
|
|
59 |
pointer-events: none;
|
60 |
z-index: 1000;
|
61 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
</style>
|
63 |
""", unsafe_allow_html=True)
|
64 |
|
|
|
85 |
"Content-Type": "application/json"
|
86 |
}
|
87 |
|
88 |
+
prompt = (f"You're playing 20 questions to guess a {category}. " +
|
89 |
+
"Ask strategic yes/no questions one at a time." +
|
90 |
+
(" Based on the answers, what is your final guess? State only the guess." if is_final else ""))
|
91 |
|
92 |
data = {
|
93 |
"model": "llama-3.3-70b-versatile",
|
|
|
115 |
st.session_state.game = {
|
116 |
'state': 'start',
|
117 |
'category': None,
|
118 |
+
'current_question': None,
|
119 |
+
'conversation': [],
|
120 |
+
'question_count': 0,
|
121 |
+
'waiting_for_answer': False
|
122 |
}
|
123 |
|
124 |
# Start Screen
|
|
|
128 |
<h3>Welcome to <span style='color:#6C63FF;'>KASOTI 🎯</span></h3>
|
129 |
<p>Think of something and I'll try to guess it with yes/no questions!</p>
|
130 |
<p>Choose a category:</p>
|
|
|
|
|
|
|
|
|
|
|
131 |
</div>
|
132 |
""", unsafe_allow_html=True)
|
133 |
|
|
|
135 |
with col1:
|
136 |
if st.button("Person", use_container_width=True):
|
137 |
st.session_state.game['category'] = 'person'
|
138 |
+
st.session_state.game['state'] = 'generate_question'
|
139 |
st.rerun()
|
140 |
with col2:
|
141 |
if st.button("Place", use_container_width=True):
|
142 |
st.session_state.game['category'] = 'place'
|
143 |
+
st.session_state.game['state'] = 'generate_question'
|
144 |
st.rerun()
|
145 |
with col3:
|
146 |
if st.button("Object", use_container_width=True):
|
147 |
st.session_state.game['category'] = 'object'
|
148 |
+
st.session_state.game['state'] = 'generate_question'
|
149 |
st.rerun()
|
150 |
|
151 |
+
# Generate Question State
|
152 |
+
elif st.session_state.game['state'] == 'generate_question':
|
153 |
+
# Generate new question
|
154 |
+
question = ask_llama(
|
155 |
+
st.session_state.game['conversation'],
|
156 |
+
st.session_state.game['category']
|
157 |
+
)
|
158 |
+
|
159 |
+
if question:
|
160 |
+
st.session_state.game['current_question'] = question
|
161 |
+
st.session_state.game['conversation'].append({"role": "assistant", "content": question})
|
162 |
+
st.session_state.game['question_count'] += 1
|
163 |
+
st.session_state.game['state'] = 'wait_for_answer'
|
164 |
+
st.session_state.game['waiting_for_answer'] = True
|
165 |
+
st.rerun()
|
166 |
+
else:
|
167 |
+
st.error("Failed to generate question. Please try again.")
|
168 |
+
st.session_state.game['state'] = 'start'
|
169 |
+
st.rerun()
|
170 |
|
171 |
+
# Wait for Answer State
|
172 |
+
elif st.session_state.game['state'] == 'wait_for_answer':
|
173 |
st.markdown(f"""
|
174 |
<div class="question-box">
|
175 |
+
Question {st.session_state.game['question_count']}:<br><br>
|
176 |
+
<strong>{st.session_state.game['current_question']}</strong>
|
177 |
</div>
|
178 |
""", unsafe_allow_html=True)
|
179 |
|
|
|
180 |
with st.form("answer_form"):
|
181 |
+
answer = st.radio("Your answer:", ["Yes", "No"], index=None, horizontal=True)
|
182 |
+
submitted = st.form_submit_button("Submit")
|
183 |
+
|
184 |
+
if submitted and answer:
|
185 |
# Record answer
|
186 |
answer_lower = answer.lower()
|
|
|
187 |
st.session_state.game['conversation'].append({"role": "user", "content": answer_lower})
|
188 |
+
st.session_state.game['waiting_for_answer'] = False
|
189 |
+
|
190 |
+
# Check if we should guess or ask another question
|
191 |
+
if st.session_state.game['question_count'] >= 5: # Minimum 5 questions before checking
|
192 |
ready = ask_llama(
|
193 |
st.session_state.game['conversation'] + [
|
194 |
{"role": "user", "content": "Can you guess now? Answer only 'yes' or 'no'."}
|
|
|
198 |
if ready and ready.strip().lower() == 'yes':
|
199 |
st.session_state.game['state'] = 'result'
|
200 |
st.rerun()
|
201 |
+
|
202 |
+
# Continue with next question if under 20
|
203 |
+
if st.session_state.game['question_count'] < 20:
|
204 |
+
st.session_state.game['state'] = 'generate_question'
|
205 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
else:
|
207 |
st.session_state.game['state'] = 'result'
|
208 |
st.rerun()
|