Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,7 @@ from audio_recorder_streamlit import audio_recorder
|
|
10 |
import torch
|
11 |
from io import BytesIO
|
12 |
import hashlib
|
|
|
13 |
|
14 |
# Load Whisper model (cached)
|
15 |
@st.cache_resource
|
@@ -59,6 +60,271 @@ def voice_input(key, prompt_text, default_text=""):
|
|
59 |
|
60 |
return text_input
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Import transformers and cache the help agent for performance
|
63 |
@st.cache_resource
|
64 |
def get_help_agent():
|
@@ -66,12 +332,7 @@ def get_help_agent():
|
|
66 |
# Using BlenderBot 400M Distill as the public conversational model (used elsewhere)
|
67 |
return pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
68 |
|
69 |
-
#
|
70 |
-
# inject_custom_css()
|
71 |
-
# show_confetti()
|
72 |
-
# ask_llama()
|
73 |
-
# ask_help_agent()
|
74 |
-
|
75 |
def main():
|
76 |
inject_custom_css()
|
77 |
|
@@ -86,9 +347,9 @@ def main():
|
|
86 |
st.session_state.conversation_history = []
|
87 |
st.session_state.category = None
|
88 |
st.session_state.final_guess = None
|
89 |
-
st.session_state.help_conversation = []
|
90 |
|
91 |
-
# Start screen with
|
92 |
if st.session_state.game_state == "start":
|
93 |
with st.container():
|
94 |
st.markdown("""
|
|
|
10 |
import torch
|
11 |
from io import BytesIO
|
12 |
import hashlib
|
13 |
+
from transformers import pipeline
|
14 |
|
15 |
# Load Whisper model (cached)
|
16 |
@st.cache_resource
|
|
|
60 |
|
61 |
return text_input
|
62 |
|
63 |
+
# Enhanced Custom CSS with modern design
|
64 |
+
def inject_custom_css():
|
65 |
+
st.markdown("""
|
66 |
+
<style>
|
67 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
68 |
+
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css');
|
69 |
+
|
70 |
+
* {
|
71 |
+
font-family: 'Inter', sans-serif;
|
72 |
+
}
|
73 |
+
|
74 |
+
body {
|
75 |
+
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
|
76 |
+
}
|
77 |
+
|
78 |
+
.title {
|
79 |
+
font-size: 2.8rem !important;
|
80 |
+
font-weight: 800 !important;
|
81 |
+
background: linear-gradient(45deg, #6C63FF, #3B82F6);
|
82 |
+
-webkit-background-clip: text;
|
83 |
+
-webkit-text-fill-color: transparent;
|
84 |
+
text-align: center;
|
85 |
+
margin: 1rem 0;
|
86 |
+
letter-spacing: -1px;
|
87 |
+
}
|
88 |
+
|
89 |
+
.subtitle {
|
90 |
+
font-size: 1.1rem !important;
|
91 |
+
text-align: center;
|
92 |
+
color: #64748B !important;
|
93 |
+
margin-bottom: 2.5rem;
|
94 |
+
animation: fadeInSlide 1s ease;
|
95 |
+
}
|
96 |
+
|
97 |
+
.question-box {
|
98 |
+
background: white;
|
99 |
+
border-radius: 20px;
|
100 |
+
padding: 2rem;
|
101 |
+
margin: 1.5rem 0;
|
102 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.08);
|
103 |
+
border: 1px solid #e2e8f0;
|
104 |
+
position: relative;
|
105 |
+
transition: transform 0.2s ease;
|
106 |
+
color: black;
|
107 |
+
}
|
108 |
+
|
109 |
+
.question-box:hover {
|
110 |
+
transform: translateY(-3px);
|
111 |
+
}
|
112 |
+
|
113 |
+
.question-box::before {
|
114 |
+
content: "🕹️";
|
115 |
+
position: absolute;
|
116 |
+
left: -15px;
|
117 |
+
top: -15px;
|
118 |
+
background: white;
|
119 |
+
border-radius: 50%;
|
120 |
+
padding: 8px;
|
121 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
122 |
+
font-size: 1.2rem;
|
123 |
+
}
|
124 |
+
|
125 |
+
.input-box {
|
126 |
+
background: white;
|
127 |
+
border-radius: 12px;
|
128 |
+
padding: 1.5rem;
|
129 |
+
margin: 1rem 0;
|
130 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
131 |
+
}
|
132 |
+
|
133 |
+
.stTextInput input {
|
134 |
+
border: 2px solid #e2e8f0 !important;
|
135 |
+
border-radius: 10px !important;
|
136 |
+
padding: 12px 16px !important;
|
137 |
+
transition: all 0.3s ease !important;
|
138 |
+
}
|
139 |
+
|
140 |
+
.stTextInput input:focus {
|
141 |
+
border-color: #6C63FF !important;
|
142 |
+
box-shadow: 0 0 0 3px rgba(108, 99, 255, 0.2) !important;
|
143 |
+
}
|
144 |
+
|
145 |
+
button {
|
146 |
+
background: linear-gradient(45deg, #6C63FF, #3B82F6) !important;
|
147 |
+
color: white !important;
|
148 |
+
border: none !important;
|
149 |
+
border-radius: 10px !important;
|
150 |
+
padding: 12px 24px !important;
|
151 |
+
font-weight: 600 !important;
|
152 |
+
transition: all 0.3s ease !important;
|
153 |
+
}
|
154 |
+
|
155 |
+
button:hover {
|
156 |
+
transform: translateY(-2px);
|
157 |
+
box-shadow: 0 5px 15px rgba(108, 99, 255, 0.3) !important;
|
158 |
+
}
|
159 |
+
|
160 |
+
.final-reveal {
|
161 |
+
animation: fadeInUp 1s ease;
|
162 |
+
font-size: 2.8rem;
|
163 |
+
background: linear-gradient(45deg, #6C63FF, #3B82F6);
|
164 |
+
-webkit-background-clip: text;
|
165 |
+
-webkit-text-fill-color: transparent;
|
166 |
+
text-align: center;
|
167 |
+
margin: 2rem 0;
|
168 |
+
font-weight: 800;
|
169 |
+
}
|
170 |
+
|
171 |
+
.help-chat {
|
172 |
+
background: rgba(255,255,255,0.9);
|
173 |
+
backdrop-filter: blur(10px);
|
174 |
+
border-radius: 15px;
|
175 |
+
padding: 1rem;
|
176 |
+
margin: 1rem 0;
|
177 |
+
box-shadow: 0 8px 30px rgba(0,0,0,0.12);
|
178 |
+
}
|
179 |
+
|
180 |
+
@keyframes fadeInSlide {
|
181 |
+
0% { opacity: 0; transform: translateY(20px); }
|
182 |
+
100% { opacity: 1; transform: translateY(0); }
|
183 |
+
}
|
184 |
+
|
185 |
+
@keyframes fadeInUp {
|
186 |
+
0% { opacity: 0; transform: translateY(30px); }
|
187 |
+
100% { opacity: 1; transform: translateY(0); }
|
188 |
+
}
|
189 |
+
|
190 |
+
.progress-bar {
|
191 |
+
height: 6px;
|
192 |
+
background: #e2e8f0;
|
193 |
+
border-radius: 3px;
|
194 |
+
margin: 1.5rem 0;
|
195 |
+
overflow: hidden;
|
196 |
+
}
|
197 |
+
|
198 |
+
.progress-fill {
|
199 |
+
height: 100%;
|
200 |
+
background: linear-gradient(90deg, #6C63FF, #3B82F6);
|
201 |
+
transition: width 0.5s ease;
|
202 |
+
}
|
203 |
+
|
204 |
+
.question-count {
|
205 |
+
color: #6C63FF;
|
206 |
+
font-weight: 600;
|
207 |
+
font-size: 0.9rem;
|
208 |
+
margin-bottom: 0.5rem;
|
209 |
+
}
|
210 |
+
</style>
|
211 |
+
""", unsafe_allow_html=True)
|
212 |
+
|
213 |
+
# Confetti animation (enhanced)
|
214 |
+
def show_confetti():
|
215 |
+
html("""
|
216 |
+
<canvas id="confetti-canvas" class="confetti"></canvas>
|
217 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/confetti.browser.min.js"></script>
|
218 |
+
<script>
|
219 |
+
const count = 200;
|
220 |
+
const defaults = {
|
221 |
+
origin: { y: 0.7 },
|
222 |
+
zIndex: 1050
|
223 |
+
};
|
224 |
+
|
225 |
+
function fire(particleRatio, opts) {
|
226 |
+
confetti(Object.assign({}, defaults, opts, {
|
227 |
+
particleCount: Math.floor(count * particleRatio)
|
228 |
+
}));
|
229 |
+
}
|
230 |
+
|
231 |
+
fire(0.25, { spread: 26, startVelocity: 55 });
|
232 |
+
fire(0.2, { spread: 60 });
|
233 |
+
fire(0.35, { spread: 100, decay: 0.91, scalar: 0.8 });
|
234 |
+
fire(0.1, { spread: 120, startVelocity: 25, decay: 0.92, scalar: 1.2 });
|
235 |
+
fire(0.1, { spread: 120, startVelocity: 45 });
|
236 |
+
</script>
|
237 |
+
""")
|
238 |
+
|
239 |
+
# Enhanced AI question generation for guessing game using Llama model
|
240 |
+
def ask_llama(conversation_history, category, is_final_guess=False):
|
241 |
+
api_url = "https://api.groq.com/openai/v1/chat/completions"
|
242 |
+
headers = {
|
243 |
+
"Authorization": "Bearer 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",
|
244 |
+
"Content-Type": "application/json"
|
245 |
+
}
|
246 |
+
|
247 |
+
system_prompt = f"""You're playing 20 questions to guess a {category}. Follow these rules:
|
248 |
+
1. Ask strategic, non-repeating yes/no questions that narrow down possibilities
|
249 |
+
2. Consider all previous answers carefully before asking next question
|
250 |
+
3. If you're very confident (80%+ sure), respond with "Final Guess: [your guess]"
|
251 |
+
4. For places: ask about continent, climate, famous landmarks, country, city or population
|
252 |
+
5. For people: ask about fictional or real, profession, gender, alive/dead, nationality, or fame
|
253 |
+
6. For objects: ask about size, color, usage, material, or where it's found
|
254 |
+
7. Never repeat questions and always make progress toward guessing"""
|
255 |
+
|
256 |
+
if is_final_guess:
|
257 |
+
prompt = f"""Based on these answers about a {category}, provide ONLY your final guess with no extra text:
|
258 |
+
{conversation_history}"""
|
259 |
+
else:
|
260 |
+
prompt = "Ask your next strategic yes/no question that will best narrow down the possibilities."
|
261 |
+
|
262 |
+
messages = [
|
263 |
+
{"role": "system", "content": system_prompt},
|
264 |
+
*conversation_history,
|
265 |
+
{"role": "user", "content": prompt}
|
266 |
+
]
|
267 |
+
|
268 |
+
data = {
|
269 |
+
"model": "llama-3.3-70b-versatile",
|
270 |
+
"messages": messages,
|
271 |
+
"temperature": 0.7 if is_final_guess else 0.8,
|
272 |
+
"max_tokens": 100
|
273 |
+
}
|
274 |
+
|
275 |
+
try:
|
276 |
+
response = requests.post(api_url, headers=headers, json=data)
|
277 |
+
response.raise_for_status()
|
278 |
+
return response.json()["choices"][0]["message"]["content"]
|
279 |
+
except Exception as e:
|
280 |
+
st.error(f"Error calling Llama API: {str(e)}")
|
281 |
+
return "Could not generate question"
|
282 |
+
|
283 |
+
# New function for the help AI assistant using the Hugging Face InferenceClient
|
284 |
+
MISTRAL_API_KEY = "wm5eLl09b9I9cOxR3E9n5rrRr1CRQQjn"
|
285 |
+
def ask_help_agent(query):
|
286 |
+
try:
|
287 |
+
# Prepare Mistral API request
|
288 |
+
url = "https://api.mistral.ai/v1/chat/completions"
|
289 |
+
headers = {
|
290 |
+
"Authorization": f"Bearer {MISTRAL_API_KEY}",
|
291 |
+
"Content-Type": "application/json"
|
292 |
+
}
|
293 |
+
|
294 |
+
system_message = "You are a friendly Chatbot."
|
295 |
+
|
296 |
+
# Build message history
|
297 |
+
messages = [{"role": "system", "content": system_message}]
|
298 |
+
if "help_conversation" in st.session_state:
|
299 |
+
for msg in st.session_state.help_conversation:
|
300 |
+
if msg.get("query"):
|
301 |
+
messages.append({"role": "user", "content": msg["query"]})
|
302 |
+
if msg.get("response"):
|
303 |
+
messages.append({"role": "assistant", "content": msg["response"]})
|
304 |
+
|
305 |
+
# Add current user query
|
306 |
+
messages.append({"role": "user", "content": query})
|
307 |
+
|
308 |
+
# API payload
|
309 |
+
payload = {
|
310 |
+
"model": "mistral-tiny",
|
311 |
+
"messages": messages,
|
312 |
+
"temperature": 0.7,
|
313 |
+
"top_p": 0.95
|
314 |
+
}
|
315 |
+
|
316 |
+
# Send POST request
|
317 |
+
response = requests.post(url, headers=headers, json=payload)
|
318 |
+
|
319 |
+
if response.status_code == 200:
|
320 |
+
result = response.json()
|
321 |
+
return result["choices"][0]["message"]["content"]
|
322 |
+
else:
|
323 |
+
return f"API Error {response.status_code}: {response.text}"
|
324 |
+
|
325 |
+
except Exception as e:
|
326 |
+
return f"Error in help agent: {str(e)}"
|
327 |
+
|
328 |
# Import transformers and cache the help agent for performance
|
329 |
@st.cache_resource
|
330 |
def get_help_agent():
|
|
|
332 |
# Using BlenderBot 400M Distill as the public conversational model (used elsewhere)
|
333 |
return pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
334 |
|
335 |
+
# Main game logic with enhanced UI
|
|
|
|
|
|
|
|
|
|
|
336 |
def main():
|
337 |
inject_custom_css()
|
338 |
|
|
|
347 |
st.session_state.conversation_history = []
|
348 |
st.session_state.category = None
|
349 |
st.session_state.final_guess = None
|
350 |
+
st.session_state.help_conversation = [] # separate history for help agent
|
351 |
|
352 |
+
# Start screen with enhanced layout
|
353 |
if st.session_state.game_state == "start":
|
354 |
with st.container():
|
355 |
st.markdown("""
|