Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,8 @@ def _load_key() -> str:
|
|
| 8 |
key = os.environ.get("GROQ_API_KEY") or os.environ.get("groq_api_key")
|
| 9 |
if not key:
|
| 10 |
raise RuntimeError(
|
| 11 |
-
"Groq API key not found. In your Space
|
| 12 |
-
"or set env var GROQ_API_KEY."
|
| 13 |
)
|
| 14 |
-
# Ensure SDK can also read it from the standard name
|
| 15 |
os.environ["GROQ_API_KEY"] = key
|
| 16 |
return key
|
| 17 |
|
|
@@ -26,8 +24,7 @@ def transcribe_audio(audio_path: str, model: str = "whisper-large-v3") -> str:
|
|
| 26 |
model=model,
|
| 27 |
response_format="verbose_json",
|
| 28 |
)
|
| 29 |
-
|
| 30 |
-
return text.strip()
|
| 31 |
|
| 32 |
def stream_answer(prompt_text: str,
|
| 33 |
model: str = "llama-3.1-8b-instant",
|
|
@@ -67,7 +64,7 @@ def text_to_speech(text: str,
|
|
| 67 |
input=tts_input,
|
| 68 |
)
|
| 69 |
out_path = os.path.join(tempfile.gettempdir(), f"answer_{int(time.time())}.{fmt}")
|
| 70 |
-
#
|
| 71 |
resp.write_to_file(out_path)
|
| 72 |
return out_path
|
| 73 |
|
|
@@ -109,7 +106,7 @@ def run_pipeline(audio_file, typed_question, llm_model, voice_name):
|
|
| 109 |
short_tb = "\n".join(traceback.format_exc().splitlines()[-6:])
|
| 110 |
help_tip = (
|
| 111 |
"\nTips:\n"
|
| 112 |
-
"- Check
|
| 113 |
"- Try a shorter audio clip.\n"
|
| 114 |
"- Verify model names.\n"
|
| 115 |
"- Confirm requirements installed."
|
|
@@ -158,5 +155,7 @@ with gr.Blocks(title="Audio Q&A with Groq") as demo:
|
|
| 158 |
return "", "", None, ""
|
| 159 |
clear_btn.click(fn=clear_all, inputs=None, outputs=[transcript_box, answer_box, answer_audio, status_md])
|
| 160 |
|
| 161 |
-
|
| 162 |
-
demo.
|
|
|
|
|
|
|
|
|
| 8 |
key = os.environ.get("GROQ_API_KEY") or os.environ.get("groq_api_key")
|
| 9 |
if not key:
|
| 10 |
raise RuntimeError(
|
| 11 |
+
"Groq API key not found. In your Space settings -> Secrets, add 'groq_api_key'."
|
|
|
|
| 12 |
)
|
|
|
|
| 13 |
os.environ["GROQ_API_KEY"] = key
|
| 14 |
return key
|
| 15 |
|
|
|
|
| 24 |
model=model,
|
| 25 |
response_format="verbose_json",
|
| 26 |
)
|
| 27 |
+
return (getattr(resp, "text", "") or "").strip()
|
|
|
|
| 28 |
|
| 29 |
def stream_answer(prompt_text: str,
|
| 30 |
model: str = "llama-3.1-8b-instant",
|
|
|
|
| 64 |
input=tts_input,
|
| 65 |
)
|
| 66 |
out_path = os.path.join(tempfile.gettempdir(), f"answer_{int(time.time())}.{fmt}")
|
| 67 |
+
# BinaryAPIResponse uses write_to_file in Groq SDK
|
| 68 |
resp.write_to_file(out_path)
|
| 69 |
return out_path
|
| 70 |
|
|
|
|
| 106 |
short_tb = "\n".join(traceback.format_exc().splitlines()[-6:])
|
| 107 |
help_tip = (
|
| 108 |
"\nTips:\n"
|
| 109 |
+
"- Check Space secret 'groq_api_key'.\n"
|
| 110 |
"- Try a shorter audio clip.\n"
|
| 111 |
"- Verify model names.\n"
|
| 112 |
"- Confirm requirements installed."
|
|
|
|
| 155 |
return "", "", None, ""
|
| 156 |
clear_btn.click(fn=clear_all, inputs=None, outputs=[transcript_box, answer_box, answer_audio, status_md])
|
| 157 |
|
| 158 |
+
if __name__ == "__main__":
|
| 159 |
+
# On HF Spaces you can simply do demo.launch()
|
| 160 |
+
# Queue enables generator streaming without extra args in Gradio v4
|
| 161 |
+
demo.queue().launch()
|