Spaces:
Sleeping
Sleeping
Update DocVoice.py
Browse files- DocVoice.py +54 -16
DocVoice.py
CHANGED
|
@@ -1,16 +1,54 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DocVoice.py
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# -------------------
|
| 6 |
+
# 1️⃣ Detect GPU
|
| 7 |
+
# -------------------
|
| 8 |
+
use_cuda = torch.cuda.is_available()
|
| 9 |
+
device_index = 0 if use_cuda else -1
|
| 10 |
+
device_str = "cuda" if use_cuda else "cpu"
|
| 11 |
+
dtype = torch.float16 if use_cuda else torch.float32
|
| 12 |
+
|
| 13 |
+
# -------------------
|
| 14 |
+
# 2️⃣ Load TTS model from Hugging Face
|
| 15 |
+
# -------------------
|
| 16 |
+
tts_model_id = "espnet/kan-bayashi_ljspeech_vits" # Example TTS model, English voice
|
| 17 |
+
|
| 18 |
+
tts_pipe = pipeline(
|
| 19 |
+
"text-to-speech",
|
| 20 |
+
model=tts_model_id,
|
| 21 |
+
device=device_index,
|
| 22 |
+
torch_dtype=dtype
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
print("🔊 TTS pipeline ready using Hugging Face.")
|
| 26 |
+
|
| 27 |
+
# -------------------
|
| 28 |
+
# 3️⃣ TTS Helper Function
|
| 29 |
+
# -------------------
|
| 30 |
+
def text_to_speech(text: str, filename="assistant_response.wav"):
|
| 31 |
+
"""
|
| 32 |
+
Generate speech from text and save as WAV file.
|
| 33 |
+
"""
|
| 34 |
+
if not text.strip():
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
print(f"📝 Generating audio for: {text}")
|
| 38 |
+
|
| 39 |
+
# Generate audio
|
| 40 |
+
speech_array = tts_pipe(text)["audio"]
|
| 41 |
+
|
| 42 |
+
# Convert to int16 and save as WAV
|
| 43 |
+
import numpy as np
|
| 44 |
+
import scipy.io.wavfile as wav
|
| 45 |
+
|
| 46 |
+
wav.write(filename, 22050, (speech_array * 32767).astype(np.int16))
|
| 47 |
+
print(f"✅ Audio saved as {filename}")
|
| 48 |
+
|
| 49 |
+
# Optional: play audio automatically (requires sounddevice)
|
| 50 |
+
try:
|
| 51 |
+
import sounddevice as sd
|
| 52 |
+
sd.play(speech_array, samplerate=22050)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"⚠️ Could not play audio automatically: {e}")
|