Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import uuid
|
3 |
+
import os
|
4 |
+
import gzip
|
5 |
+
import subprocess
|
6 |
+
import datetime
|
7 |
+
import time
|
8 |
+
import soundfile as sf
|
9 |
+
|
10 |
+
from app_utils import tts_interface, models
|
11 |
+
|
12 |
+
# === Only keep the 'برنا' model ===
|
13 |
+
borna_model = next(model for model in models if "برنا" in model[0] or "برنا" in model[2])
|
14 |
+
voice_id = borna_model[2]
|
15 |
+
voice_url = borna_model[3]
|
16 |
+
|
17 |
+
# === Paths ===
|
18 |
+
STATIC_DIR = "static"
|
19 |
+
os.makedirs(STATIC_DIR, exist_ok=True)
|
20 |
+
PHONETIC_FILE = os.path.join(STATIC_DIR, "fa_extra.txt.gz")
|
21 |
+
|
22 |
+
# === TTS Function ===
|
23 |
+
def tts(text):
|
24 |
+
# Save input text for phonemizer
|
25 |
+
input_file = os.path.join(STATIC_DIR, "input.txt")
|
26 |
+
with open(input_file, "w", encoding="utf-8") as f:
|
27 |
+
f.write(text.strip())
|
28 |
+
|
29 |
+
# Run espeak-ng to get phonemes
|
30 |
+
phonemes_result = subprocess.run(
|
31 |
+
["espeak-ng", "-v", "fa", "-x", "-q", "-f", input_file],
|
32 |
+
capture_output=True,
|
33 |
+
text=True
|
34 |
+
)
|
35 |
+
phonemes = phonemes_result.stdout.strip()
|
36 |
+
|
37 |
+
# Run TTS
|
38 |
+
(sample_rate, audio_data), _ = tts_interface(voice_id, text.strip(), '')
|
39 |
+
out_path = os.path.join(STATIC_DIR, f"{uuid.uuid4().hex}.wav")
|
40 |
+
sf.write(out_path, audio_data, samplerate=sample_rate, subtype="PCM_16")
|
41 |
+
|
42 |
+
status = f"مدل: {voice_url}\nآوانگاشت: {phonemes}"
|
43 |
+
return out_path, status
|
44 |
+
|
45 |
+
# === Phonemizer + Save ===
|
46 |
+
def phonemize(word, phonetic, task):
|
47 |
+
if task == "phonemize" or not phonetic.strip():
|
48 |
+
phoneme_target = word
|
49 |
+
else:
|
50 |
+
phoneme_target = f"[[{phonetic.strip()}]]"
|
51 |
+
|
52 |
+
input_file = os.path.join(STATIC_DIR, "input.txt")
|
53 |
+
with open(input_file, "w", encoding="utf-8") as f:
|
54 |
+
f.write(phoneme_target)
|
55 |
+
|
56 |
+
result = subprocess.run(
|
57 |
+
["espeak-ng", "-v", "fa", "-x", "-q", "-f", input_file],
|
58 |
+
capture_output=True,
|
59 |
+
text=True
|
60 |
+
)
|
61 |
+
phonemes = result.stdout.strip()
|
62 |
+
|
63 |
+
out_path = os.path.join(STATIC_DIR, f"{uuid.uuid4().hex}.wav")
|
64 |
+
subprocess.run(f'espeak-ng -v fa -w "{out_path}" -f "{input_file}"', shell=True)
|
65 |
+
|
66 |
+
if task == "send" and phonetic.strip():
|
67 |
+
with gzip.open(PHONETIC_FILE, "at", encoding="utf-8") as f:
|
68 |
+
f.write(f"{word.strip()}\t{phonetic.strip()}\n")
|
69 |
+
|
70 |
+
status = f"متن: {word}\nآوانگاشت: {phonemes}"
|
71 |
+
return out_path, phonemes, status
|
72 |
+
|
73 |
+
# === List saved words ===
|
74 |
+
def list_words():
|
75 |
+
entries = []
|
76 |
+
with gzip.open(PHONETIC_FILE, "rt", encoding="utf-8") as f:
|
77 |
+
for line in f:
|
78 |
+
if not line.startswith("//"):
|
79 |
+
parts = line.strip().split("\t")
|
80 |
+
if len(parts) == 2:
|
81 |
+
entries.append((parts[0], parts[1]))
|
82 |
+
return entries
|
83 |
+
|
84 |
+
# === Iran time without pytz ===
|
85 |
+
def get_iran_time():
|
86 |
+
offset = 3.5 * 3600
|
87 |
+
utc = time.gmtime()
|
88 |
+
iran_time = time.localtime(time.mktime(utc) + offset)
|
89 |
+
return datetime.datetime(*iran_time[:6]).strftime("%Y-%m-%d %H:%M:%S")
|
90 |
+
|
91 |
+
# Log start
|
92 |
+
with gzip.open(PHONETIC_FILE, "at", encoding="utf-8") as f:
|
93 |
+
f.write(f"// started at : {get_iran_time()}\n")
|
94 |
+
|
95 |
+
# === UI ===
|
96 |
+
|
97 |
+
with gr.Blocks(title="TTS for Persian with Bornaa") as demo:
|
98 |
+
gr.Markdown("## متن به گفتار 🗣️")
|
99 |
+
|
100 |
+
with gr.Tab("تبدیل متن به گفتار"):
|
101 |
+
text_input = gr.Textbox(label="متن فارسی", lines=2)
|
102 |
+
out_audio = gr.Audio(label="خروجی صوتی")
|
103 |
+
out_status = gr.Textbox(label="وضعیت مدل", interactive=False)
|
104 |
+
|
105 |
+
btn_tts = gr.Button("تبدیل کن")
|
106 |
+
btn_tts.click(tts, inputs=text_input, outputs=[out_audio, out_status])
|
107 |
+
|
108 |
+
with gr.Tab("ویرایش تلفظ"):
|
109 |
+
word_input = gr.Textbox(label="کلمه", placeholder="مثلاً: سلام")
|
110 |
+
phonetic_input = gr.Textbox(label="آوانویسی دلخواه (اختیاری)")
|
111 |
+
task_choice = gr.Radio(label="عملیات", choices=["phonemize", "send"], value="phonemize")
|
112 |
+
|
113 |
+
ph_audio = gr.Audio(label="صدای تولیدشده")
|
114 |
+
ph_output = gr.Textbox(label="آوانگاشت")
|
115 |
+
ph_status = gr.Textbox(label="وضعیت", interactive=False)
|
116 |
+
|
117 |
+
btn_phonemize = gr.Button("انجام")
|
118 |
+
btn_phonemize.click(phonemize,
|
119 |
+
inputs=[word_input, phonetic_input, task_choice],
|
120 |
+
outputs=[ph_audio, ph_output, ph_status])
|
121 |
+
|
122 |
+
with gr.Tab("واژههای اخیر"):
|
123 |
+
gr.Markdown("### واژههای ذخیرهشده")
|
124 |
+
word_table = gr.Dataframe(headers=["کلمه", "آوانویسی"], datatype=["str", "str"])
|
125 |
+
btn_load_words = gr.Button("بارگذاری")
|
126 |
+
btn_load_words.click(fn=list_words, inputs=[], outputs=[word_table])
|
127 |
+
|
128 |
+
demo.launch()
|