Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,74 @@
|
|
1 |
import gradio as gr
|
2 |
import torchaudio
|
|
|
3 |
import os
|
|
|
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 |
demo = gr.Interface(
|
29 |
fn=process_audio,
|
30 |
-
inputs=
|
|
|
|
|
|
|
31 |
outputs=gr.Audio(label="Обработанный аудиофайл"),
|
32 |
-
title="VoiceReplacer",
|
33 |
-
description="Замените голос в аудиофайле на ваш собственный голос"
|
34 |
)
|
35 |
|
36 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torchaudio
|
3 |
+
import torch
|
4 |
import os
|
5 |
+
from speechbrain.pretrained import SpeakerRecognition
|
6 |
|
7 |
+
# Путь к вашему голосовому образцу
|
8 |
+
user_voice_path = "voice_recording.wav"
|
9 |
+
|
10 |
+
# Загрузка модели
|
11 |
+
model = SpeakerRecognition.from_hparams(
|
12 |
+
source="speechbrain/spkrec-ecapa-voxceleb",
|
13 |
+
savedir="pretrained_models/spkrec-ecapa-voxceleb",
|
14 |
+
)
|
15 |
+
|
16 |
+
def process_audio(input_audio, pitch_shift=0):
|
17 |
+
try:
|
18 |
+
# Проверяем, является ли input_audio кортежем (путь к файлу, sample_rate)
|
19 |
+
if isinstance(input_audio, tuple):
|
20 |
+
input_audio_path = input_audio[0]
|
21 |
+
elif isinstance(input_audio, str):
|
22 |
+
input_audio_path = input_audio
|
23 |
+
else:
|
24 |
+
raise ValueError(f"Неподдерживаемый формат входных данных: {type(input_audio)}")
|
25 |
+
|
26 |
+
# Загрузка аудиофайлов
|
27 |
+
user_waveform, user_sr = torchaudio.load(user_voice_path)
|
28 |
+
target_waveform, target_sr = torchaudio.load(input_audio_path)
|
29 |
+
|
30 |
+
# Приведение к одинаковой частоте дискретизации
|
31 |
+
if user_sr != target_sr:
|
32 |
+
target_waveform = torchaudio.functional.resample(target_waveform, target_sr, user_sr)
|
33 |
+
|
34 |
+
# Изменение тона голоса
|
35 |
+
if pitch_shift != 0:
|
36 |
+
target_waveform = torchaudio.functional.pitch_shift(
|
37 |
+
waveform=target_waveform,
|
38 |
+
sample_rate=user_sr,
|
39 |
+
n_steps=pitch_shift
|
40 |
+
)
|
41 |
+
|
42 |
+
# Перенос голоса
|
43 |
+
with torch.no_grad():
|
44 |
+
embeddings_user = model.encode_batch(user_waveform)
|
45 |
+
embeddings_target = model.encode_batch(target_waveform)
|
46 |
+
converted_embeddings = embeddings_user + (embeddings_target - embeddings_user) * 0.5
|
47 |
+
|
48 |
+
# Восстановление аудио из embeddings
|
49 |
+
converted_waveform = model.synth_model.generate(
|
50 |
+
converted_embeddings,
|
51 |
+
length=target_waveform.shape[-1]
|
52 |
+
)
|
53 |
+
|
54 |
+
# Сохранение результата
|
55 |
+
output_path = "converted_audio.wav"
|
56 |
+
torchaudio.save(output_path, converted_waveform.cpu(), user_sr)
|
57 |
+
|
58 |
+
return output_path
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Ошибка: {str(e)}")
|
61 |
+
return None
|
62 |
|
63 |
demo = gr.Interface(
|
64 |
fn=process_audio,
|
65 |
+
inputs=[
|
66 |
+
gr.Audio(label="Загрузите аудиофайл с голосом для замены", type="filepath"),
|
67 |
+
gr.Slider(-24, 24, 0, step=1, label="Изменение тона (в полутонов)")
|
68 |
+
],
|
69 |
outputs=gr.Audio(label="Обработанный аудиофайл"),
|
70 |
+
title="VoiceReplacer Pro",
|
71 |
+
description="Замените голос в аудиофайле на ваш собственный голос с возможностью изменения тона"
|
72 |
)
|
73 |
|
74 |
demo.launch()
|