shaikrabbani-dev commited on
Commit
f3c6970
·
verified ·
1 Parent(s): 4dd712f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -9
app.py CHANGED
@@ -11,7 +11,7 @@ from f5_tts.api import F5TTS
11
  # ========================================== #
12
  # 1. System Setup & Configuration #
13
  # ========================================== #
14
- # SECURE FIX: Automatically look for the Hugging Face Space Secret first
15
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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")
16
  voice_file = "my_voice_fixed.wav"
17
 
@@ -49,10 +49,6 @@ print("All Speech Systems Fully Active!")
49
  # ========================================== #
50
  def get_ai_answer(question):
51
  try:
52
- # Quick validation check to warn you in the UI
53
- if not GROQ_API_KEY or GROQ_API_KEY.startswith("gsk_21Y7DDN"):
54
- return "Error: Your Groq API key is expired or invalid. Please add a valid GROQ_API_KEY to your Hugging Face Space Settings -> Secrets."
55
-
56
  chat_completion = groq_client.chat.completions.create(
57
  messages=[
58
  {"role": "system", "content": "You are a professional voice simulation assistant. Keep answers ultra short, under 10 words."},
@@ -62,19 +58,18 @@ def get_ai_answer(question):
62
  max_tokens=40,
63
  timeout=15
64
  )
 
65
  return chat_completion.choices[0].message.content.strip()
66
  except Exception as e:
67
  print(f"⚠️ Groq API issue detected: {e}")
68
- # DIAGNOSTIC FIX: Returns the actual error string so you can see exactly why it is failing
69
- return f"API Connection Error: {str(e)}"
70
 
71
  # ========================================== #
72
  # 3. Native Voice Synthesis Processing #
73
  # ========================================== #
74
  def clone_and_speak(text):
75
  try:
76
- # Don't waste compute resources if text generation failed
77
- if "Error:" in text or "Connection Error:" in text:
78
  return None
79
 
80
  unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
 
11
  # ========================================== #
12
  # 1. System Setup & Configuration #
13
  # ========================================== #
14
+ # Pull secure secret value dynamically from your Hugging Face Space environment
15
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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")
16
  voice_file = "my_voice_fixed.wav"
17
 
 
49
  # ========================================== #
50
  def get_ai_answer(question):
51
  try:
 
 
 
 
52
  chat_completion = groq_client.chat.completions.create(
53
  messages=[
54
  {"role": "system", "content": "You are a professional voice simulation assistant. Keep answers ultra short, under 10 words."},
 
58
  max_tokens=40,
59
  timeout=15
60
  )
61
+ # CRITICAL FIXED LINE: Properly indexing the first entry [0] within the list structure
62
  return chat_completion.choices[0].message.content.strip()
63
  except Exception as e:
64
  print(f"⚠️ Groq API issue detected: {e}")
65
+ return f"The AI engine is currently offline. Details: {str(e)}"
 
66
 
67
  # ========================================== #
68
  # 3. Native Voice Synthesis Processing #
69
  # ========================================== #
70
  def clone_and_speak(text):
71
  try:
72
+ if "The AI engine is currently offline" in text:
 
73
  return None
74
 
75
  unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"