Spaces:
Sleeping
Sleeping
Delete DocVoice.py
Browse files- DocVoice.py +0 -44
DocVoice.py
DELETED
|
@@ -1,44 +0,0 @@
|
|
| 1 |
-
# DocVoice.py
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
import soundfile as sf
|
| 5 |
-
|
| 6 |
-
# -------------------
|
| 7 |
-
# 1️⃣ Detect GPU
|
| 8 |
-
# -------------------
|
| 9 |
-
use_cuda = torch.cuda.is_available()
|
| 10 |
-
device = 0 if use_cuda else -1
|
| 11 |
-
print(f"🌟 Using {'GPU' if use_cuda else 'CPU'}")
|
| 12 |
-
|
| 13 |
-
# -------------------
|
| 14 |
-
# 2️⃣ Load TTS model
|
| 15 |
-
# -------------------
|
| 16 |
-
tts_model_id = "microsoft/speecht5_tts" # Compatible TTS model
|
| 17 |
-
|
| 18 |
-
tts_pipe = pipeline(
|
| 19 |
-
"text-to-speech",
|
| 20 |
-
model=tts_model_id,
|
| 21 |
-
device=device
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
print("🔊 TTS pipeline ready using Hugging Face.")
|
| 25 |
-
|
| 26 |
-
# -------------------
|
| 27 |
-
# 3️⃣ TTS Helper Function
|
| 28 |
-
# -------------------
|
| 29 |
-
def text_to_speech(text: str, filename="assistant_response.wav"):
|
| 30 |
-
"""
|
| 31 |
-
Generate speech from text and save as WAV file.
|
| 32 |
-
"""
|
| 33 |
-
if not text.strip():
|
| 34 |
-
return None
|
| 35 |
-
|
| 36 |
-
print(f"📝 Generating audio for: {text}")
|
| 37 |
-
|
| 38 |
-
speech_array = tts_pipe(text)[0]["array"] # returns numpy array
|
| 39 |
-
sample_rate = tts_pipe.model.config.sampling_rate if hasattr(tts_pipe.model.config, "sampling_rate") else 16000
|
| 40 |
-
|
| 41 |
-
# Save audio
|
| 42 |
-
sf.write(filename, speech_array, sample_rate)
|
| 43 |
-
print(f"✅ Audio saved as {filename}")
|
| 44 |
-
return filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|