Update app.py
Browse files
app.py
CHANGED
@@ -102,21 +102,21 @@ def show_confetti():
|
|
102 |
|
103 |
# Groq Llama AI API call
|
104 |
def ask_llama(conversation_history, category):
|
105 |
-
api_url = "https://api.groq.com/openai/v1/chat/completions"
|
106 |
headers = {
|
107 |
-
"Authorization": "Bearer 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",
|
108 |
"Content-Type": "application/json"
|
109 |
}
|
110 |
|
111 |
messages = [
|
112 |
{
|
113 |
"role": "system",
|
114 |
-
"content": f"You're playing 20 questions to guess a {category}. Ask strategic yes/no questions."
|
115 |
}
|
116 |
] + conversation_history
|
117 |
|
118 |
data = {
|
119 |
-
"model": "llama-3.3-70b-versatile",
|
120 |
"messages": messages,
|
121 |
"temperature": 0.7,
|
122 |
"max_tokens": 100
|
@@ -144,6 +144,7 @@ def main():
|
|
144 |
st.session_state.answers = []
|
145 |
st.session_state.conversation_history = []
|
146 |
st.session_state.category = None
|
|
|
147 |
|
148 |
# Start screen
|
149 |
if st.session_state.game_state == "start":
|
@@ -181,15 +182,18 @@ def main():
|
|
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 |
|
@@ -202,12 +206,12 @@ def main():
|
|
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:
|
208 |
st.session_state.game_state = "result"
|
209 |
else:
|
210 |
-
# Generate next question
|
211 |
next_question = ask_llama(
|
212 |
st.session_state.conversation_history,
|
213 |
st.session_state.category
|
@@ -217,8 +221,14 @@ def main():
|
|
217 |
{"role": "assistant", "content": next_question}
|
218 |
)
|
219 |
st.session_state.current_q += 1
|
|
|
220 |
st.rerun()
|
221 |
|
|
|
|
|
|
|
|
|
|
|
222 |
# Result screen
|
223 |
elif st.session_state.game_state == "result":
|
224 |
# Generate final guess
|
@@ -235,9 +245,12 @@ def main():
|
|
235 |
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{final_guess}</div>',
|
236 |
unsafe_allow_html=True)
|
237 |
|
|
|
|
|
|
|
238 |
if st.button("Play Again", key="play_again"):
|
239 |
st.session_state.clear()
|
240 |
st.rerun()
|
241 |
|
242 |
if __name__ == "__main__":
|
243 |
-
main()
|
|
|
102 |
|
103 |
# Groq Llama AI API call
|
104 |
def ask_llama(conversation_history, category):
|
105 |
+
api_url = "https://api.groq.com/openai/v1/chat/completions"
|
106 |
headers = {
|
107 |
+
"Authorization": "Bearer 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",
|
108 |
"Content-Type": "application/json"
|
109 |
}
|
110 |
|
111 |
messages = [
|
112 |
{
|
113 |
"role": "system",
|
114 |
+
"content": f"You're playing 20 questions to guess a {category}. Ask strategic yes/no questions one at a time based on previous answers."
|
115 |
}
|
116 |
] + conversation_history
|
117 |
|
118 |
data = {
|
119 |
+
"model": "llama-3.3-70b-versatile",
|
120 |
"messages": messages,
|
121 |
"temperature": 0.7,
|
122 |
"max_tokens": 100
|
|
|
144 |
st.session_state.answers = []
|
145 |
st.session_state.conversation_history = []
|
146 |
st.session_state.category = None
|
147 |
+
st.session_state.waiting_for_answer = False
|
148 |
|
149 |
# Start screen
|
150 |
if st.session_state.game_state == "start":
|
|
|
182 |
{"role": "assistant", "content": first_question}
|
183 |
]
|
184 |
st.session_state.game_state = "gameplay"
|
185 |
+
st.session_state.waiting_for_answer = True
|
186 |
st.rerun()
|
187 |
|
188 |
# Gameplay screen
|
189 |
elif st.session_state.game_state == "gameplay":
|
190 |
+
# Show current question
|
191 |
current_question = st.session_state.questions[st.session_state.current_q]
|
192 |
st.markdown(f'<div class="question-box">Question {st.session_state.current_q + 1}/20:<br><br>'
|
193 |
f'<strong>{current_question}</strong></div>',
|
194 |
unsafe_allow_html=True)
|
195 |
|
196 |
+
# Answer form
|
197 |
with st.form("answer_form"):
|
198 |
answer_input = st.text_input("Your answer (yes/no):").strip().lower()
|
199 |
|
|
|
206 |
st.session_state.conversation_history.append(
|
207 |
{"role": "user", "content": answer_input}
|
208 |
)
|
209 |
+
|
210 |
# Check if we've reached max questions
|
211 |
+
if st.session_state.current_q >= 19:
|
212 |
st.session_state.game_state = "result"
|
213 |
else:
|
214 |
+
# Generate next question based on all previous Q&A
|
215 |
next_question = ask_llama(
|
216 |
st.session_state.conversation_history,
|
217 |
st.session_state.category
|
|
|
221 |
{"role": "assistant", "content": next_question}
|
222 |
)
|
223 |
st.session_state.current_q += 1
|
224 |
+
|
225 |
st.rerun()
|
226 |
|
227 |
+
# Early guess button
|
228 |
+
if st.button("I think I can guess now!"):
|
229 |
+
st.session_state.game_state = "result"
|
230 |
+
st.rerun()
|
231 |
+
|
232 |
# Result screen
|
233 |
elif st.session_state.game_state == "result":
|
234 |
# Generate final guess
|
|
|
245 |
st.markdown(f'<div class="final-reveal" style="font-size:3.5rem;color:#6C63FF;">{final_guess}</div>',
|
246 |
unsafe_allow_html=True)
|
247 |
|
248 |
+
st.markdown("<br><br>", unsafe_allow_html=True)
|
249 |
+
|
250 |
+
# Play again button
|
251 |
if st.button("Play Again", key="play_again"):
|
252 |
st.session_state.clear()
|
253 |
st.rerun()
|
254 |
|
255 |
if __name__ == "__main__":
|
256 |
+
main()
|