Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from piper import PiperVoice
|
|
| 7 |
from transformers import pipeline
|
| 8 |
import hazm
|
| 9 |
import typing
|
|
|
|
| 10 |
|
| 11 |
normalizer = hazm.Normalizer()
|
| 12 |
sent_tokenizer = hazm.SentenceTokenizer()
|
|
@@ -44,11 +45,17 @@ def fix_words(words: typing.List[str]) -> typing.List[str]:
|
|
| 44 |
return fixed_words
|
| 45 |
|
| 46 |
def synthesize_speech(text):
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Create an in-memory buffer for the WAV file
|
| 50 |
-
|
| 51 |
-
with wave.open(buffer, 'wb') as wav_file:
|
| 52 |
wav_file.setframerate(voice.config.sample_rate)
|
| 53 |
wav_file.setsampwidth(2) # 16-bit
|
| 54 |
wav_file.setnchannels(1) # mono
|
|
@@ -57,18 +64,21 @@ def synthesize_speech(text):
|
|
| 57 |
eztext = preprocess_text(text)
|
| 58 |
voice.synthesize(eztext, wav_file)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
buffer.seek(0)
|
| 62 |
-
audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
|
| 63 |
-
|
| 64 |
-
return audio_data.tobytes()
|
| 65 |
|
| 66 |
# Using Gradio Blocks
|
| 67 |
with gr.Blocks(theme=gr.themes.Base()) as blocks:
|
| 68 |
input_text = gr.Textbox(label="Input")
|
| 69 |
-
|
| 70 |
submit_button = gr.Button("Synthesize")
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Run the app
|
| 74 |
-
blocks.launch()
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
import hazm
|
| 9 |
import typing
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
normalizer = hazm.Normalizer()
|
| 13 |
sent_tokenizer = hazm.SentenceTokenizer()
|
|
|
|
| 45 |
return fixed_words
|
| 46 |
|
| 47 |
def synthesize_speech(text):
|
| 48 |
+
# Define the directory to save audio files
|
| 49 |
+
save_dir = "audio_files"
|
| 50 |
+
if not os.path.exists(save_dir):
|
| 51 |
+
os.makedirs(save_dir)
|
| 52 |
+
|
| 53 |
+
# Create a unique file name
|
| 54 |
+
file_name = f"audio_{np.random.randint(1000, 9999)}.wav"
|
| 55 |
+
file_path = os.path.join(save_dir, file_name)
|
| 56 |
|
| 57 |
# Create an in-memory buffer for the WAV file
|
| 58 |
+
with wave.open(file_path, 'wb') as wav_file:
|
|
|
|
| 59 |
wav_file.setframerate(voice.config.sample_rate)
|
| 60 |
wav_file.setsampwidth(2) # 16-bit
|
| 61 |
wav_file.setnchannels(1) # mono
|
|
|
|
| 64 |
eztext = preprocess_text(text)
|
| 65 |
voice.synthesize(eztext, wav_file)
|
| 66 |
|
| 67 |
+
return file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Using Gradio Blocks
|
| 70 |
with gr.Blocks(theme=gr.themes.Base()) as blocks:
|
| 71 |
input_text = gr.Textbox(label="Input")
|
| 72 |
+
output_link = gr.Textbox(label="Output Link", type="auto", readonly=True)
|
| 73 |
submit_button = gr.Button("Synthesize")
|
| 74 |
|
| 75 |
+
def synthesize_and_get_link(text):
|
| 76 |
+
file_path = synthesize_speech(text)
|
| 77 |
+
return file_path
|
| 78 |
+
|
| 79 |
+
def update_output_link(file_path):
|
| 80 |
+
return file_path
|
| 81 |
+
|
| 82 |
+
submit_button.click(synthesize_and_get_link, inputs=input_text, outputs=update_output_link)
|
| 83 |
# Run the app
|
| 84 |
+
blocks.launch()
|