Muhammadidrees commited on
Commit
0109f78
·
verified ·
1 Parent(s): 0849418

Update DocVoice.py

Browse files
Files changed (1) hide show
  1. DocVoice.py +54 -16
DocVoice.py CHANGED
@@ -1,16 +1,54 @@
1
- import pyttsx3
2
-
3
- def text_to_speech(text: str):
4
- # Initialize engine
5
- engine = pyttsx3.init()
6
-
7
- # Use default voice
8
- engine.setProperty('voice', engine.getProperty('voices')[0].id)
9
-
10
- # Speak the text
11
- engine.say(text)
12
- engine.runAndWait()
13
-
14
- # Example usage
15
- if __name__ == "__main__":
16
- text_to_speech("Hello Abdul Moiz! This is your Riaya Tech project speaking.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")