iisadia commited on
Commit
6116725
·
verified ·
1 Parent(s): 25f0c4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -68
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 " + category +
103
- ". Ask strategic yes/no questions one at a time." +
104
- (" Based on the answers, what is your final guess? State only the guess." if is_final else ""))
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
- 'questions': [],
133
- 'answers': [],
134
- 'conversation': []
 
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'] = 'playing'
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'] = 'playing'
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'] = 'playing'
167
  st.rerun()
168
 
169
- # Game Play Screen
170
- elif st.session_state.game['state'] == 'playing':
171
- # Generate first question if none exists
172
- if not st.session_state.game['questions']:
173
- question = ask_llama(
174
- [{"role": "user", "content": "Ask your first yes/no question."}],
175
- st.session_state.game['category']
176
- )
177
- if question:
178
- st.session_state.game['questions'].append(question)
179
- st.session_state.game['conversation'].append({"role": "assistant", "content": question})
180
- st.rerun()
181
- else:
182
- st.error("Failed to generate question. Please try again.")
183
- st.session_state.game['state'] = 'start'
184
- st.rerun()
 
 
 
185
 
186
- # Show current question
187
- current_q = len(st.session_state.game['questions']) - 1
188
  st.markdown(f"""
189
  <div class="question-box">
190
- Question {current_q + 1}:<br><br>
191
- <strong>{st.session_state.game['questions'][current_q]}</strong>
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
- if st.form_submit_button("Submit"):
 
 
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
- # Check if AI can guess now
205
- if current_q >= 4: # Minimum 5 questions before checking
 
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
- # Generate next question if under 20
217
- if current_q < 19:
218
- next_q = ask_llama(
219
- st.session_state.game['conversation'],
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()