aboudcode commited on
Commit
adf9331
·
verified ·
1 Parent(s): f6728d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -22
app.py CHANGED
@@ -3,29 +3,38 @@ from pydub import AudioSegment
3
  from pydub.effects import normalize, compress_dynamic_range
4
  import os
5
 
6
- def mix_audio(vocal_file, music_file):
7
- # تحميل ومعالجة الصوت
8
- vocals = AudioSegment.from_file(vocal_file)
9
- music = AudioSegment.from_file(music_file)
10
-
11
- vocals = normalize(vocals)
12
- vocals = compress_dynamic_range(vocals, threshold=-20.0, ratio=4.0)
13
-
14
- if len(music) < len(vocals):
15
- music = music * ((len(vocals) // len(music)) + 1)
16
-
17
- music = music[:len(vocals) + 2000] - 9
18
- final = music.overlay(vocals, position=0)
19
- final = normalize(final)
20
-
21
- output_path = "final_master.mp3"
22
- final.export(output_path, format="mp3", bitrate="320k")
23
- return output_path
 
 
 
 
 
 
 
 
 
24
 
25
- # واجهة بسيطة لاستقبال الملفات من n8n
26
  demo = gr.Interface(
27
- fn=mix_audio,
28
- inputs=[gr.Audio(type="filepath"), gr.Audio(type="filepath")],
29
- outputs=gr.Audio(type="filepath")
30
  )
 
31
  demo.launch()
 
3
  from pydub.effects import normalize, compress_dynamic_range
4
  import os
5
 
6
+ def process_audio(vocal_file, music_file):
7
+ try:
8
+ if vocal_file is None or music_file is None:
9
+ return "الرجاء رفع الملفين أولاً (صوتك + الموسيقى)"
10
+
11
+ # تحميل الملفات
12
+ vocals = AudioSegment.from_file(vocal_file.name)
13
+ music = AudioSegment.from_file(music_file.name)
14
+
15
+ # المعالجة الاحترافية
16
+ vocals = normalize(vocals)
17
+ vocals = compress_dynamic_range(vocals, threshold=-20.0, ratio=4.0)
18
+
19
+ if len(music) < len(vocals):
20
+ music = music * ((len(vocals) // len(music)) + 1)
21
+
22
+ music = music[:len(vocals) + 2000] - 10
23
+ final_mix = music.overlay(vocals, position=0)
24
+ final_mix = normalize(final_mix).fade_out(2000)
25
+
26
+ output_path = "final_master.mp3"
27
+ final_mix.export(output_path, format="mp3", bitrate="320k")
28
+ return output_path
29
+
30
+ except Exception as e:
31
+ # هذا السطر سيظهر لك الخطأ الحقيقي في واجهة Hugging Face
32
+ return f"حدث خطأ تقني: {str(e)}"
33
 
 
34
  demo = gr.Interface(
35
+ fn=process_audio,
36
+ inputs=[gr.File(label="ارفع صوتك هنا"), gr.File(label="ارفع الموسيقى هنا")],
37
+ outputs=gr.File(label="تحميل الأغنية النهائية")
38
  )
39
+
40
  demo.launch()