Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import speech_recognition as sr
|
|
@@ -9,8 +7,8 @@ import os
|
|
| 9 |
import numpy as np
|
| 10 |
|
| 11 |
# Set your API keys as HF secrets or environment variables
|
| 12 |
-
os.environ["MISTRAL_API_KEY"] =
|
| 13 |
-
os.environ["GROQ_API_KEY"] =
|
| 14 |
|
| 15 |
game_state = {
|
| 16 |
"active": False,
|
|
@@ -19,6 +17,7 @@ game_state = {
|
|
| 19 |
"current_question": None,
|
| 20 |
"consult_mode": False
|
| 21 |
}
|
|
|
|
| 22 |
|
| 23 |
def transcribe_audio(audio, language):
|
| 24 |
if audio is None:
|
|
@@ -40,7 +39,20 @@ def transcribe_audio(audio, language):
|
|
| 40 |
return text.lower()
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Transcription error: {e}")
|
| 43 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def query_llm(api, messages, model=None):
|
| 46 |
headers = {
|
|
@@ -147,31 +159,40 @@ def get_consult_hint():
|
|
| 147 |
return get_hint(game_state["current_question"], game_state["answers"])
|
| 148 |
|
| 149 |
# Colorful and polished UI
|
| 150 |
-
with gr.Blocks(css="
|
| 151 |
gr.Markdown("## 🧠 Kasoti: 20 Questions AI Game")
|
| 152 |
-
gr.Markdown("Think of a person, place, or thing. I'll try to guess it in 20 questions or less!
|
| 153 |
-
|
| 154 |
with gr.Row():
|
| 155 |
-
start_btn = gr.Button("🚀 Start Game")
|
| 156 |
-
consult_btn = gr.Button("💬 Toggle Consult Mode")
|
| 157 |
consult_status = gr.Textbox(label="Consult Mode", interactive=False)
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
with gr.Column():
|
| 161 |
language = gr.Dropdown(["English", "Urdu"], label="Audio Language", value="English")
|
| 162 |
audio_input = gr.Audio(label="🎤 Answer via Microphone", type="numpy", sources=["microphone"])
|
| 163 |
-
transcribe_btn = gr.Button("📝 Transcribe Audio")
|
| 164 |
transcribed_text = gr.Textbox(label="✍️ Answer Text", interactive=True)
|
| 165 |
-
submit_btn = gr.Button("✅ Submit Answer")
|
| 166 |
|
| 167 |
with gr.Column():
|
| 168 |
game_output = gr.Textbox(label="🎲 Game Progress", interactive=False)
|
| 169 |
consult_output = gr.Textbox(label="💡 Consult Hint", visible=False)
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
start_btn.click(start_game, outputs=[game_output, transcribed_text, submit_btn, consult_status, consult_output])
|
| 172 |
-
consult_btn.click(toggle_consult_mode, outputs=[consult_status, consult_output])
|
| 173 |
consult_btn.click(get_consult_hint, outputs=[consult_output])
|
| 174 |
transcribe_btn.click(transcribe_audio, inputs=[audio_input, language], outputs=[transcribed_text])
|
| 175 |
submit_btn.click(process_answer, inputs=[transcribed_text], outputs=[game_output, transcribed_text, transcribed_text, consult_output])
|
|
|
|
| 176 |
|
| 177 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import speech_recognition as sr
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
|
| 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,
|
|
|
|
| 17 |
"current_question": None,
|
| 18 |
"consult_mode": False
|
| 19 |
}
|
| 20 |
+
consult_history = []
|
| 21 |
|
| 22 |
def transcribe_audio(audio, language):
|
| 23 |
if audio is None:
|
|
|
|
| 39 |
return text.lower()
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Transcription error: {e}")
|
| 42 |
+
return "❌ Could not transcribe the audio."
|
| 43 |
+
|
| 44 |
+
def handle_user_question(user_question):
|
| 45 |
+
if not game_state["consult_mode"]:
|
| 46 |
+
return "❗Consultant mode is off.", gr.update(value=""), gr.update(visible=True)
|
| 47 |
+
|
| 48 |
+
messages = [{"role": "user", "content": user_question}]
|
| 49 |
+
answer = query_llm("MISTRAL", messages)
|
| 50 |
+
if answer:
|
| 51 |
+
consult_history.append((user_question, answer))
|
| 52 |
+
formatted_history = "\n".join([f"**Q:** {q}\n**A:** {a}" for q, a in consult_history])
|
| 53 |
+
return answer, gr.update(value=formatted_history), gr.update(visible=True)
|
| 54 |
+
else:
|
| 55 |
+
return "⚠️ Failed to get a response.", gr.update(), gr.update()
|
| 56 |
|
| 57 |
def query_llm(api, messages, model=None):
|
| 58 |
headers = {
|
|
|
|
| 159 |
return get_hint(game_state["current_question"], game_state["answers"])
|
| 160 |
|
| 161 |
# Colorful and polished UI
|
| 162 |
+
with gr.Blocks(css="") as demo:
|
| 163 |
gr.Markdown("## 🧠 Kasoti: 20 Questions AI Game")
|
| 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 |
+
start_btn = gr.Button("🚀 Start Game", elem_id="start-btn")
|
| 168 |
+
consult_btn = gr.Button("💬 Toggle Consult Mode", elem_id="consult-btn")
|
| 169 |
consult_status = gr.Textbox(label="Consult Mode", interactive=False)
|
| 170 |
|
| 171 |
with gr.Row():
|
| 172 |
with gr.Column():
|
| 173 |
language = gr.Dropdown(["English", "Urdu"], label="Audio Language", value="English")
|
| 174 |
audio_input = gr.Audio(label="🎤 Answer via Microphone", type="numpy", sources=["microphone"])
|
| 175 |
+
transcribe_btn = gr.Button("📝 Transcribe Audio", elem_id="transcribe-btn")
|
| 176 |
transcribed_text = gr.Textbox(label="✍️ Answer Text", interactive=True)
|
| 177 |
+
submit_btn = gr.Button("✅ Submit Answer", elem_id="submit-btn")
|
| 178 |
|
| 179 |
with gr.Column():
|
| 180 |
game_output = gr.Textbox(label="🎲 Game Progress", interactive=False)
|
| 181 |
consult_output = gr.Textbox(label="💡 Consult Hint", visible=False)
|
| 182 |
|
| 183 |
+
# Consultant Interaction Section
|
| 184 |
+
with gr.Row(visible=False) as consult_row:
|
| 185 |
+
user_question = gr.Textbox(label="Ask a question to the LLM")
|
| 186 |
+
ask_btn = gr.Button("📤 Ask")
|
| 187 |
+
llm_answer = gr.Textbox(label="🧠 LLM Answer", interactive=False)
|
| 188 |
+
consult_history_box = gr.Textbox(label="🗂️ Session History", lines=8, interactive=False)
|
| 189 |
+
|
| 190 |
+
# Events
|
| 191 |
start_btn.click(start_game, outputs=[game_output, transcribed_text, submit_btn, consult_status, consult_output])
|
| 192 |
+
consult_btn.click(toggle_consult_mode, outputs=[consult_status, consult_output, consult_row])
|
| 193 |
consult_btn.click(get_consult_hint, outputs=[consult_output])
|
| 194 |
transcribe_btn.click(transcribe_audio, inputs=[audio_input, language], outputs=[transcribed_text])
|
| 195 |
submit_btn.click(process_answer, inputs=[transcribed_text], outputs=[game_output, transcribed_text, transcribed_text, consult_output])
|
| 196 |
+
ask_btn.click(handle_user_question, inputs=[user_question], outputs=[llm_answer, consult_history_box, consult_row])
|
| 197 |
|
| 198 |
+
demo.launch()
|