Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
import whisper
|
|
@@ -11,7 +24,6 @@ from f5_tts.api import F5TTS
|
|
| 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 |
|
|
@@ -58,7 +70,6 @@ def get_ai_answer(question):
|
|
| 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}")
|
|
@@ -67,6 +78,8 @@ def get_ai_answer(question):
|
|
| 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:
|
|
@@ -110,6 +123,8 @@ def clone_and_speak(text):
|
|
| 110 |
# ========================================== #
|
| 111 |
# 4. Request Router Pipeline #
|
| 112 |
# ========================================== #
|
|
|
|
|
|
|
| 113 |
def chatbot_engine(text_input, audio_input):
|
| 114 |
if audio_input is not None:
|
| 115 |
user_transcription = whisper_model.transcribe(audio_input, fp16=False)
|
|
|
|
| 1 |
+
# ========================================== #
|
| 2 |
+
# 0. HUGGING FACE ZERO-GPU INITIALIZATION #
|
| 3 |
+
# ========================================== #
|
| 4 |
+
# CRITICAL: This MUST be imported before ANY other library!
|
| 5 |
+
try:
|
| 6 |
+
import spaces
|
| 7 |
+
except ImportError:
|
| 8 |
+
# Fallback to a dummy decorator if running locally without huggingface spaces
|
| 9 |
+
class spaces:
|
| 10 |
+
@staticmethod
|
| 11 |
+
def GPU(fn):
|
| 12 |
+
return fn
|
| 13 |
+
|
| 14 |
import os
|
| 15 |
import uuid
|
| 16 |
import whisper
|
|
|
|
| 24 |
# ========================================== #
|
| 25 |
# 1. System Setup & Configuration #
|
| 26 |
# ========================================== #
|
|
|
|
| 27 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "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")
|
| 28 |
voice_file = "my_voice_fixed.wav"
|
| 29 |
|
|
|
|
| 70 |
max_tokens=40,
|
| 71 |
timeout=15
|
| 72 |
)
|
|
|
|
| 73 |
return chat_completion.choices[0].message.content.strip()
|
| 74 |
except Exception as e:
|
| 75 |
print(f"⚠️ Groq API issue detected: {e}")
|
|
|
|
| 78 |
# ========================================== #
|
| 79 |
# 3. Native Voice Synthesis Processing #
|
| 80 |
# ========================================== #
|
| 81 |
+
# CRITICAL FIX: Allocates GPU dynamically on Hugging Face Spaces during generation
|
| 82 |
+
@spaces.GPU
|
| 83 |
def clone_and_speak(text):
|
| 84 |
try:
|
| 85 |
if "The AI engine is currently offline" in text:
|
|
|
|
| 123 |
# ========================================== #
|
| 124 |
# 4. Request Router Pipeline #
|
| 125 |
# ========================================== #
|
| 126 |
+
# CRITICAL FIX: Allocates GPU dynamically for Whisper voice transcription
|
| 127 |
+
@spaces.GPU
|
| 128 |
def chatbot_engine(text_input, audio_input):
|
| 129 |
if audio_input is not None:
|
| 130 |
user_transcription = whisper_model.transcribe(audio_input, fp16=False)
|