Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import gradio as gr
|
|
| 5 |
from docx import Document
|
| 6 |
from TTS.api import TTS
|
| 7 |
import tempfile
|
|
|
|
| 8 |
|
| 9 |
# Voice model
|
| 10 |
VOICE_MODEL = "tts_models/en/vctk/vits"
|
|
@@ -135,6 +136,40 @@ def docx_to_wav(doc_file, selected_desc):
|
|
| 135 |
tts.tts_to_file(text=full_text, file_path=wav_path, speaker=speaker_id)
|
| 136 |
return wav_path
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Gradio UI
|
| 139 |
with gr.Blocks() as interface:
|
| 140 |
gr.Markdown("# 🎤 English Voice Generator from DOCX")
|
|
@@ -151,10 +186,10 @@ with gr.Blocks() as interface:
|
|
| 151 |
output_audio = gr.Audio(label="Generated Audio", type="filepath")
|
| 152 |
|
| 153 |
generate_btn.click(
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
interface.launch()
|
|
|
|
| 5 |
from docx import Document
|
| 6 |
from TTS.api import TTS
|
| 7 |
import tempfile
|
| 8 |
+
import zipfile
|
| 9 |
|
| 10 |
# Voice model
|
| 11 |
VOICE_MODEL = "tts_models/en/vctk/vits"
|
|
|
|
| 136 |
tts.tts_to_file(text=full_text, file_path=wav_path, speaker=speaker_id)
|
| 137 |
return wav_path
|
| 138 |
|
| 139 |
+
|
| 140 |
+
def docx_to_zipped_wavs(doc_file, selected_desc):
|
| 141 |
+
speaker_id = next((sid for desc, sid in get_speaker_dropdown_choices() if desc == selected_desc), None)
|
| 142 |
+
if not speaker_id:
|
| 143 |
+
raise ValueError("Invalid speaker selection")
|
| 144 |
+
|
| 145 |
+
tts = load_tts_model()
|
| 146 |
+
document = Document(doc_file.name)
|
| 147 |
+
paragraphs = [p.text.strip() for p in document.paragraphs if p.text.strip()]
|
| 148 |
+
|
| 149 |
+
if not paragraphs:
|
| 150 |
+
raise ValueError("No non-empty paragraphs found in the document.")
|
| 151 |
+
|
| 152 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 153 |
+
wav_paths = []
|
| 154 |
+
for i, para in enumerate(paragraphs, start=1):
|
| 155 |
+
wav_path = os.path.join(temp_dir, f"chunk_{i:02d}.wav")
|
| 156 |
+
tts.tts_to_file(text=para, file_path=wav_path, speaker=speaker_id)
|
| 157 |
+
wav_paths.append(wav_path)
|
| 158 |
+
|
| 159 |
+
# Create a zip file
|
| 160 |
+
zip_path = os.path.join(temp_dir, "voice_chunks.zip")
|
| 161 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
| 162 |
+
for wav in wav_paths:
|
| 163 |
+
zipf.write(wav, os.path.basename(wav))
|
| 164 |
+
|
| 165 |
+
# Copy zip to a final temp file for Gradio to return
|
| 166 |
+
final_zip = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)
|
| 167 |
+
with open(zip_path, "rb") as src, open(final_zip.name, "wb") as dst:
|
| 168 |
+
dst.write(src.read())
|
| 169 |
+
|
| 170 |
+
return final_zip.name
|
| 171 |
+
|
| 172 |
+
|
| 173 |
# Gradio UI
|
| 174 |
with gr.Blocks() as interface:
|
| 175 |
gr.Markdown("# 🎤 English Voice Generator from DOCX")
|
|
|
|
| 186 |
output_audio = gr.Audio(label="Generated Audio", type="filepath")
|
| 187 |
|
| 188 |
generate_btn.click(
|
| 189 |
+
fn=docx_to_zipped_wavs,
|
| 190 |
+
inputs=[doc_input, speaker_dropdown],
|
| 191 |
+
outputs=gr.File(label="Download ZIP of Audio Files")
|
| 192 |
+
)
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
interface.launch()
|