Aranwer commited on
Commit
5f987f1
·
verified ·
1 Parent(s): db56e14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -18
app.py CHANGED
@@ -9,6 +9,7 @@ import numpy as np
9
  # Set your API keys as HF secrets or environment variables
10
  os.environ["MISTRAL_API_KEY"] = "cNjUx79Hl0A2AeiAMf6yi7o7ah4APoZy"
11
  os.environ["GROQ_API_KEY"] = "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"
 
12
  game_state = {
13
  "active": False,
14
  "questions_asked": 0,
@@ -23,8 +24,7 @@ def transcribe_audio(audio, language):
23
  return ""
24
  try:
25
  sr_rate, audio_data = audio
26
- if isinstance(audio_data, tuple):
27
- audio_data = np.array(audio_data)
28
 
29
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
30
  scipy.io.wavfile.write(tmp_file.name, sr_rate, audio_data)
@@ -38,7 +38,7 @@ def transcribe_audio(audio, language):
38
  return text.lower()
39
  except Exception as e:
40
  print(f"Transcription error: {e}")
41
- return ""
42
 
43
  def handle_user_question(user_question):
44
  if not game_state["consult_mode"]:
@@ -120,14 +120,14 @@ def process_answer(answer_text):
120
  return "❌ Please reply with 'yes' or 'no' (or 'ہاں/نہیں').", "", answer_text, gr.update(visible=game_state["consult_mode"])
121
 
122
  if game_state["current_question"] and "is this correct" in game_state["current_question"].lower():
123
- if normalized == "yes":
124
- game_state["active"] = False
125
- return "🎉 YAY! I guessed it!", "", answer_text, gr.update(visible=False)
126
- else:
127
- next_q = generate_question(game_state["answers"])
128
- game_state["current_question"] = next_q
129
- game_state["questions_asked"] += 1
130
- return next_q, "", answer_text, gr.update(visible=game_state["consult_mode"])
131
 
132
  game_state["answers"].append((game_state["current_question"], normalized))
133
 
@@ -151,7 +151,7 @@ def toggle_consult_mode():
151
  game_state["consult_mode"] = not game_state["consult_mode"]
152
  return ("🔔 Consult Mode: ON" if game_state["consult_mode"] else "🔕 Consult Mode: OFF",
153
  gr.update(visible=game_state["consult_mode"]),
154
- consult_row.update(visible=game_state["consult_mode"])) # Fix for consult row visibility toggle
155
 
156
  def get_consult_hint():
157
  if not game_state["active"] or not game_state["consult_mode"]:
@@ -164,12 +164,10 @@ with gr.Blocks(css="body {background-color: #f7f7f7;}") as demo:
164
  gr.Markdown("Think of a person, place, or thing. I'll try to guess it in 20 questions or less!")
165
 
166
  with gr.Row():
167
-
168
  with gr.Column():
169
  start_btn = gr.Button("🚀 Start Game", elem_id="start-btn")
170
  consult_output = gr.Textbox(label="💡 Consult Hint", visible=False)
171
-
172
-
173
  with gr.Row():
174
  with gr.Column():
175
  game_output = gr.Textbox(label="🎲 Game Progress", interactive=False)
@@ -180,12 +178,10 @@ with gr.Blocks(css="body {background-color: #f7f7f7;}") as demo:
180
  transcribed_text = gr.Textbox(label="✍️ Answer Text", interactive=True)
181
  submit_btn = gr.Button("✅ Submit Answer", elem_id="submit-btn")
182
 
183
-
184
-
185
  with gr.Row():
186
  consult_btn = gr.Button("💬 Toggle Consult Mode", elem_id="consult-btn")
187
  consult_status = gr.Textbox(label="Consult Mode", interactive=False)
188
- # Consultant Interaction Section
189
  with gr.Row(visible=False) as consult_row:
190
  user_question = gr.Textbox(label="Ask a question to the LLM")
191
  ask_btn = gr.Button("📤 Ask")
 
9
  # Set your API keys as HF secrets or environment variables
10
  os.environ["MISTRAL_API_KEY"] = "cNjUx79Hl0A2AeiAMf6yi7o7ah4APoZy"
11
  os.environ["GROQ_API_KEY"] = "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"
12
+
13
  game_state = {
14
  "active": False,
15
  "questions_asked": 0,
 
24
  return ""
25
  try:
26
  sr_rate, audio_data = audio
27
+ audio_data = np.array(audio_data, dtype=np.int16)
 
28
 
29
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
30
  scipy.io.wavfile.write(tmp_file.name, sr_rate, audio_data)
 
38
  return text.lower()
39
  except Exception as e:
40
  print(f"Transcription error: {e}")
41
+ return "Could not transcribe."
42
 
43
  def handle_user_question(user_question):
44
  if not game_state["consult_mode"]:
 
120
  return "❌ Please reply with 'yes' or 'no' (or 'ہاں/نہیں').", "", answer_text, gr.update(visible=game_state["consult_mode"])
121
 
122
  if game_state["current_question"] and "is this correct" in game_state["current_question"].lower():
123
+ if normalized == "yes":
124
+ game_state["active"] = False
125
+ return "🎉 YAY! I guessed it!", "", answer_text, gr.update(visible=False)
126
+ else:
127
+ next_q = generate_question(game_state["answers"])
128
+ game_state["current_question"] = next_q
129
+ game_state["questions_asked"] += 1
130
+ return next_q, "", answer_text, gr.update(visible=game_state["consult_mode"])
131
 
132
  game_state["answers"].append((game_state["current_question"], normalized))
133
 
 
151
  game_state["consult_mode"] = not game_state["consult_mode"]
152
  return ("🔔 Consult Mode: ON" if game_state["consult_mode"] else "🔕 Consult Mode: OFF",
153
  gr.update(visible=game_state["consult_mode"]),
154
+ gr.update(visible=game_state["consult_mode"]))
155
 
156
  def get_consult_hint():
157
  if not game_state["active"] or not game_state["consult_mode"]:
 
164
  gr.Markdown("Think of a person, place, or thing. I'll try to guess it in 20 questions or less!")
165
 
166
  with gr.Row():
 
167
  with gr.Column():
168
  start_btn = gr.Button("🚀 Start Game", elem_id="start-btn")
169
  consult_output = gr.Textbox(label="💡 Consult Hint", visible=False)
170
+
 
171
  with gr.Row():
172
  with gr.Column():
173
  game_output = gr.Textbox(label="🎲 Game Progress", interactive=False)
 
178
  transcribed_text = gr.Textbox(label="✍️ Answer Text", interactive=True)
179
  submit_btn = gr.Button("✅ Submit Answer", elem_id="submit-btn")
180
 
 
 
181
  with gr.Row():
182
  consult_btn = gr.Button("💬 Toggle Consult Mode", elem_id="consult-btn")
183
  consult_status = gr.Textbox(label="Consult Mode", interactive=False)
184
+
185
  with gr.Row(visible=False) as consult_row:
186
  user_question = gr.Textbox(label="Ask a question to the LLM")
187
  ask_btn = gr.Button("📤 Ask")