Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import tempfile
|
|
| 6 |
import scipy.io.wavfile
|
| 7 |
import requests
|
| 8 |
|
| 9 |
-
# Set your API keys
|
| 10 |
os.environ["MISTRAL_API_KEY"] = "R1ISnVkHrj7fSd5Dh6ZSZHqCJhhct0ZR"
|
| 11 |
os.environ["GROQ_API_KEY"] = "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"
|
| 12 |
|
|
@@ -51,7 +51,7 @@ def query_llm(api, messages, model=None):
|
|
| 51 |
|
| 52 |
response = requests.post(endpoint, headers=headers, json=payload)
|
| 53 |
if response.status_code == 200:
|
| 54 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 55 |
else:
|
| 56 |
return f"[{api} Error] {response.status_code} - {response.text}"
|
| 57 |
|
|
@@ -60,23 +60,23 @@ def begin_game():
|
|
| 60 |
state["history"] = []
|
| 61 |
state["question_num"] = 1
|
| 62 |
state["hint_mode"] = False
|
| 63 |
-
state["current_q"] = "Does it belong to the world of living things?"
|
| 64 |
state["final_answer"] = ""
|
|
|
|
| 65 |
return (
|
| 66 |
-
"🎮 Game started! Think of something and answer with yes/no.\n\n"
|
| 67 |
f"Question 1: {state['current_q']}",
|
|
|
|
| 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)
|
| 76 |
|
| 77 |
normalized = user_answer.strip().lower()
|
| 78 |
if normalized not in ["yes", "no", "ہاں", "نہیں"]:
|
| 79 |
-
return "⚠️ Respond with 'yes' or 'no' (or ہاں/نہیں).", "",
|
| 80 |
|
| 81 |
state["history"].append((state["current_q"], normalized))
|
| 82 |
|
|
@@ -84,26 +84,26 @@ def interpret_answer(user_answer):
|
|
| 84 |
state["started"] = False
|
| 85 |
guess = generate_final_guess()
|
| 86 |
state["final_answer"] = guess
|
| 87 |
-
return
|
| 88 |
|
| 89 |
if state["question_num"] % 5 == 0:
|
| 90 |
guess = generate_final_guess()
|
| 91 |
-
if "i think" in guess.lower():
|
| 92 |
state["current_q"] = f"{guess} Am I right?"
|
| 93 |
-
return
|
| 94 |
|
| 95 |
state["question_num"] += 1
|
| 96 |
state["current_q"] = get_next_question()
|
| 97 |
-
return f"
|
| 98 |
|
| 99 |
def get_next_question():
|
| 100 |
history_prompt = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 101 |
-
prompt = f"""You're playing a guessing game. You can only ask yes/no questions. Based on the following history, ask the next smart question
|
| 102 |
-
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 103 |
|
| 104 |
def generate_final_guess():
|
| 105 |
history = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 106 |
-
prompt = f"""Guess the secret concept based on these Q&A. If you're not sure, say "I need more clues."
|
| 107 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 108 |
|
| 109 |
def hint_response():
|
|
@@ -116,37 +116,39 @@ def hint_response():
|
|
| 116 |
|
| 117 |
def toggle_hint_mode():
|
| 118 |
state["hint_mode"] = not state["hint_mode"]
|
| 119 |
-
return (
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
with gr.Blocks(title="Kasoti") as demo:
|
| 122 |
-
gr.Markdown("##
|
| 123 |
-
gr.Markdown("Think of something. I
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
start = gr.Button("🎲 Start Game", scale=1)
|
| 127 |
-
hint_toggle = gr.Button("💡Hint Mode", scale=1)
|
| 128 |
hint_status = gr.Textbox(value="Hint Mode: OFF", show_label=False, interactive=False)
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
with gr.Column(scale=1):
|
| 132 |
-
gr.Markdown("### 🎤 Input
|
| 133 |
-
lang_sel = gr.CheckboxGroup(
|
| 134 |
-
["English", "Urdu"], label="Select Languages", value=["English"]
|
| 135 |
-
)
|
| 136 |
mic_input = gr.Audio(sources=["microphone"], type="numpy", label="Record Your Answer")
|
| 137 |
transcribe = gr.Button("🎙️ Transcribe")
|
| 138 |
-
typed_ans = gr.Textbox(label="✍️ Or type your answer
|
| 139 |
submit = gr.Button("✅ Submit Answer")
|
| 140 |
|
| 141 |
with gr.Column(scale=2):
|
| 142 |
-
gr.Markdown("###
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
hint_toggle.click(fn=
|
|
|
|
| 149 |
transcribe.click(fn=convert_audio_to_text, inputs=[mic_input, lang_sel], outputs=[typed_ans])
|
| 150 |
-
submit.click(fn=interpret_answer, inputs=[typed_ans], outputs=[
|
| 151 |
|
| 152 |
-
demo.launch()
|
|
|
|
| 6 |
import scipy.io.wavfile
|
| 7 |
import requests
|
| 8 |
|
| 9 |
+
# Set your API keys
|
| 10 |
os.environ["MISTRAL_API_KEY"] = "R1ISnVkHrj7fSd5Dh6ZSZHqCJhhct0ZR"
|
| 11 |
os.environ["GROQ_API_KEY"] = "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"
|
| 12 |
|
|
|
|
| 51 |
|
| 52 |
response = requests.post(endpoint, headers=headers, json=payload)
|
| 53 |
if response.status_code == 200:
|
| 54 |
+
return response.json()["choices"][0]["message"]["content"].strip()
|
| 55 |
else:
|
| 56 |
return f"[{api} Error] {response.status_code} - {response.text}"
|
| 57 |
|
|
|
|
| 60 |
state["history"] = []
|
| 61 |
state["question_num"] = 1
|
| 62 |
state["hint_mode"] = False
|
|
|
|
| 63 |
state["final_answer"] = ""
|
| 64 |
+
state["current_q"] = "Does it belong to the world of living things?"
|
| 65 |
return (
|
|
|
|
| 66 |
f"Question 1: {state['current_q']}",
|
| 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 |
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 f"Question {state['question_num']}: {state['current_q']}", user_answer, gr.update(visible=False), "", gr.update(visible=False)
|
| 98 |
|
| 99 |
def get_next_question():
|
| 100 |
history_prompt = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 101 |
+
prompt = f"""You're playing a guessing game. You can only ask yes/no questions. Based on the following history, ask the next smart question:\n{history_prompt}\n\nYour question:"""
|
| 102 |
+
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 103 |
|
| 104 |
def generate_final_guess():
|
| 105 |
history = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 106 |
+
prompt = f"""Guess the secret concept based on these Q&A. If you're not sure, say "I need more clues."\n\n{history}\n\nYour guess:"""
|
| 107 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 108 |
|
| 109 |
def hint_response():
|
|
|
|
| 116 |
|
| 117 |
def toggle_hint_mode():
|
| 118 |
state["hint_mode"] = not state["hint_mode"]
|
| 119 |
+
return (
|
| 120 |
+
"Hint Mode: ON" if state["hint_mode"] else "Hint Mode: OFF",
|
| 121 |
+
gr.update(visible=True)
|
| 122 |
+
)
|
| 123 |
|
| 124 |
with gr.Blocks(title="Kasoti") as demo:
|
| 125 |
+
gr.Markdown("## 🧠 Kasoti: 20 Questions Challenge")
|
| 126 |
+
gr.Markdown("Think of something. I will try to guess it in 20 yes/no questions. Answer with **yes/no** or **ہاں/نہیں**.")
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
start = gr.Button("🎲 Start Game", scale=1)
|
| 130 |
+
hint_toggle = gr.Button("💡 Toggle Hint Mode", scale=1)
|
| 131 |
hint_status = gr.Textbox(value="Hint Mode: OFF", show_label=False, interactive=False)
|
| 132 |
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column(scale=1):
|
| 135 |
+
gr.Markdown("### 🎤 Input")
|
| 136 |
+
lang_sel = gr.CheckboxGroup(["English", "Urdu"], label="Select Language", value=["English"])
|
|
|
|
|
|
|
| 137 |
mic_input = gr.Audio(sources=["microphone"], type="numpy", label="Record Your Answer")
|
| 138 |
transcribe = gr.Button("🎙️ Transcribe")
|
| 139 |
+
typed_ans = gr.Textbox(label="✍️ Or type your answer")
|
| 140 |
submit = gr.Button("✅ Submit Answer")
|
| 141 |
|
| 142 |
with gr.Column(scale=2):
|
| 143 |
+
gr.Markdown("### 🎮 Game Status")
|
| 144 |
+
game_q_box = gr.Textbox(label="🧩 Current Question", interactive=False, lines=2)
|
| 145 |
+
game_history = gr.Textbox(label="🧾 Hint / Status", interactive=False, lines=2)
|
| 146 |
+
final_answer_box = gr.Textbox(label="🎯 Final Answer", visible=False, lines=2, interactive=False)
|
| 147 |
+
|
| 148 |
+
start.click(fn=begin_game, outputs=[game_q_box, game_history, final_answer_box, submit, final_answer_box])
|
| 149 |
+
hint_toggle.click(fn=toggle_hint_mode, outputs=[hint_status, game_history])
|
| 150 |
+
hint_toggle.click(fn=hint_response, outputs=[game_history])
|
| 151 |
transcribe.click(fn=convert_audio_to_text, inputs=[mic_input, lang_sel], outputs=[typed_ans])
|
| 152 |
+
submit.click(fn=interpret_answer, inputs=[typed_ans], outputs=[game_q_box, typed_ans, final_answer_box, game_history, final_answer_box])
|
| 153 |
|
| 154 |
+
demo.launch()
|