Update app.py
Browse files
app.py
CHANGED
|
@@ -16,13 +16,13 @@ state = {
|
|
| 16 |
"current_q": "",
|
| 17 |
"question_num": 0,
|
| 18 |
"hint_mode": False,
|
| 19 |
-
"final_answer": ""
|
| 20 |
-
"category": ""
|
| 21 |
}
|
| 22 |
|
| 23 |
def convert_audio_to_text(audio, lang_choices):
|
| 24 |
if audio is None:
|
| 25 |
return ""
|
|
|
|
| 26 |
try:
|
| 27 |
sr_rate, audio_data = audio
|
| 28 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
|
@@ -48,6 +48,7 @@ def query_llm(api, messages, model=None):
|
|
| 48 |
"MISTRAL": "https://api.mistral.ai/v1/chat/completions",
|
| 49 |
"GROQ": "https://api.groq.com/openai/v1/chat/completions"
|
| 50 |
}[api]
|
|
|
|
| 51 |
response = requests.post(endpoint, headers=headers, json=payload)
|
| 52 |
if response.status_code == 200:
|
| 53 |
return response.json()["choices"][0]["message"]["content"]
|
|
@@ -55,23 +56,27 @@ def query_llm(api, messages, model=None):
|
|
| 55 |
return f"[{api} Error] {response.status_code} - {response.text}"
|
| 56 |
|
| 57 |
def begin_game():
|
| 58 |
-
state
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def interpret_answer(user_answer):
|
| 69 |
if not state["started"]:
|
| 70 |
-
return "⛔ Please start a new game.", "", gr.update(visible=False)
|
| 71 |
|
| 72 |
normalized = user_answer.strip().lower()
|
| 73 |
if normalized not in ["yes", "no", "ہاں", "نہیں"]:
|
| 74 |
-
return "⚠️
|
| 75 |
|
| 76 |
state["history"].append((state["current_q"], normalized))
|
| 77 |
|
|
@@ -79,27 +84,26 @@ def interpret_answer(user_answer):
|
|
| 79 |
state["started"] = False
|
| 80 |
guess = generate_final_guess()
|
| 81 |
state["final_answer"] = guess
|
| 82 |
-
return f"🎯
|
| 83 |
|
| 84 |
if state["question_num"] % 5 == 0:
|
| 85 |
guess = generate_final_guess()
|
| 86 |
if "i think" in guess.lower():
|
| 87 |
state["current_q"] = f"{guess} Am I right?"
|
| 88 |
-
return f"{state['current_q']}", user_answer, gr.update(visible=False)
|
| 89 |
|
| 90 |
state["question_num"] += 1
|
| 91 |
state["current_q"] = get_next_question()
|
| 92 |
-
return f"Question {state['question_num']}: {state['current_q']}", "", gr.update(visible=False)
|
| 93 |
|
| 94 |
def get_next_question():
|
| 95 |
history_prompt = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 96 |
-
|
| 97 |
-
prompt = f"""You're playing a guessing game. Ask a smart yes/no question based on the following:\n{history_prompt}{category_hint}\nYour question:"""
|
| 98 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}]).strip()
|
| 99 |
|
| 100 |
def generate_final_guess():
|
| 101 |
history = "\n".join([f"Q: {q}\nA: {a}" for q, a in state["history"]])
|
| 102 |
-
prompt = f"""
|
| 103 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 104 |
|
| 105 |
def hint_response():
|
|
@@ -107,48 +111,42 @@ def hint_response():
|
|
| 107 |
return "Hint mode is off or game not active."
|
| 108 |
question = state["current_q"]
|
| 109 |
history = "\n".join([f"{q} - {a}" for q, a in state["history"]])
|
| 110 |
-
prompt = f"""The
|
| 111 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 112 |
|
| 113 |
def toggle_hint_mode():
|
| 114 |
state["hint_mode"] = not state["hint_mode"]
|
| 115 |
-
return ("Hint Mode: ON" if state["hint_mode"] else "Hint Mode: OFF")
|
| 116 |
-
|
| 117 |
-
def choose_category(category):
|
| 118 |
-
state["category"] = category
|
| 119 |
-
return f"📦 Category selected: {category}"
|
| 120 |
|
| 121 |
with gr.Blocks(title="Kasoti") as demo:
|
| 122 |
-
gr.Markdown("##
|
| 123 |
-
gr.Markdown("Think of something
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
-
start = gr.Button("🎲 Start Game")
|
| 127 |
-
hint_toggle = gr.Button("💡
|
| 128 |
-
hint_status = gr.Textbox(value="Hint Mode: OFF",
|
| 129 |
-
|
| 130 |
-
with gr.Row():
|
| 131 |
-
gr.Markdown("### 📂 Choose a category to help the AI")
|
| 132 |
-
with gr.Row():
|
| 133 |
-
for cat in ["Animal", "Plant", "Thing", "Car", "Human", "Place", "Other"]:
|
| 134 |
-
gr.Button(cat).click(fn=choose_category, inputs=[], outputs=[hint_status], _js=None, _name=None, _id=None, _target=None)
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
submit = gr.Button("✅ Submit Answer")
|
| 143 |
|
| 144 |
with gr.Column(scale=2):
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
start.click(fn=begin_game, outputs=[game_text,
|
| 149 |
-
|
| 150 |
-
submit.click(fn=interpret_answer, inputs=[typed_ans], outputs=[game_text, typed_ans, final_answer_box])
|
| 151 |
-
hint_toggle.click(fn=toggle_hint_mode, outputs=[hint_status])
|
| 152 |
hint_toggle.click(fn=hint_response, outputs=[hint_status])
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
demo.launch()
|
|
|
|
| 16 |
"current_q": "",
|
| 17 |
"question_num": 0,
|
| 18 |
"hint_mode": False,
|
| 19 |
+
"final_answer": ""
|
|
|
|
| 20 |
}
|
| 21 |
|
| 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:
|
|
|
|
| 48 |
"MISTRAL": "https://api.mistral.ai/v1/chat/completions",
|
| 49 |
"GROQ": "https://api.groq.com/openai/v1/chat/completions"
|
| 50 |
}[api]
|
| 51 |
+
|
| 52 |
response = requests.post(endpoint, headers=headers, json=payload)
|
| 53 |
if response.status_code == 200:
|
| 54 |
return response.json()["choices"][0]["message"]["content"]
|
|
|
|
| 56 |
return f"[{api} Error] {response.status_code} - {response.text}"
|
| 57 |
|
| 58 |
def begin_game():
|
| 59 |
+
state["started"] = True
|
| 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) # Final Answer Box - Hidden initially
|
| 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 ہاں/نہیں).", "", user_answer, 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 f"🎯 Final guess: {guess}", "", user_answer, gr.update(visible=True)
|
| 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 f"🤔 {state['current_q']}", "", user_answer, 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)
|
| 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: {history_prompt} Your question:"""
|
|
|
|
| 102 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}]).strip()
|
| 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." {history} Your guess:"""
|
| 107 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 108 |
|
| 109 |
def hint_response():
|
|
|
|
| 111 |
return "Hint mode is off or game not active."
|
| 112 |
question = state["current_q"]
|
| 113 |
history = "\n".join([f"{q} - {a}" for q, a in state["history"]])
|
| 114 |
+
prompt = f"""The player seems confused by this question: "{question}". Previous Q&A were:\n{history}\n\nGive a helpful, simple hint."""
|
| 115 |
return query_llm("MISTRAL", [{"role": "user", "content": prompt}])
|
| 116 |
|
| 117 |
def toggle_hint_mode():
|
| 118 |
state["hint_mode"] = not state["hint_mode"]
|
| 119 |
+
return ("Hint Mode: ON" if state["hint_mode"] else "Hint Mode: OFF"), gr.update(visible=state["hint_mode"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
with gr.Blocks(title="Kasoti") as demo:
|
| 122 |
+
gr.Markdown("## 🌸 Kasoti")
|
| 123 |
+
gr.Markdown("Think of something. I'll guess it in 20 tries or less. Answer **yes/no** each time.")
|
| 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 Zone")
|
| 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 here")
|
| 139 |
submit = gr.Button("✅ Submit Answer")
|
| 140 |
|
| 141 |
with gr.Column(scale=2):
|
| 142 |
+
gr.Markdown("### 🧠 Game Engine")
|
| 143 |
+
game_text = gr.Textbox(label="📝 Game Status", lines=10, interactive=False)
|
| 144 |
+
final_answer_box = gr.Textbox(label="🎯 Final Answer", visible=False, lines=3, interactive=False)
|
| 145 |
|
| 146 |
+
start.click(fn=begin_game, outputs=[game_text, hint_status, submit, final_answer_box])
|
| 147 |
+
hint_toggle.click(fn=toggle_hint_mode, outputs=[hint_status, hint_status])
|
|
|
|
|
|
|
| 148 |
hint_toggle.click(fn=hint_response, outputs=[hint_status])
|
| 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=[game_text, typed_ans, typed_ans, final_answer_box])
|
| 151 |
|
| 152 |
+
demo.launch()
|