Spaces:
Paused
Paused
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Piper TTS โ Hugging Face Space (CPU Free Tier)
|
| 3 |
+
ูุนู
ู ุนูู CPU ุงูู
ุฌุงูู ุจุฏูู GPU
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import subprocess
|
| 7 |
+
import tempfile
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# โโ ุชุญู
ูู ุงูุฃุตูุงุช ุนูุฏ ุจุฏุก ุงูุชุดุบูู โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 11 |
+
os.makedirs("voices", exist_ok=True)
|
| 12 |
+
|
| 13 |
+
VOICES = {
|
| 14 |
+
"ุนุฑุจู ๐ฏ๐ด (ูุฑูู
)": {
|
| 15 |
+
"model": "voices/ar.onnx",
|
| 16 |
+
"url_onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/ar/ar_JO/kareem/medium/ar_JO-kareem-medium.onnx",
|
| 17 |
+
"url_json": "https://huggingface.co/rhasspy/piper-voices/resolve/main/ar/ar_JO/kareem/medium/ar_JO-kareem-medium.onnx.json",
|
| 18 |
+
},
|
| 19 |
+
"English ๐บ๐ธ (Lessac)": {
|
| 20 |
+
"model": "voices/en.onnx",
|
| 21 |
+
"url_onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/en_US-lessac-medium.onnx",
|
| 22 |
+
"url_json": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_US/lessac/medium/en_US-lessac-medium.onnx.json",
|
| 23 |
+
},
|
| 24 |
+
"English ๐ฌ๐ง (Alan)": {
|
| 25 |
+
"model": "voices/en_gb.onnx",
|
| 26 |
+
"url_onnx": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_GB/alan/medium/en_GB-alan-medium.onnx",
|
| 27 |
+
"url_json": "https://huggingface.co/rhasspy/piper-voices/resolve/main/en/en_GB/alan/medium/en_GB-alan-medium.onnx.json",
|
| 28 |
+
},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def download_voice(info: dict):
|
| 32 |
+
onnx = info["model"]
|
| 33 |
+
json = onnx + ".json"
|
| 34 |
+
if not os.path.exists(onnx):
|
| 35 |
+
print(f"โณ Downloading {onnx}...")
|
| 36 |
+
subprocess.run(["wget", "-q", "-O", onnx, info["url_onnx"]], check=True)
|
| 37 |
+
subprocess.run(["wget", "-q", "-O", json, info["url_json"]], check=True)
|
| 38 |
+
print(f"โ
{onnx} ready!")
|
| 39 |
+
|
| 40 |
+
# ุชุญู
ูู ูู ุงูุฃุตูุงุช ุนูุฏ ุงูุจุฏุงูุฉ
|
| 41 |
+
for name, info in VOICES.items():
|
| 42 |
+
try:
|
| 43 |
+
download_voice(info)
|
| 44 |
+
except Exception as e:
|
| 45 |
+
print(f"โ ๏ธ Could not download {name}: {e}")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# โโ ุฏุงูุฉ ุงูุชูููุฏ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 49 |
+
def synthesize(text: str, voice_name: str):
|
| 50 |
+
if not text.strip():
|
| 51 |
+
raise gr.Error("โ ๏ธ ู
ู ูุถูู ุงูุชุจ ูุตุงู ุฃููุงู!")
|
| 52 |
+
|
| 53 |
+
info = VOICES[voice_name]
|
| 54 |
+
model_path = info["model"]
|
| 55 |
+
|
| 56 |
+
if not os.path.exists(model_path):
|
| 57 |
+
raise gr.Error(f"โ ๏ธ ุงูุตูุช ุบูุฑ ู
ุชุงุญุ ุญุงูู ู
ุฑุฉ ุฃุฎุฑู ุจุนุฏ ูููู.")
|
| 58 |
+
|
| 59 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 60 |
+
output_path = tmp.name
|
| 61 |
+
|
| 62 |
+
result = subprocess.run(
|
| 63 |
+
f'echo "{text}" | python -m piper --model {model_path} --output_file {output_path}',
|
| 64 |
+
shell=True,
|
| 65 |
+
capture_output=True,
|
| 66 |
+
text=True,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if result.returncode != 0 or not os.path.exists(output_path):
|
| 70 |
+
raise gr.Error(f"ุฎุทุฃ ูู ุงูุชูููุฏ: {result.stderr[:200]}")
|
| 71 |
+
|
| 72 |
+
return output_path
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# โโ ูุงุฌูุฉ Gradio โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 76 |
+
css = """
|
| 77 |
+
.gradio-container { max-width: 750px !important; margin: 0 auto !important; }
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
EXAMPLES = [
|
| 81 |
+
["ู
ุฑุญุจุงู ุจู ูู ุนุงูู
ุงูุฐูุงุก ุงูุงุตุทูุงุนู!", "ุนุฑุจู ๐ฏ๐ด (ูุฑูู
)"],
|
| 82 |
+
["ุงูููู
ุทูุณ ุฌู
ูู ูุงูุณู
ุงุก ุตุงููุฉ ูุฒุฑูุงุก.", "ุนุฑุจู ๐ฏ๐ด (ูุฑูู
)"],
|
| 83 |
+
["Hello! Welcome to Piper text to speech.", "English ๐บ๐ธ (Lessac)"],
|
| 84 |
+
["The quick brown fox jumps over the lazy dog.", "English ๐ฌ๐ง (Alan)"],
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(css=css, title="Piper TTS") as demo:
|
| 88 |
+
gr.Markdown("# ๐ Piper TTS โ ูุต ุฅูู ุตูุช")
|
| 89 |
+
gr.Markdown(
|
| 90 |
+
"ุญููู ุฃู ูุต ุนุฑุจู ุฃู ุฅูุฌููุฒู ุฅูู ููุงู
ููุฑุงู โ "
|
| 91 |
+
"ูุนู
ู ู
ุญููุงู ุจุฏูู ุฅูุชุฑูุช ูุจุณุฑุนุฉ ูุงุฆูุฉ! โก"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Row():
|
| 95 |
+
with gr.Column():
|
| 96 |
+
voice_dd = gr.Dropdown(
|
| 97 |
+
choices=list(VOICES.keys()),
|
| 98 |
+
value="ุนุฑุจู ๐ฏ๐ด (ูุฑูู
)",
|
| 99 |
+
label="ุงูุตูุช",
|
| 100 |
+
)
|
| 101 |
+
text_in = gr.Textbox(
|
| 102 |
+
label="ุงููุต",
|
| 103 |
+
placeholder="ุงูุชุจ ููุง...",
|
| 104 |
+
lines=5,
|
| 105 |
+
value="ู
ุฑุญุจุงูุ ูุฐุง ุงุฎุชุจุงุฑ ูู
ูุชุจุฉ ุจุงูุจุฑ ูุชุญููู ุงููุต ุฅูู ููุงู
.",
|
| 106 |
+
)
|
| 107 |
+
btn = gr.Button("๐ ุชูููุฏ ุงูุตูุช", variant="primary", size="lg")
|
| 108 |
+
|
| 109 |
+
with gr.Column():
|
| 110 |
+
audio_out = gr.Audio(
|
| 111 |
+
label="ุงูุตูุช ุงูู
ููููุฏ",
|
| 112 |
+
type="filepath",
|
| 113 |
+
autoplay=True,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
btn.click(
|
| 117 |
+
fn=synthesize,
|
| 118 |
+
inputs=[text_in, voice_dd],
|
| 119 |
+
outputs=audio_out,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
gr.Examples(
|
| 123 |
+
examples=EXAMPLES,
|
| 124 |
+
inputs=[text_in, voice_dd],
|
| 125 |
+
outputs=audio_out,
|
| 126 |
+
fn=synthesize,
|
| 127 |
+
cache_examples=True,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
gr.Markdown(
|
| 131 |
+
"### ๐ก ูุตุงุฆุญ\n"
|
| 132 |
+
"- ุงูุตูุช ููููููุฏ ูู **ุฃูู ู
ู ุซุงููุฉ** โก\n"
|
| 133 |
+
"- ูุฏุนู
ุงููุตูุต ุงูุทูููุฉ\n"
|
| 134 |
+
"- ูู
ููู ุชุญู
ูู ุงูุตูุช ุจุงูุถุบุท ุนูู โฌ๏ธ"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
demo.queue(max_size=5).launch()
|