File size: 7,608 Bytes
1292ed1 b922996 1292ed1 b922996 1292ed1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
import streamlit as st
from transformers import pipeline
import streamlit as st
from transformers import pipeline
import os
# Disable parallelism to avoid warnings
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# Initialize the game
def init_game():
if 'questions_asked' not in st.session_state:
st.session_state.questions_asked = 0
st.session_state.answers = []
st.session_state.game_over = False
st.session_state.current_question = "Is it a living thing?"
# Load a smaller model with caching
@st.cache_resource(show_spinner="Loading game engine...")
def load_model():
try:
return pipeline(
"text-generation",
model="distilgpt2", # Smaller than GPT-2
device=-1, # Force CPU
framework="pt", # Explicitly use PyTorch
torch_dtype="auto"
)
except Exception as e:
st.error(f"Failed to load model: {str(e)}")
return None
def generate_question(model, previous_answers):
if not previous_answers:
return "Is it a living thing?"
prompt = """We're playing a guessing game. Here are the previous Q&A:
"""
for i, (q, a) in enumerate(previous_answers, 1):
prompt += f"{i}. Q: {q} A: {'Yes' if a else 'No'}\n"
prompt += "\nWhat should be the next yes/no question to narrow down the possible options?\nQ:"
try:
response = model(
prompt,
max_new_tokens=30, # Shorter response
do_sample=True,
temperature=0.7,
top_p=0.9
)
question = response[0]['generated_text'].split("Q:")[-1].strip()
question = question.split("\n")[0].split("?")[0] + "?" if "?" not in question else question.split("?")[0] + "?"
return question
except Exception as e:
st.error(f"Error generating question: {str(e)}")
return "Is it something you can hold in your hand?"
def main():
st.title("KASOTI - The Guessing Game")
# Initialize with loading state
with st.spinner("Setting up the game..."):
model = load_model()
if model is None:
st.error("Failed to initialize the game. Please refresh the page.")
return
init_game()
st.header("Think of a famous person, place, or object")
st.write(f"Questions asked: {st.session_state.questions_asked}/20")
if not st.session_state.game_over:
st.subheader(st.session_state.current_question)
col1, col2, col3 = st.columns(3)
with col1:
if st.button("Yes"):
st.session_state.answers.append((st.session_state.current_question, True))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
with col2:
if st.button("No"):
st.session_state.answers.append((st.session_state.current_question, False))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
with col3:
if st.button("I don't know"):
st.session_state.answers.append((st.session_state.current_question, None))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
if st.session_state.questions_asked >= 20:
st.session_state.game_over = True
if st.session_state.game_over:
st.subheader("Game Over!")
st.write("I've run out of questions. What were you thinking of?")
user_input = st.text_input("Enter what you were thinking of:")
if user_input:
st.write(f"Ah! I was thinking of {user_input}. Let's play again!")
st.session_state.clear()
st.rerun()
if st.button("Play Again"):
st.session_state.clear()
st.rerun()
if __name__ == "__main__":
main()
# Initialize the game
def init_game():
if 'questions_asked' not in st.session_state:
st.session_state.questions_asked = 0
st.session_state.answers = []
st.session_state.game_over = False
st.session_state.current_question = "Is it a living thing?"
# Load the LLM model
@st.cache_resource
def load_model():
return pipeline("text-generation", model="gpt2")
def generate_question(model, previous_answers):
if not previous_answers:
return "Is it a living thing?"
# Create a prompt for the LLM based on previous answers
prompt = "We're playing a guessing game. Here are the previous Q&A:\n"
for i, (q, a) in enumerate(previous_answers, 1):
prompt += f"{i}. Q: {q} A: {'Yes' if a else 'No'}\n"
prompt += "What should be the next yes/no question to narrow down the possible options?\nQ:"
# Generate the next question
response = model(prompt, max_length=100, num_return_sequences=1)
question = response[0]['generated_text'].split("Q:")[-1].strip()
# Clean up the question (remove anything after newlines or multiple questions)
question = question.split("\n")[0].split("?")[0] + "?" if "?" not in question else question.split("?")[0] + "?"
return question
def main():
st.title("KASOTI - The Guessing Game")
# Initialize the model and game state
model = load_model()
init_game()
# Display game header
st.header("Think of a famous person, place, or object")
st.write(f"Questions asked: {st.session_state.questions_asked}/20")
# Display the current question
if not st.session_state.game_over:
st.subheader(st.session_state.current_question)
# Answer buttons
col1, col2, col3 = st.columns(3)
with col1:
if st.button("Yes"):
st.session_state.answers.append((st.session_state.current_question, True))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
with col2:
if st.button("No"):
st.session_state.answers.append((st.session_state.current_question, False))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
with col3:
if st.button("I don't know"):
st.session_state.answers.append((st.session_state.current_question, None))
st.session_state.questions_asked += 1
st.session_state.current_question = generate_question(model, st.session_state.answers)
st.rerun()
# Check if game is over
if st.session_state.questions_asked >= 20:
st.session_state.game_over = True
# Game over state
if st.session_state.game_over:
st.subheader("Game Over!")
st.write("I've run out of questions. What were you thinking of?")
user_input = st.text_input("Enter what you were thinking of:")
if user_input:
st.write(f"Ah! I was thinking of {user_input}. Let's play again!")
st.session_state.clear()
st.rerun()
if st.button("Play Again"):
st.session_state.clear()
st.rerun()
if __name__ == "__main__":
main() |