Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,43 +5,12 @@ from io import BytesIO
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
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()
|
| 13 |
-
word_tokenizer = hazm.WordTokenizer()
|
| 14 |
-
|
| 15 |
-
tagger_path = hf_hub_download(repo_id="gyroing/HAZM_POS_TAGGER", filename="pos_tagger.model")
|
| 16 |
-
tagger = hazm.POSTagger(model=tagger_path)
|
| 17 |
model_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx")
|
| 18 |
config_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx.json")
|
| 19 |
voice = PiperVoice.load(model_path, config_path)
|
| 20 |
|
| 21 |
-
def preprocess_text(text: str) -> typing.List[typing.List[str]]:
|
| 22 |
-
"""Split/normalize text into sentences/words with hazm"""
|
| 23 |
-
text = normalizer.normalize(text)
|
| 24 |
-
processed_sentences = []
|
| 25 |
-
|
| 26 |
-
for sentence in sent_tokenizer.tokenize(text):
|
| 27 |
-
words = word_tokenizer.tokenize(sentence)
|
| 28 |
-
processed_words = fix_words(words)
|
| 29 |
-
processed_sentences.append(" ".join(processed_words))
|
| 30 |
-
return " ".join(processed_sentences)
|
| 31 |
-
def fix_words(words: typing.List[str]) -> typing.List[str]:
|
| 32 |
-
fixed_words = []
|
| 33 |
-
|
| 34 |
-
for word, pos in tagger.tag(words):
|
| 35 |
-
if pos[-1] == "Z":
|
| 36 |
-
if word[-1] != "ِ":
|
| 37 |
-
if (word[-1] == "ه") and (word[-2] != "ا"):
|
| 38 |
-
word += "ی"
|
| 39 |
-
word += "ِ"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
fixed_words.append(word)
|
| 43 |
-
|
| 44 |
-
return fixed_words
|
| 45 |
|
| 46 |
def synthesize_speech(text):
|
| 47 |
|
|
@@ -54,8 +23,8 @@ def synthesize_speech(text):
|
|
| 54 |
wav_file.setnchannels(1) # mono
|
| 55 |
|
| 56 |
# Synthesize speech
|
| 57 |
-
eztext = preprocess_text(text)
|
| 58 |
-
voice.synthesize(
|
| 59 |
|
| 60 |
# Convert buffer to NumPy array for Gradio output
|
| 61 |
buffer.seek(0)
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from piper import PiperVoice
|
| 7 |
from transformers import pipeline
|
|
|
|
| 8 |
import typing
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
model_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx")
|
| 11 |
config_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx.json")
|
| 12 |
voice = PiperVoice.load(model_path, config_path)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def synthesize_speech(text):
|
| 16 |
|
|
|
|
| 23 |
wav_file.setnchannels(1) # mono
|
| 24 |
|
| 25 |
# Synthesize speech
|
| 26 |
+
# eztext = preprocess_text(text)
|
| 27 |
+
voice.synthesize(text, wav_file)
|
| 28 |
|
| 29 |
# Convert buffer to NumPy array for Gradio output
|
| 30 |
buffer.seek(0)
|