FatimaWaeli commited on
Commit
67bf90c
·
1 Parent(s): 4add10c

Add application file

Browse files
Files changed (1) hide show
  1. app.py +30 -35
app.py CHANGED
@@ -83,9 +83,9 @@ button.primary:hover {
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
@@ -107,50 +107,42 @@ def load_synthesizer():
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"):
@@ -200,25 +192,28 @@ with gr.Blocks(css=custom_css) as demo:
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()
 
83
  }
84
  """
85
 
86
+ def load_synthesizer(status_output):
87
+ # Update status message
88
+ status = "در حال بارگذاری مدل... لطفاً منتظر بمانید"
89
 
90
  try:
91
  # Download model files from Hugging Face Hub
 
107
  use_cuda=False # Usually no GPU in free Spaces
108
  )
109
 
110
+ status = "مدل با موفقیت بارگذاری شد! اکنون می‌توانید از سیستم استفاده کنید."
111
+ return synthesizer, status
112
 
113
  except Exception as e:
114
  error_msg = f"خطا در بارگذاری مدل: {str(e)}"
115
+ status = f"❌ {error_msg}"
116
  raise RuntimeError(error_msg)
117
 
118
+ def tts(text, speed, status_output):
119
  if not text.strip():
120
+ return None, "لطفاً متنی وارد کنید.", status_output.value
121
 
122
  try:
123
+ status = "در حال تبدیل متن به گفتار..."
 
 
 
 
 
124
 
125
  # Generate speech
126
  wav = synthesizer.tts(text, speed=speed)
127
  output_path = "output.wav"
128
  synthesizer.save_wav(wav, output_path)
129
 
130
+ status = "✅ صدا با موفقیت تولید شد!"
131
+ return output_path, "تبدیل با موفقیت انجام شد.", status
132
 
133
  except Exception as e:
134
  error_msg = f"خطا در تولید صدا: {str(e)}"
135
+ status = f"❌ {error_msg}"
136
+ return None, error_msg, status
 
 
 
137
 
138
+ # Create the interface
139
  with gr.Blocks(css=custom_css) as demo:
140
  with gr.Column(elem_classes="container"):
141
  gr.Markdown("# سامانه تبدیل متن فارسی به گفتار", elem_classes="main-header")
142
 
143
  # Status area
144
  with gr.Column(elem_classes="status-panel"):
145
+ status_output = gr.Markdown("در حال آماده‌سازی سیستم...", elem_id="status")
146
 
147
  # Input panel
148
  with gr.Column(elem_classes="input-panel"):
 
192
  )
193
 
194
  gr.Markdown(
195
+ "توسعه داده شده با استفاده از مدل VITS فارسی | [QomSSLab/vits-fa-voice](https://huggingface.co/QomSSLab/vits-fa-voice)",
196
  elem_classes="footer"
197
  )
198
 
199
+ # Initialize the synthesizer on app startup
200
+ synthesizer = None
201
+
202
+ @demo.load
203
+ def init_synthesizer():
204
+ global synthesizer
205
+ try:
206
+ synthesizer, status = load_synthesizer(status_output)
207
+ return status_output.update(status)
208
+ except Exception as e:
209
+ return status_output.update(f"❌ خطا در بارگذاری مدل: {str(e)}")
210
+
211
  # Connect the function to the button
212
  submit_btn.click(
213
  fn=tts,
214
+ inputs=[text_input, speed_slider, status_output],
215
+ outputs=[output_audio, result_text, status_output]
216
  )
 
 
 
 
 
 
217
 
218
  # Launch the interface
219
  demo.launch()