Spaces:
Running
Running
File size: 12,050 Bytes
319ea69 c296f8a cd88e5f 39ac26a ba6b9e4 319ea69 defad0a cd88e5f defad0a 9ddae4b defad0a ba6b9e4 cd88e5f defad0a cd88e5f defad0a cd88e5f defad0a cd88e5f defad0a ba6b9e4 defad0a 1c4f949 ba6b9e4 1c4f949 defad0a c296f8a defad0a 319ea69 defad0a 1c4f949 defad0a ba6b9e4 1c4f949 cd88e5f defad0a cd88e5f 1c4f949 defad0a ba6b9e4 cd88e5f defad0a cd88e5f defad0a ba6b9e4 1c4f949 ba6b9e4 1c4f949 7271ac7 1c4f949 7271ac7 1c4f949 ba6b9e4 1c4f949 7271ac7 ba6b9e4 1c4f949 ba6b9e4 7271ac7 1c4f949 7271ac7 1c4f949 7271ac7 1c4f949 ba6b9e4 1c4f949 7271ac7 1c4f949 ba6b9e4 1c4f949 ba6b9e4 1c4f949 ba6b9e4 59f4e89 ba6b9e4 1c4f949 ba6b9e4 1c4f949 defad0a 1c4f949 defad0a 1c4f949 defad0a 1c4f949 ba6b9e4 1c4f949 defad0a 1c4f949 defad0a 1c4f949 ba6b9e4 1c4f949 defad0a 3a12f33 ba6b9e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
import gradio as gr
from pydub import AudioSegment
import requests
import os
import uuid
import re
# مسیر ذخیره فایلهای موقت
TEMP_DIR = "temp_audio"
if not os.path.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
def download_file(url, output_path):
"""فایل را از یک URL دانلود میکند."""
try:
response = requests.get(url, stream=True)
response.raise_for_status()
with open(output_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return True
except requests.exceptions.RequestException as e:
print(f"Error downloading {url}: {e}")
return False
except Exception as e:
print(f"An unexpected error occurred during download of {url}: {e}")
return False
def get_audio_from_input(input_source):
"""منبع ورودی را پردازش کرده و یک شی AudioSegment برمیگرداند."""
unique_filename = os.path.join(TEMP_DIR, str(uuid.uuid4()))
if input_source.startswith("http://") or input_source.startswith("https://"):
file_extension = os.path.splitext(input_source.split('?')[0])[1]
if not file_extension:
file_extension = ".mp3"
temp_filepath = unique_filename + "_downloaded" + file_extension
if not download_file(input_source, temp_filepath):
return None, f"خطا در دانلود فایل از لینک: {input_source}"
audio_path = temp_filepath
else:
audio_path = input_source
try:
audio = AudioSegment.from_file(audio_path)
return audio, None
except Exception as e:
return None, f"خطا در بارگذاری فایل صوتی ({audio_path}): {e}. مطمئن شوید فایل MP3 یا WAV معتبر است."
finally:
if 'temp_filepath' in locals() and os.path.exists(temp_filepath):
try:
os.remove(temp_filepath)
except OSError as e:
print(f"Error removing temporary file {temp_filepath}: {e}")
def merge_audio_files(input_sources):
"""چندین فایل صوتی را ادغام میکند و یک فایل MP3 خروجی میدهد."""
if not input_sources:
return None, "لیست ورودیهای صوتی خالی است."
combined_audio = AudioSegment.empty()
errors = []
for source in input_sources:
audio_segment, error = get_audio_from_input(source)
if audio_segment:
combined_audio += audio_segment
else:
errors.append(error)
print(f"Skipping {source} due to error: {error}")
if not combined_audio.duration_seconds > 0:
return None, "هیچ فایل صوتی معتبری برای ادغام پیدا نشد. " + "\n".join(errors) if errors else ""
output_filename = os.path.join(TEMP_DIR, f"merged_audio_{uuid.uuid4()}.mp3")
try:
combined_audio.export(output_filename, format="mp3")
return output_filename, "عملیات موفقیت آمیز بود!"
except Exception as e:
return None, f"خطا در ذخیره فایل خروجی: {e}"
def add_intro_outro_and_background(podcast_audio, intro_audio_url, background_audio_url, outro_audio_url):
"""افکتهای صوتی را به پادکست اضافه میکند."""
# دانلود صدای ابتدایی (intro)
intro_audio, _ = get_audio_from_input(intro_audio_url)
if not intro_audio:
return None, "خطا در دانلود یا بارگذاری صدای ابتدایی"
# دانلود موزیک پسزمینه
background_audio, _ = get_audio_from_input(background_audio_url)
if not background_audio:
return None, "خطا در دانلود یا بارگذاری موزیک پسزمینه"
# دانلود صدای انتهایی (outro)
outro_audio, _ = get_audio_from_input(outro_audio_url)
if not outro_audio:
return None, "خطا در دانلود یا بارگذاری صدای انتهایی"
# طول پادکست اصلی
podcast_duration = len(podcast_audio)
# اضافه کردن صدای ابتدایی
final_audio = intro_audio
# اضافه کردن موزیک پسزمینه
background_audio = background_audio[:podcast_duration + 2000] # موزیک به اندازه پادکست + 2 ثانیه
background_audio = background_audio.fade_in(2000).fade_out(2000) # fade-in و fade-out
final_audio = final_audio.overlay(background_audio, position=0) # موزیک پسزمینه از ابتدا شروع میشود
# اضافه کردن پادکست اصلی
final_audio = final_audio + podcast_audio
# اضافه کردن صدای انتهایی
final_audio = final_audio + outro_audio
return final_audio, None
def tts_and_merge_with_effects(text_input):
"""متن ورودی را پردازش کرده و فایل صوتی با افکتهای صوتی تولید میکند."""
if not text_input.strip():
return None, "لطفاً متنی برای پردازش وارد کنید."
# تجزیه متن و تولید پادکست اصلی
lines = text_input.strip().split('\n')
audio_urls_to_merge = []
errors = []
for line in lines:
match = re.match(r'^\s*\((\d+)\)(.*)$', line)
if match:
speaker_number = match.group(1)
text_for_tts = match.group(2).strip()
if not text_for_tts:
errors.append(f"خطا: متن خالی برای گوینده {speaker_number} در خط '{line}'")
continue
# ساخت URL برای Talkbot API
tts_url = f"https://talkbot.ir/api/TTS-S{speaker_number}?text={text_for_tts}"
print(f"درخواست TTS برای گوینده {speaker_number}: {tts_url}")
try:
response = requests.get(tts_url)
response.raise_for_status()
audio_link = response.text.strip()
if audio_link.startswith("http"):
audio_urls_to_merge.append(audio_link)
else:
errors.append(f"API برای گوینده {speaker_number} لینک معتبری برنگرداند: '{audio_link}'")
except requests.exceptions.RequestException as e:
errors.append(f"خطا در ارتباط با Talkbot API برای گوینده {speaker_number}: {e}")
except Exception as e:
errors.append(f"خطای غیرمنتظره در پردازش Talkbot API برای گوینده {speaker_number}: {e}")
else:
if line.strip():
errors.append(f"فرمت نامعتبر در خط: '{line}'. انتظار میرود (شماره)متن.")
if not audio_urls_to_merge:
return None, "هیچ فایل صوتی برای ادغام تولید نشد." + "\n".join(errors) if errors else ""
# ادغام فایلهای صوتی تولید شده
podcast_audio_path, merge_message = merge_audio_files(audio_urls_to_merge)
if not podcast_audio_path:
return None, merge_message
# بارگذاری پادکست اصلی
podcast_audio = AudioSegment.from_file(podcast_audio_path)
# افزودن افکتهای صوتی
final_audio, error = add_intro_outro_and_background(
podcast_audio,
intro_audio_url="https://talkbot.ir/example/effect-podcast/wk.mp3",
background_audio_url="https://talkbot.ir/example/effect-podcast/bk.mp3", # مسیر موزیک پسزمینه
outro_audio_url="https://talkbot.ir/example/effect-podcast/1.mp3" # مسیر صدای انتهایی
)
if not final_audio:
return None, error
# ذخیره فایل نهایی
output_filename = os.path.join(TEMP_DIR, f"final_podcast_{uuid.uuid4()}.mp3")
try:
final_audio.export(output_filename, format="mp3")
return output_filename, "پادکست با افکتهای صوتی با موفقیت تولید شد!"
except Exception as e:
return None, f"خطا در ذخیره پادکست نهایی: {e}"
# ایجاد رابط کاربری Gradio
with gr.Blocks() as demo:
gr.Markdown(
"""
# ابزار ادغام فایلهای صوتی (MP3/WAV) و تولید صدا از متن
در اینجا دو بخش اصلی وجود دارد:
1. **ادغام فایلهای صوتی موجود:** میتوانید لینکهای فایلهای صوتی (MP3/WAV) را وارد کنید.
2. **تولید صدا از متن با Talkbot API و ادغام:** متنی را با فرمت `(شماره)متن` وارد کنید.
(مثال: `(1)سلام\n(2)بله`)
این بخش با استفاده از Talkbot API صدا تولید کرده و آنها را ادغام میکند.
"""
)
with gr.Tab("ادغام فایلهای صوتی موجود"):
gr.Markdown("## ادغام فایلهای صوتی موجود (از لینک یا فایل محلی)")
audio_links_input = gr.Textbox(
label="لینک یا مسیر فایلهای صوتی (هر کدام در یک خط جدید)",
placeholder="مثال:\nhttps://example.com/audio1.mp3\n./local_audio.wav\nhttps://example.com/audio2.wav",
lines=10
)
audio_merge_output_message = gr.Textbox(label="پیام", interactive=False)
audio_merge_output_audio = gr.Audio(label="فایل صوتی ادغام شده", type="filepath")
merge_button = gr.Button("ادغام فایلهای صوتی")
merge_button.click(
fn=lambda x: merge_audio_files([s.strip() for s in x.split('\n') if s.strip()]),
inputs=[audio_links_input],
outputs=[audio_merge_output_audio, audio_merge_output_message]
)
gr.Examples(
examples=[
["https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3\nhttps://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"],
],
inputs=audio_links_input,
label="نمونهها"
)
with gr.Tab("تولید صدا از متن (Talkbot API) و ادغام"):
gr.Markdown("## تولید صدا با Talkbot API و ادغام فایلها")
tts_text_input = gr.Textbox(
label="متن برای تولید صدا (فرمت: (شماره)متن - هر پرسوناژ در یک خط جدید)",
placeholder="(1)سلام این تست صحبت اولین نفر است.\n(2)سلام، بله این هم یک تست است و من کاراکتر دوم هستم.\n(1)خب از کجا شروع کنیم\n(2)بهتره از اول شروع کنیم",
lines=10
)
tts_output_message = gr.Textbox(label="پیام", interactive=False)
tts_output_audio = gr.Audio(label="فایل صوتی تولید و ادغام شده", type="filepath")
tts_merge_button = gr.Button("تولید و ادغام صدا")
tts_merge_button.click(
fn=tts_and_merge_with_effects,
inputs=[tts_text_input],
outputs=[tts_output_audio, tts_output_message]
)
gr.Examples(
examples=[
["(1)سلام این تست صحبت اولین نفر است.\n(2)سلام، بله این هم یک تست است و من کاراکتر دوم هستم."],
["(1)امروز هوا چطوره؟\n(2)فکر کنم آفتابیه."]
],
inputs=tts_text_input,
label="نمونهها"
)
if __name__ == "__main__":
demo.launch() # برای اجرا در لوکال
# demo.launch(share=True) # برای اشتراکگذاری موقت در یک لینک عمومی
|