Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize the game
|
| 5 |
+
def init_game():
|
| 6 |
+
if 'questions_asked' not in st.session_state:
|
| 7 |
+
st.session_state.questions_asked = 0
|
| 8 |
+
st.session_state.answers = []
|
| 9 |
+
st.session_state.game_over = False
|
| 10 |
+
st.session_state.current_question = "Is it a living thing?"
|
| 11 |
+
|
| 12 |
+
# Load the LLM model
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
return pipeline("text-generation", model="gpt2")
|
| 16 |
+
|
| 17 |
+
def generate_question(model, previous_answers):
|
| 18 |
+
if not previous_answers:
|
| 19 |
+
return "Is it a living thing?"
|
| 20 |
+
|
| 21 |
+
# Create a prompt for the LLM based on previous answers
|
| 22 |
+
prompt = "We're playing a guessing game. Here are the previous Q&A:\n"
|
| 23 |
+
for i, (q, a) in enumerate(previous_answers, 1):
|
| 24 |
+
prompt += f"{i}. Q: {q} A: {'Yes' if a else 'No'}\n"
|
| 25 |
+
prompt += "What should be the next yes/no question to narrow down the possible options?\nQ:"
|
| 26 |
+
|
| 27 |
+
# Generate the next question
|
| 28 |
+
response = model(prompt, max_length=100, num_return_sequences=1)
|
| 29 |
+
question = response[0]['generated_text'].split("Q:")[-1].strip()
|
| 30 |
+
|
| 31 |
+
# Clean up the question (remove anything after newlines or multiple questions)
|
| 32 |
+
question = question.split("\n")[0].split("?")[0] + "?" if "?" not in question else question.split("?")[0] + "?"
|
| 33 |
+
return question
|
| 34 |
+
|
| 35 |
+
def main():
|
| 36 |
+
st.title("KASOTI - The Guessing Game")
|
| 37 |
+
|
| 38 |
+
# Initialize the model and game state
|
| 39 |
+
model = load_model()
|
| 40 |
+
init_game()
|
| 41 |
+
|
| 42 |
+
# Display game header
|
| 43 |
+
st.header("Think of a famous person, place, or object")
|
| 44 |
+
st.write(f"Questions asked: {st.session_state.questions_asked}/20")
|
| 45 |
+
|
| 46 |
+
# Display the current question
|
| 47 |
+
if not st.session_state.game_over:
|
| 48 |
+
st.subheader(st.session_state.current_question)
|
| 49 |
+
|
| 50 |
+
# Answer buttons
|
| 51 |
+
col1, col2, col3 = st.columns(3)
|
| 52 |
+
with col1:
|
| 53 |
+
if st.button("Yes"):
|
| 54 |
+
st.session_state.answers.append((st.session_state.current_question, True))
|
| 55 |
+
st.session_state.questions_asked += 1
|
| 56 |
+
st.session_state.current_question = generate_question(model, st.session_state.answers)
|
| 57 |
+
st.rerun()
|
| 58 |
+
with col2:
|
| 59 |
+
if st.button("No"):
|
| 60 |
+
st.session_state.answers.append((st.session_state.current_question, False))
|
| 61 |
+
st.session_state.questions_asked += 1
|
| 62 |
+
st.session_state.current_question = generate_question(model, st.session_state.answers)
|
| 63 |
+
st.rerun()
|
| 64 |
+
with col3:
|
| 65 |
+
if st.button("I don't know"):
|
| 66 |
+
st.session_state.answers.append((st.session_state.current_question, None))
|
| 67 |
+
st.session_state.questions_asked += 1
|
| 68 |
+
st.session_state.current_question = generate_question(model, st.session_state.answers)
|
| 69 |
+
st.rerun()
|
| 70 |
+
|
| 71 |
+
# Check if game is over
|
| 72 |
+
if st.session_state.questions_asked >= 20:
|
| 73 |
+
st.session_state.game_over = True
|
| 74 |
+
|
| 75 |
+
# Game over state
|
| 76 |
+
if st.session_state.game_over:
|
| 77 |
+
st.subheader("Game Over!")
|
| 78 |
+
st.write("I've run out of questions. What were you thinking of?")
|
| 79 |
+
user_input = st.text_input("Enter what you were thinking of:")
|
| 80 |
+
if user_input:
|
| 81 |
+
st.write(f"Ah! I was thinking of {user_input}. Let's play again!")
|
| 82 |
+
st.session_state.clear()
|
| 83 |
+
st.rerun()
|
| 84 |
+
|
| 85 |
+
if st.button("Play Again"):
|
| 86 |
+
st.session_state.clear()
|
| 87 |
+
st.rerun()
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
main()
|