Spaces:
Sleeping
Sleeping
File size: 3,122 Bytes
e62cec6 cc85380 e62cec6 cc85380 e62cec6 cc85380 e62cec6 cc85380 e62cec6 cc85380 55862dc cc85380 e62cec6 cc85380 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import torch
from transformers import VitsModel, AutoTokenizer
import torchaudio
import os
from gtts import gTTS
def generate_audio(text, filename="output.mp3", gender="female", speed="normal"):
"""
Convert text to speech and save it as an audio file.
Parameters:
text (str): The text to convert.
filename (str): The output file name.
gender (str): "male" (use MMS-TTS) or "female" (use gTTS).
speed (str): "slow", "normal", or "fast" (only for gTTS).
"""
print("\n[DEBUG] Function: generate_audio")
print(f"Received parameters -> text: {text[:50]}..., filename: {filename}, gender: {gender}, speed: {speed}")
lang = "vi"
if gender.lower() == "female":
print("[DEBUG] Using gTTS for female voice...")
speed_mapping = {"slow": True, "normal": False, "fast": False}
slow = speed_mapping.get(speed.lower(), False)
try:
tts = gTTS(text=text, lang=lang, slow=slow)
tts.save(filename)
print(f"β
Audio saved as {filename}")
except Exception as e:
print(f"[ERROR] Failed to generate audio: {e}")
else:
print("[ERROR] Male voice generation not implemented yet!")
# Debug check for file existence
if os.path.exists(filename):
print(f"β
Verified: {filename} exists.")
else:
print(f"[ERROR] {filename} was not created.")
import os
def text_to_speech(gender, speed):
"""
Convert text files in the folder to speech and save as audio files.
Parameters:
gender (str): "male" (use MMS-TTS) or "female" (use gTTS).
speed (str): "slow", "normal", or "fast" (only for gTTS).
"""
print("\n[DEBUG] Function: text_to_speech")
print(f"Received parameters -> gender: {gender}, speed: {speed}")
text_folder = "./"
print(f"[DEBUG] Looking for text files in: {os.path.abspath(text_folder)}")
# Get all valid text files (excluding specific ones)
text_files = sorted([
f for f in os.listdir(text_folder)
if f.endswith('.txt') and f not in ["text.txt", "requirements.txt"]
])
print(f"[DEBUG] Found text files: {text_files}")
if not text_files:
print("[WARNING] No text files found for conversion.")
for text_file in text_files:
file_path = os.path.join(text_folder, text_file)
try:
print(f"[DEBUG] Reading file: {file_path}")
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
audio_file = text_file.replace("txt", "mp3")
print(f"[DEBUG] Generating audio for: {text_file} -> {audio_file}")
generate_audio(content, audio_file, gender=gender, speed=speed)
# Verify if audio was created
if os.path.exists(audio_file):
print(f"β
Audio file created: {audio_file}")
else:
print(f"[ERROR] Audio file {audio_file} was not created!")
except Exception as e:
print(f"[ERROR] Failed to process {text_file}: {e}")
|