Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 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):
|
|
@@ -28,7 +26,7 @@ GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "gsk_21Y7DDNOd1wmlESvRku4WGdyb3FY9
|
|
| 28 |
voice_file = "my_voice_fixed.wav"
|
| 29 |
|
| 30 |
if not os.path.exists(voice_file):
|
| 31 |
-
print(f"⚠️ Reference audio file '{voice_file}' not found!
|
| 32 |
sample_rate = 24000
|
| 33 |
duration = 1.0
|
| 34 |
audio_data = np.zeros(int(sample_rate * duration), dtype=np.int16)
|
|
@@ -70,6 +68,7 @@ def get_ai_answer(question):
|
|
| 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,8 +77,6 @@ def get_ai_answer(question):
|
|
| 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:
|
|
@@ -94,6 +91,7 @@ def clone_and_speak(text):
|
|
| 94 |
gen_text=text
|
| 95 |
)
|
| 96 |
|
|
|
|
| 97 |
if isinstance(result, tuple):
|
| 98 |
if len(result) == 2:
|
| 99 |
audio_data, sample_rate = result
|
|
@@ -123,7 +121,7 @@ def clone_and_speak(text):
|
|
| 123 |
# ========================================== #
|
| 124 |
# 4. Request Router Pipeline #
|
| 125 |
# ========================================== #
|
| 126 |
-
#
|
| 127 |
@spaces.GPU
|
| 128 |
def chatbot_engine(text_input, audio_input):
|
| 129 |
if audio_input is not None:
|
|
|
|
| 1 |
# ========================================== #
|
| 2 |
# 0. HUGGING FACE ZERO-GPU INITIALIZATION #
|
| 3 |
# ========================================== #
|
|
|
|
| 4 |
try:
|
| 5 |
import spaces
|
| 6 |
except ImportError:
|
|
|
|
| 7 |
class spaces:
|
| 8 |
@staticmethod
|
| 9 |
def GPU(fn):
|
|
|
|
| 26 |
voice_file = "my_voice_fixed.wav"
|
| 27 |
|
| 28 |
if not os.path.exists(voice_file):
|
| 29 |
+
print(f"⚠️ Reference audio file '{voice_file}' not found! Creating safe baseline array profile...")
|
| 30 |
sample_rate = 24000
|
| 31 |
duration = 1.0
|
| 32 |
audio_data = np.zeros(int(sample_rate * duration), dtype=np.int16)
|
|
|
|
| 68 |
max_tokens=40,
|
| 69 |
timeout=15
|
| 70 |
)
|
| 71 |
+
# FIXED: Added missing [0] index to resolve choices object extraction crash
|
| 72 |
return chat_completion.choices[0].message.content.strip()
|
| 73 |
except Exception as e:
|
| 74 |
print(f"⚠️ Groq API issue detected: {e}")
|
|
|
|
| 77 |
# ========================================== #
|
| 78 |
# 3. Native Voice Synthesis Processing #
|
| 79 |
# ========================================== #
|
|
|
|
|
|
|
| 80 |
def clone_and_speak(text):
|
| 81 |
try:
|
| 82 |
if "The AI engine is currently offline" in text:
|
|
|
|
| 91 |
gen_text=text
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# FIXED: Corrected tuple indexing format structure
|
| 95 |
if isinstance(result, tuple):
|
| 96 |
if len(result) == 2:
|
| 97 |
audio_data, sample_rate = result
|
|
|
|
| 121 |
# ========================================== #
|
| 122 |
# 4. Request Router Pipeline #
|
| 123 |
# ========================================== #
|
| 124 |
+
# SINGLE DECORATOR: Reserves the GPU session cleanly for all execution sub-calls
|
| 125 |
@spaces.GPU
|
| 126 |
def chatbot_engine(text_input, audio_input):
|
| 127 |
if audio_input is not None:
|