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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from TTS.utils.synthesizer import Synthesizer
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# Uncomment for private models if needed
|
| 9 |
+
# login(token=os.environ.get("HF_TOKEN"))
|
| 10 |
+
|
| 11 |
+
# Custom CSS for better styling
|
| 12 |
+
custom_css = """
|
| 13 |
+
.gradio-container {
|
| 14 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 15 |
+
font-family: 'Vazirmatn', 'Tahoma', sans-serif;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
.main-header {
|
| 19 |
+
color: #2d3748;
|
| 20 |
+
text-align: center;
|
| 21 |
+
margin-bottom: 2rem;
|
| 22 |
+
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.container {
|
| 26 |
+
max-width: 900px;
|
| 27 |
+
margin: 0 auto;
|
| 28 |
+
padding: 20px;
|
| 29 |
+
background-color: white;
|
| 30 |
+
border-radius: 12px;
|
| 31 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
.footer {
|
| 35 |
+
text-align: center;
|
| 36 |
+
margin-top: 2rem;
|
| 37 |
+
color: #4a5568;
|
| 38 |
+
font-size: 0.9rem;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
/* Persian text alignment */
|
| 42 |
+
textarea, .label {
|
| 43 |
+
text-align: right;
|
| 44 |
+
direction: rtl;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/* Button styling */
|
| 48 |
+
button.primary {
|
| 49 |
+
background: linear-gradient(to right, #4776E6, #8E54E9);
|
| 50 |
+
border: none;
|
| 51 |
+
border-radius: 8px;
|
| 52 |
+
color: white;
|
| 53 |
+
font-weight: bold;
|
| 54 |
+
transition: all 0.3s ease;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
button.primary:hover {
|
| 58 |
+
transform: translateY(-2px);
|
| 59 |
+
box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08);
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
.input-panel, .output-panel {
|
| 63 |
+
background-color: rgba(255, 255, 255, 0.9);
|
| 64 |
+
border-radius: 10px;
|
| 65 |
+
padding: 15px;
|
| 66 |
+
margin-bottom: 15px;
|
| 67 |
+
border: 1px solid #e2e8f0;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.examples-panel {
|
| 71 |
+
background-color: rgba(255, 255, 255, 0.8);
|
| 72 |
+
border-radius: 10px;
|
| 73 |
+
padding: 10px;
|
| 74 |
+
border: 1px solid #e2e8f0;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.status-panel {
|
| 78 |
+
background-color: #edf2f7;
|
| 79 |
+
border-radius: 8px;
|
| 80 |
+
padding: 10px;
|
| 81 |
+
margin-bottom: 15px;
|
| 82 |
+
text-align: center;
|
| 83 |
+
}
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
def load_synthesizer():
|
| 87 |
+
# Status for loading
|
| 88 |
+
status_block.update("در حال بارگذاری مدل... لطفاً منتظر بمانید")
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
# Download model files from Hugging Face Hub
|
| 92 |
+
model_path = hf_hub_download(
|
| 93 |
+
repo_id="QomSSLab/vits-fa-voice",
|
| 94 |
+
filename="best_model.pth",
|
| 95 |
+
cache_dir="models"
|
| 96 |
+
)
|
| 97 |
+
config_path = hf_hub_download(
|
| 98 |
+
repo_id="QomSSLab/vits-fa-voice",
|
| 99 |
+
filename="config.json",
|
| 100 |
+
cache_dir="models"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Create synthesizer
|
| 104 |
+
synthesizer = Synthesizer(
|
| 105 |
+
tts_checkpoint=model_path,
|
| 106 |
+
tts_config_path=config_path,
|
| 107 |
+
use_cuda=False # Usually no GPU in free Spaces
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
status_block.update("مدل با موفقیت بارگذاری شد! اکنون میتوانید از سیستم استفاده کنید.")
|
| 111 |
+
return synthesizer
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
error_msg = f"خطا در بارگذاری مدل: {str(e)}"
|
| 115 |
+
status_block.update(f"❌ {error_msg}")
|
| 116 |
+
raise RuntimeError(error_msg)
|
| 117 |
+
|
| 118 |
+
def tts(text, speed=1.0):
|
| 119 |
+
if not text.strip():
|
| 120 |
+
return None, "لطفاً متنی وارد کنید."
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
status_block.update("در حال تبدیل متن به گفتار...")
|
| 124 |
+
|
| 125 |
+
# Show processing animation
|
| 126 |
+
for i in range(3):
|
| 127 |
+
time.sleep(0.3)
|
| 128 |
+
status_block.update(f"در حال پردازش{'.' * (i+1)}")
|
| 129 |
+
|
| 130 |
+
# Generate speech
|
| 131 |
+
wav = synthesizer.tts(text, speed=speed)
|
| 132 |
+
output_path = "output.wav"
|
| 133 |
+
synthesizer.save_wav(wav, output_path)
|
| 134 |
+
|
| 135 |
+
status_block.update("✅ صدا با موفقیت تولید شد!")
|
| 136 |
+
return output_path, "تبدیل با موفقیت انجام شد."
|
| 137 |
+
|
| 138 |
+
except Exception as e:
|
| 139 |
+
error_msg = f"خطا در تولید صدا: {str(e)}"
|
| 140 |
+
status_block.update(f"❌ {error_msg}")
|
| 141 |
+
return None, error_msg
|
| 142 |
+
|
| 143 |
+
# Create a status block for feedback
|
| 144 |
+
status_block = gr.Markdown("در حال آمادهسازی سیستم...")
|
| 145 |
+
|
| 146 |
+
# First create the interface without the synthesizer
|
| 147 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 148 |
+
with gr.Column(elem_classes="container"):
|
| 149 |
+
gr.Markdown("# سامانه تبدیل متن فارسی به گفتار", elem_classes="main-header")
|
| 150 |
+
|
| 151 |
+
# Status area
|
| 152 |
+
with gr.Column(elem_classes="status-panel"):
|
| 153 |
+
status_output = gr.Markdown("", elem_id="status")
|
| 154 |
+
|
| 155 |
+
# Input panel
|
| 156 |
+
with gr.Column(elem_classes="input-panel"):
|
| 157 |
+
gr.Markdown("### متن ورودی", elem_classes="label")
|
| 158 |
+
text_input = gr.Textbox(
|
| 159 |
+
placeholder="متن فارسی خود را اینجا وارد کنید...",
|
| 160 |
+
lines=5,
|
| 161 |
+
label="",
|
| 162 |
+
elem_classes="input-text"
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
with gr.Row():
|
| 166 |
+
speed_slider = gr.Slider(
|
| 167 |
+
minimum=0.5,
|
| 168 |
+
maximum=2.0,
|
| 169 |
+
value=1.0,
|
| 170 |
+
step=0.1,
|
| 171 |
+
label="سرعت گفتار",
|
| 172 |
+
elem_classes="speed-slider"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
submit_btn = gr.Button("تبدیل به گفتار", variant="primary", elem_classes="primary")
|
| 176 |
+
|
| 177 |
+
# Output panel
|
| 178 |
+
with gr.Column(elem_classes="output-panel"):
|
| 179 |
+
gr.Markdown("### خروجی صوتی", elem_classes="label")
|
| 180 |
+
output_audio = gr.Audio(label="")
|
| 181 |
+
result_text = gr.Markdown("")
|
| 182 |
+
|
| 183 |
+
# Examples panel
|
| 184 |
+
with gr.Column(elem_classes="examples-panel"):
|
| 185 |
+
gr.Markdown("### نمونههای متنی", elem_classes="label")
|
| 186 |
+
examples = gr.Examples(
|
| 187 |
+
examples=[
|
| 188 |
+
["سلام دنیا، این یک آزمایش برای سیستم تبدیل متن به گفتار فارسی است."],
|
| 189 |
+
["امروز هوا بسیار خوب است و من احساس شادی میکنم."],
|
| 190 |
+
["فناوری هوش مصنوعی به سرعت در حال پیشرفت است و به زودی در تمام جنبههای زندگی ما حضور خواهد داشت."]
|
| 191 |
+
],
|
| 192 |
+
inputs=text_input,
|
| 193 |
+
label="نمونههای متنی را امتحان کنید"
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
gr.Markdown(
|
| 197 |
+
"**راهنما**: متن فارسی خود را در کادر بالا وارد کنید و دکمه تبدیل را فشار دهید. "
|
| 198 |
+
"میتوانید سرعت گفتار را با استفاده از نوار لغزنده تنظیم کنید.",
|
| 199 |
+
elem_classes="footer"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
gr.Markdown(
|
| 203 |
+
"توسعه داده شده با استفاده از مدل VITS فارسی | [WaeliFatima/vits-fa-voice](https://huggingface.co/WaeliFatima/vits-fa-voice)",
|
| 204 |
+
elem_classes="footer"
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Initialize the synthesizer
|
| 208 |
+
try:
|
| 209 |
+
synthesizer = load_synthesizer()
|
| 210 |
+
# Connect the function to the button
|
| 211 |
+
submit_btn.click(
|
| 212 |
+
fn=tts,
|
| 213 |
+
inputs=[text_input, speed_slider],
|
| 214 |
+
outputs=[output_audio, result_text]
|
| 215 |
+
)
|
| 216 |
+
# Update the status block
|
| 217 |
+
status_block.update("سیستم آماده استفاده است!")
|
| 218 |
+
|
| 219 |
+
except Exception as e:
|
| 220 |
+
print(f"Error: {str(e)}")
|
| 221 |
+
status_block.update(f"❌ خطا در بارگذاری مدل: {str(e)}")
|
| 222 |
+
|
| 223 |
+
# Launch the interface
|
| 224 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.40.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
+
soundfile>=0.12.1
|
| 5 |
+
huggingface-hub>=0.20.0
|
| 6 |
+
TTS==0.22.0
|