Spaces:
Runtime error
Runtime error
Commit
·
33051d3
1
Parent(s):
9a433d2
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,67 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from pdfminer.high_level import extract_text
|
| 3 |
-
from balacoon_tts import TTS
|
| 4 |
from huggingface_hub import hf_hub_download, list_repo_files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def read_pdf(file):
|
| 7 |
-
|
| 8 |
-
text = extract_text(f)
|
| 9 |
return text
|
| 10 |
|
| 11 |
-
def set_model(model_name_str):
|
| 12 |
-
model_path = hf_hub_download(repo_id="balacoon/tts", filename=model_name_str)
|
| 13 |
-
global tts
|
| 14 |
-
tts = TTS(model_path)
|
| 15 |
-
speakers = tts.get_speakers()
|
| 16 |
-
speaker_dropdown.choices = speakers
|
| 17 |
-
speaker_dropdown.update(default=speakers[0] if speakers else None)
|
| 18 |
-
|
| 19 |
def synthesize_audio(file, model_name_str, speaker_str):
|
| 20 |
text_str = read_pdf(file)
|
| 21 |
if len(text_str) > 1024:
|
| 22 |
text_str = text_str[:1024]
|
| 23 |
samples = tts.synthesize(text_str, speaker_str)
|
| 24 |
-
return (tts.get_sampling_rate()
|
| 25 |
|
| 26 |
def main():
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
|
| 37 |
-
|
| 38 |
-
fn=synthesize_audio,
|
| 39 |
-
inputs=[file_input, model_name_dropdown, speaker_dropdown],
|
| 40 |
-
outputs=audio,
|
| 41 |
-
title="PDF TO SPEECH CONVERTER",
|
| 42 |
-
layout="rows",
|
| 43 |
-
debug=True
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
main()
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
from typing import cast
|
| 3 |
from pdfminer.high_level import extract_text
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download, list_repo_files
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from balacoon_tts import TTS
|
| 7 |
+
|
| 8 |
+
# Global TTS module, initialized from a selected model
|
| 9 |
+
tts = None
|
| 10 |
|
| 11 |
def read_pdf(file):
|
| 12 |
+
text = extract_text(file.name)
|
|
|
|
| 13 |
return text
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def synthesize_audio(file, model_name_str, speaker_str):
|
| 16 |
text_str = read_pdf(file)
|
| 17 |
if len(text_str) > 1024:
|
| 18 |
text_str = text_str[:1024]
|
| 19 |
samples = tts.synthesize(text_str, speaker_str)
|
| 20 |
+
return (samples, tts.get_sampling_rate())
|
| 21 |
|
| 22 |
def main():
|
| 23 |
+
logging.basicConfig(level=logging.INFO)
|
| 24 |
+
with gr.Interface(fn=None, title="PDF TO SPEECH CONVERTER", layout="rows", debug=True) as iface:
|
| 25 |
+
gr.Markdown(
|
| 26 |
+
"""
|
| 27 |
+
<h1 align="center">PDF TO SPEECH CONVERTER</h1>
|
| 28 |
+
1. Insert a PDF
|
| 29 |
+
2. Select the model to synthesize with
|
| 30 |
+
3. Select speaker
|
| 31 |
+
4. Hit "Generate" and listen to the result!
|
| 32 |
+
When you select a model for the first time, it may take some time to download it.
|
| 33 |
+
This project is designed to bring the joy of reading without the hassle of looking over.
|
| 34 |
+
If you want an audiobook, you've got it!
|
| 35 |
+
"""
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
file_input = gr.inputs.File(label="Upload PDF")
|
| 39 |
+
|
| 40 |
+
model_files = list_repo_files(repo_id="balacoon/tts")
|
| 41 |
+
model_name_dropdown = gr.inputs.Dropdown(label="Model", choices=model_files)
|
| 42 |
|
| 43 |
+
def set_model(model_name_str: str):
|
| 44 |
+
model_path = hf_hub_download(repo_id="balacoon/tts", filename=model_name_str)
|
| 45 |
+
global tts
|
| 46 |
+
tts = TTS(model_path)
|
| 47 |
+
speakers = tts.get_speakers()
|
| 48 |
+
default_speaker = speakers[-1]
|
| 49 |
+
return speakers, default_speaker
|
| 50 |
|
| 51 |
+
model_name_dropdown.set_action(set_model)
|
| 52 |
|
| 53 |
+
speaker_dropdown = gr.inputs.Dropdown(label="Speaker", choices=[])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
audio = gr.outputs.Audio(label="Generated Audio", type="numpy")
|
| 56 |
|
| 57 |
+
def generate_audio(file, model_name_str, speaker_str):
|
| 58 |
+
return synthesize_audio(file, model_name_str, speaker_str)
|
| 59 |
|
| 60 |
+
iface.add_input(file_input)
|
| 61 |
+
iface.add_input(model_name_dropdown)
|
| 62 |
+
iface.add_input(speaker_dropdown)
|
| 63 |
+
iface.add_output(audio)
|
| 64 |
+
iface.run()
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
main()
|