iisadia commited on
Commit
b922996
·
verified ·
1 Parent(s): 8857a57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py CHANGED
@@ -1,6 +1,117 @@
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:
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import streamlit as st
4
+ from transformers import pipeline
5
+ import os
6
+
7
+ # Disable parallelism to avoid warnings
8
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
9
+
10
+ # Initialize the game
11
+ def init_game():
12
+ if 'questions_asked' not in st.session_state:
13
+ st.session_state.questions_asked = 0
14
+ st.session_state.answers = []
15
+ st.session_state.game_over = False
16
+ st.session_state.current_question = "Is it a living thing?"
17
+
18
+ # Load a smaller model with caching
19
+ @st.cache_resource(show_spinner="Loading game engine...")
20
+ def load_model():
21
+ try:
22
+ return pipeline(
23
+ "text-generation",
24
+ model="distilgpt2", # Smaller than GPT-2
25
+ device=-1, # Force CPU
26
+ framework="pt", # Explicitly use PyTorch
27
+ torch_dtype="auto"
28
+ )
29
+ except Exception as e:
30
+ st.error(f"Failed to load model: {str(e)}")
31
+ return None
32
+
33
+ def generate_question(model, previous_answers):
34
+ if not previous_answers:
35
+ return "Is it a living thing?"
36
+
37
+ prompt = """We're playing a guessing game. Here are the previous Q&A:
38
+ """
39
+ for i, (q, a) in enumerate(previous_answers, 1):
40
+ prompt += f"{i}. Q: {q} A: {'Yes' if a else 'No'}\n"
41
+ prompt += "\nWhat should be the next yes/no question to narrow down the possible options?\nQ:"
42
+
43
+ try:
44
+ response = model(
45
+ prompt,
46
+ max_new_tokens=30, # Shorter response
47
+ do_sample=True,
48
+ temperature=0.7,
49
+ top_p=0.9
50
+ )
51
+ question = response[0]['generated_text'].split("Q:")[-1].strip()
52
+ question = question.split("\n")[0].split("?")[0] + "?" if "?" not in question else question.split("?")[0] + "?"
53
+ return question
54
+ except Exception as e:
55
+ st.error(f"Error generating question: {str(e)}")
56
+ return "Is it something you can hold in your hand?"
57
 
58
+ def main():
59
+ st.title("KASOTI - The Guessing Game")
60
+
61
+ # Initialize with loading state
62
+ with st.spinner("Setting up the game..."):
63
+ model = load_model()
64
+
65
+ if model is None:
66
+ st.error("Failed to initialize the game. Please refresh the page.")
67
+ return
68
+
69
+ init_game()
70
+
71
+ st.header("Think of a famous person, place, or object")
72
+ st.write(f"Questions asked: {st.session_state.questions_asked}/20")
73
+
74
+ if not st.session_state.game_over:
75
+ st.subheader(st.session_state.current_question)
76
+
77
+ col1, col2, col3 = st.columns(3)
78
+ with col1:
79
+ if st.button("Yes"):
80
+ st.session_state.answers.append((st.session_state.current_question, True))
81
+ st.session_state.questions_asked += 1
82
+ st.session_state.current_question = generate_question(model, st.session_state.answers)
83
+ st.rerun()
84
+ with col2:
85
+ if st.button("No"):
86
+ st.session_state.answers.append((st.session_state.current_question, False))
87
+ st.session_state.questions_asked += 1
88
+ st.session_state.current_question = generate_question(model, st.session_state.answers)
89
+ st.rerun()
90
+ with col3:
91
+ if st.button("I don't know"):
92
+ st.session_state.answers.append((st.session_state.current_question, None))
93
+ st.session_state.questions_asked += 1
94
+ st.session_state.current_question = generate_question(model, st.session_state.answers)
95
+ st.rerun()
96
+
97
+ if st.session_state.questions_asked >= 20:
98
+ st.session_state.game_over = True
99
+
100
+ if st.session_state.game_over:
101
+ st.subheader("Game Over!")
102
+ st.write("I've run out of questions. What were you thinking of?")
103
+ user_input = st.text_input("Enter what you were thinking of:")
104
+ if user_input:
105
+ st.write(f"Ah! I was thinking of {user_input}. Let's play again!")
106
+ st.session_state.clear()
107
+ st.rerun()
108
+
109
+ if st.button("Play Again"):
110
+ st.session_state.clear()
111
+ st.rerun()
112
+
113
+ if __name__ == "__main__":
114
+ main()
115
  # Initialize the game
116
  def init_game():
117
  if 'questions_asked' not in st.session_state: