Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,6 @@ state = {
|
|
22 |
def convert_audio_to_text(audio, lang_choices):
|
23 |
if audio is None:
|
24 |
return ""
|
25 |
-
|
26 |
try:
|
27 |
sr_rate, audio_data = audio
|
28 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
@@ -67,16 +66,17 @@ def begin_game():
|
|
67 |
"",
|
68 |
gr.update(visible=False),
|
69 |
gr.update(visible=True),
|
70 |
-
gr.update(visible=False)
|
|
|
71 |
)
|
72 |
|
73 |
def interpret_answer(user_answer):
|
74 |
if not state["started"]:
|
75 |
-
return "⛔ Please start a new game.", "", gr.update(visible=False), "", gr.update(visible=False)
|
76 |
|
77 |
normalized = user_answer.strip().lower()
|
78 |
if normalized not in ["yes", "no", "ہاں", "نہیں"]:
|
79 |
-
return "⚠️ Respond with 'yes' or 'no' (or ہاں/نہیں).", user_answer, gr.update(visible=False), "", gr.update(visible=False)
|
80 |
|
81 |
state["history"].append((state["current_q"], normalized))
|
82 |
|
@@ -84,26 +84,33 @@ def interpret_answer(user_answer):
|
|
84 |
state["started"] = False
|
85 |
guess = generate_final_guess()
|
86 |
state["final_answer"] = guess
|
87 |
-
return "Game Over!", user_answer, gr.update(visible=True), "", gr.update(value=guess, visible=True)
|
88 |
|
89 |
if state["question_num"] % 5 == 0:
|
90 |
guess = generate_final_guess()
|
91 |
if "i think" in guess.lower() or "maybe" in guess.lower():
|
92 |
state["current_q"] = f"{guess} Am I right?"
|
93 |
-
return state["current_q"], user_answer, gr.update(visible=False), "", gr.update(visible=False)
|
94 |
|
95 |
state["question_num"] += 1
|
96 |
state["current_q"] = get_next_question()
|
97 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
def get_next_question():
|
100 |
history_prompt = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
101 |
prompt = (
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
)
|
107 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
108 |
|
109 |
def generate_final_guess():
|
@@ -125,8 +132,9 @@ def toggle_hint_mode():
|
|
125 |
"Hint Mode: ON" if state["hint_mode"] else "Hint Mode: OFF",
|
126 |
gr.update(visible=True)
|
127 |
)
|
|
|
|
|
128 |
with gr.Blocks(title="Kasoti", theme=gr.themes.Soft(primary_hue="pink", secondary_hue="blue")) as demo:
|
129 |
-
# Fonts & CSS
|
130 |
gr.Markdown(
|
131 |
"""
|
132 |
<link href="https://fonts.googleapis.com/css2?family=Comic+Neue&display=swap" rel="stylesheet">
|
@@ -185,15 +193,13 @@ with gr.Blocks(title="Kasoti", theme=gr.themes.Soft(primary_hue="pink", secondar
|
|
185 |
fn=begin_game,
|
186 |
outputs=[game_q_box, game_history, final_answer_box, submit, final_answer_box, question_progress]
|
187 |
)
|
188 |
-
|
189 |
hint_toggle.click(fn=toggle_hint_mode, outputs=[hint_status, game_history])
|
190 |
hint_toggle.click(fn=hint_response, outputs=[game_history])
|
191 |
transcribe.click(fn=convert_audio_to_text, inputs=[mic_input, lang_sel], outputs=[typed_ans])
|
192 |
-
|
193 |
submit.click(
|
194 |
fn=interpret_answer,
|
195 |
inputs=[typed_ans],
|
196 |
outputs=[game_q_box, typed_ans, final_answer_box, game_history, final_answer_box, question_progress]
|
197 |
)
|
198 |
|
199 |
-
demo.launch()
|
|
|
22 |
def convert_audio_to_text(audio, lang_choices):
|
23 |
if audio is None:
|
24 |
return ""
|
|
|
25 |
try:
|
26 |
sr_rate, audio_data = audio
|
27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
|
|
66 |
"",
|
67 |
gr.update(visible=False),
|
68 |
gr.update(visible=True),
|
69 |
+
gr.update(visible=False),
|
70 |
+
state["question_num"]
|
71 |
)
|
72 |
|
73 |
def interpret_answer(user_answer):
|
74 |
if not state["started"]:
|
75 |
+
return "⛔ Please start a new game.", "", gr.update(visible=False), "", gr.update(visible=False), state["question_num"]
|
76 |
|
77 |
normalized = user_answer.strip().lower()
|
78 |
if normalized not in ["yes", "no", "ہاں", "نہیں"]:
|
79 |
+
return "⚠️ Respond with 'yes' or 'no' (or ہاں/نہیں).", user_answer, gr.update(visible=False), "", gr.update(visible=False), state["question_num"]
|
80 |
|
81 |
state["history"].append((state["current_q"], normalized))
|
82 |
|
|
|
84 |
state["started"] = False
|
85 |
guess = generate_final_guess()
|
86 |
state["final_answer"] = guess
|
87 |
+
return "Game Over!", user_answer, gr.update(visible=True), "", gr.update(value=guess, visible=True), state["question_num"]
|
88 |
|
89 |
if state["question_num"] % 5 == 0:
|
90 |
guess = generate_final_guess()
|
91 |
if "i think" in guess.lower() or "maybe" in guess.lower():
|
92 |
state["current_q"] = f"{guess} Am I right?"
|
93 |
+
return state["current_q"], user_answer, gr.update(visible=False), "", gr.update(visible=False), state["question_num"]
|
94 |
|
95 |
state["question_num"] += 1
|
96 |
state["current_q"] = get_next_question()
|
97 |
+
return (
|
98 |
+
f"Question {state['question_num']}: {state['current_q']}",
|
99 |
+
user_answer,
|
100 |
+
gr.update(visible=False),
|
101 |
+
"",
|
102 |
+
gr.update(visible=False),
|
103 |
+
state["question_num"]
|
104 |
+
)
|
105 |
|
106 |
def get_next_question():
|
107 |
history_prompt = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
108 |
prompt = (
|
109 |
+
"You're playing a guessing game. Only respond with a yes/no question, nothing else.\n"
|
110 |
+
"Based on the following history, ask the next smart question:\n\n"
|
111 |
+
f"{history_prompt}\n\n"
|
112 |
+
"Next question only:"
|
113 |
+
)
|
114 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
115 |
|
116 |
def generate_final_guess():
|
|
|
132 |
"Hint Mode: ON" if state["hint_mode"] else "Hint Mode: OFF",
|
133 |
gr.update(visible=True)
|
134 |
)
|
135 |
+
|
136 |
+
# UI
|
137 |
with gr.Blocks(title="Kasoti", theme=gr.themes.Soft(primary_hue="pink", secondary_hue="blue")) as demo:
|
|
|
138 |
gr.Markdown(
|
139 |
"""
|
140 |
<link href="https://fonts.googleapis.com/css2?family=Comic+Neue&display=swap" rel="stylesheet">
|
|
|
193 |
fn=begin_game,
|
194 |
outputs=[game_q_box, game_history, final_answer_box, submit, final_answer_box, question_progress]
|
195 |
)
|
|
|
196 |
hint_toggle.click(fn=toggle_hint_mode, outputs=[hint_status, game_history])
|
197 |
hint_toggle.click(fn=hint_response, outputs=[game_history])
|
198 |
transcribe.click(fn=convert_audio_to_text, inputs=[mic_input, lang_sel], outputs=[typed_ans])
|
|
|
199 |
submit.click(
|
200 |
fn=interpret_answer,
|
201 |
inputs=[typed_ans],
|
202 |
outputs=[game_q_box, typed_ans, final_answer_box, game_history, final_answer_box, question_progress]
|
203 |
)
|
204 |
|
205 |
+
demo.launch()
|