ibrahim313 commited on
Commit
f872fa9
·
verified ·
1 Parent(s): bfa0389

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
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, add a secret named 'groq_api_key' "
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
- text = getattr(resp, "text", "") or ""
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
- # Groq SDK returns BinaryAPIResponse. Use write_to_file, not stream_to_file.
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 your Groq key in Space secrets.\n"
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
- # Use queue for better concurrency on Spaces
162
- demo.queue(concurrency_count=2).launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
 
 
 
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()