Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,6 @@ import torchaudio
|
|
| 19 |
|
| 20 |
|
| 21 |
import torch.nn.functional as F
|
| 22 |
-
import os
|
| 23 |
|
| 24 |
from moviepy.editor import VideoFileClip
|
| 25 |
|
|
@@ -30,7 +29,6 @@ from huggingface_hub import hf_hub_download
|
|
| 30 |
#!pip install gradio
|
| 31 |
import gradio as gr
|
| 32 |
|
| 33 |
-
from moviepy.editor import VideoFileClip
|
| 34 |
|
| 35 |
|
| 36 |
|
|
@@ -68,13 +66,12 @@ def seprate_speaker(audio_file, pipeline):
|
|
| 68 |
for speaker, segments in speaker_segments.items():
|
| 69 |
# ํ์์ ๋ชจ๋ ๋ฐํ ๊ตฌ๊ฐ์ ์ด์ด๋ถ์
|
| 70 |
combined_waveform = torch.cat(segments, dim=1)
|
| 71 |
-
|
| 72 |
-
output_path = "/tmp/wav" # ๊ฒฝ๋ก
|
| 73 |
os.makedirs(output_path, exist_ok=True) # ๊ฒฝ๋ก๊ฐ ์์ผ๋ฉด ์์ฑ
|
| 74 |
output_filename = os.path.join(output_path,f"{speaker}.wav")
|
| 75 |
|
| 76 |
torchaudio.save(output_filename, combined_waveform, sample_rate) #์ค๋์ค ํ์ผ ์ ์ฅ
|
| 77 |
-
|
| 78 |
|
| 79 |
|
| 80 |
# ๊ฐ๋จํ DeepVoice ์คํ์ผ ๋ชจ๋ธ ์ ์
|
|
@@ -120,28 +117,36 @@ def real_fake_check(list_dir, path, model):
|
|
| 120 |
f_cnt = 0
|
| 121 |
prob = {}
|
| 122 |
for i in list_dir: # real / fake ์ ํ
|
|
|
|
| 123 |
input_data = extract_mfcc_path(os.path.join(path, i))
|
| 124 |
input_data = torch.tensor(input_data).unsqueeze(0).to('cuda') # ๋ฐฐ์น ์ฐจ์์ ์ถ๊ฐํ์ฌ (1, input_dim, sequence_length)๋ก ๋ง์ถค
|
| 125 |
result = model(input_data.float())
|
|
|
|
| 126 |
probabilities = F.softmax(result, dim=1)
|
| 127 |
prob[i]='%.2f'%probabilities[0][1].item()
|
| 128 |
-
predicted_class = 0 if probabilities[0][0] >= THRESHOLD else 1 # ํ๋ฅ ๊ฐ์ด ๊ธฐ์ค์น๋ณด๋ค ํฌ๋ค๋ฉด real, ์๋๋ฉด fake
|
| 129 |
|
|
|
|
|
|
|
| 130 |
if predicted_class == 0:
|
|
|
|
| 131 |
r_cnt += 1
|
| 132 |
else:
|
|
|
|
| 133 |
f_cnt += 1
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
return {'real: ':f'{r_cnt}/{len(list_dir)}', 'fake: ':f'{f_cnt}/{len(list_dir)}', 'prob: ': prob}
|
| 136 |
|
| 137 |
|
| 138 |
def main(file_name):
|
| 139 |
-
pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1")
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
|
| 142 |
video_file = file_name #deepfake #meganfox.mp4'
|
| 143 |
-
|
| 144 |
-
audio_file = '/tmp/output_audio.wav' # ์ ์ฅํ ์ค๋์ค ํ์ผ์ ๊ฒฝ๋ก, ์ด๋ฆ ์ง์
|
| 145 |
|
| 146 |
extract_audio_from_video(video_file, audio_file)
|
| 147 |
|
|
@@ -157,7 +162,7 @@ def main(file_name):
|
|
| 157 |
# ๋ชจ๋ธ
|
| 158 |
model_name = hf_hub_download(repo_id="sssssungk/deepfake_voice", filename="deepvoice_model_girl.pth")
|
| 159 |
model = DeepVoiceModel(input_dim, hidden_dim, num_classes, dropout_rate, l2_reg).to(device)
|
| 160 |
-
model.load_state_dict(torch.load(model_name))
|
| 161 |
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ค์
|
| 162 |
|
| 163 |
|
|
@@ -166,25 +171,29 @@ def main(file_name):
|
|
| 166 |
#real_path = '/content/drive/MyDrive/Celeb-DF-v2/Celeb-real'
|
| 167 |
|
| 168 |
#real = os.listdir(real_path)
|
| 169 |
-
|
| 170 |
-
fake_path = '/tmp/wav'
|
| 171 |
fake = os.listdir(fake_path)
|
| 172 |
|
|
|
|
|
|
|
|
|
|
| 173 |
rf_check = real_fake_check(fake, fake_path,model) #fake dataset\
|
| 174 |
return rf_check
|
| 175 |
|
|
|
|
| 176 |
def deepvoice_check(video_file):
|
| 177 |
results = main(video_file)
|
| 178 |
return results
|
| 179 |
|
| 180 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 181 |
-
|
| 182 |
-
fn=
|
| 183 |
inputs=gr.Video(label="Upload mp4 File"),
|
| 184 |
-
outputs=gr.Textbox(label="
|
| 185 |
-
title="
|
| 186 |
-
description="Upload an mp4 file to check."
|
| 187 |
)
|
| 188 |
|
|
|
|
| 189 |
if __name__ == "__main__":
|
| 190 |
-
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
import torch.nn.functional as F
|
|
|
|
| 22 |
|
| 23 |
from moviepy.editor import VideoFileClip
|
| 24 |
|
|
|
|
| 29 |
#!pip install gradio
|
| 30 |
import gradio as gr
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
|
|
|
|
| 66 |
for speaker, segments in speaker_segments.items():
|
| 67 |
# ํ์์ ๋ชจ๋ ๋ฐํ ๊ตฌ๊ฐ์ ์ด์ด๋ถ์
|
| 68 |
combined_waveform = torch.cat(segments, dim=1)
|
| 69 |
+
output_path = "/content/wav" # ๊ฒฝ๋ก
|
|
|
|
| 70 |
os.makedirs(output_path, exist_ok=True) # ๊ฒฝ๋ก๊ฐ ์์ผ๋ฉด ์์ฑ
|
| 71 |
output_filename = os.path.join(output_path,f"{speaker}.wav")
|
| 72 |
|
| 73 |
torchaudio.save(output_filename, combined_waveform, sample_rate) #์ค๋์ค ํ์ผ ์ ์ฅ
|
| 74 |
+
#print(f"Saved {output_filename} for speaker {speaker}")
|
| 75 |
|
| 76 |
|
| 77 |
# ๊ฐ๋จํ DeepVoice ์คํ์ผ ๋ชจ๋ธ ์ ์
|
|
|
|
| 117 |
f_cnt = 0
|
| 118 |
prob = {}
|
| 119 |
for i in list_dir: # real / fake ์ ํ
|
| 120 |
+
#print('------',i)
|
| 121 |
input_data = extract_mfcc_path(os.path.join(path, i))
|
| 122 |
input_data = torch.tensor(input_data).unsqueeze(0).to('cuda') # ๋ฐฐ์น ์ฐจ์์ ์ถ๊ฐํ์ฌ (1, input_dim, sequence_length)๋ก ๋ง์ถค
|
| 123 |
result = model(input_data.float())
|
| 124 |
+
# predicted_class = torch.argmax(result, dim=1).item()
|
| 125 |
probabilities = F.softmax(result, dim=1)
|
| 126 |
prob[i]='%.2f'%probabilities[0][1].item()
|
|
|
|
| 127 |
|
| 128 |
+
predicted_class = 0 if probabilities[0][0] >= THRESHOLD else 1 # ํ๋ฅ ๊ฐ์ด ๊ธฐ์ค์น๋ณด๋ค ํฌ๋ค๋ฉด real, ์๋๋ฉด fake
|
| 129 |
+
# print('-- %.2f'%probabilities[0][0].item()) #ํ๋ฅ ๊ฐ ์ถ๋ ฅ
|
| 130 |
if predicted_class == 0:
|
| 131 |
+
# print("REAL")
|
| 132 |
r_cnt += 1
|
| 133 |
else:
|
| 134 |
+
# print("FAKE")
|
| 135 |
f_cnt += 1
|
| 136 |
+
#print()
|
| 137 |
+
#print('real: ',r_cnt,'/',len(list_dir))
|
| 138 |
+
#print('fake: ',f_cnt,'/',len(list_dir))
|
| 139 |
return {'real: ':f'{r_cnt}/{len(list_dir)}', 'fake: ':f'{f_cnt}/{len(list_dir)}', 'prob: ': prob}
|
| 140 |
|
| 141 |
|
| 142 |
def main(file_name):
|
| 143 |
+
pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1",)
|
| 144 |
+
|
| 145 |
+
#pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1")
|
| 146 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 147 |
|
| 148 |
video_file = file_name #deepfake #meganfox.mp4'
|
| 149 |
+
audio_file = '/content/output_audio.wav' # ์ ์ฅํ ์ค๋์ค ํ์ผ์ ๊ฒฝ๋ก, ์ด๋ฆ ์ง์
|
|
|
|
| 150 |
|
| 151 |
extract_audio_from_video(video_file, audio_file)
|
| 152 |
|
|
|
|
| 162 |
# ๋ชจ๋ธ
|
| 163 |
model_name = hf_hub_download(repo_id="sssssungk/deepfake_voice", filename="deepvoice_model_girl.pth")
|
| 164 |
model = DeepVoiceModel(input_dim, hidden_dim, num_classes, dropout_rate, l2_reg).to(device)
|
| 165 |
+
model.load_state_dict(torch.load(model_name))#("/content/drive/MyDrive/แแ
ขแธแแ
ณแแ
ฉแซ 1แแ
ฉ/model/deepvoice_model_girl.pth"))
|
| 166 |
model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ค์
|
| 167 |
|
| 168 |
|
|
|
|
| 171 |
#real_path = '/content/drive/MyDrive/Celeb-DF-v2/Celeb-real'
|
| 172 |
|
| 173 |
#real = os.listdir(real_path)
|
| 174 |
+
fake_path = '/content/wav'#'/content/drive/MyDrive/แแ
ขแธแแ
ณแแ
ฉแซ 1แแ
ฉ/data/deepvoice/fake'
|
|
|
|
| 175 |
fake = os.listdir(fake_path)
|
| 176 |
|
| 177 |
+
#print("\n-------real data---------")
|
| 178 |
+
#real_fake_check(real, real_path, model) #real dataset
|
| 179 |
+
#print("\n-------fake data---------")
|
| 180 |
rf_check = real_fake_check(fake, fake_path,model) #fake dataset\
|
| 181 |
return rf_check
|
| 182 |
|
| 183 |
+
#Gradio ๋ฉ์ธ ํจ์
|
| 184 |
def deepvoice_check(video_file):
|
| 185 |
results = main(video_file)
|
| 186 |
return results
|
| 187 |
|
| 188 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 189 |
+
iface = gr.Interface(
|
| 190 |
+
fn=main,
|
| 191 |
inputs=gr.Video(label="Upload mp4 File"),
|
| 192 |
+
outputs=gr.Textbox(label="Deepfake Detection Result"),
|
| 193 |
+
title="DeepVoice Check",
|
| 194 |
+
description="Upload an mp4 file to check for DeepVoice indicators."
|
| 195 |
)
|
| 196 |
|
| 197 |
+
# Gradio ์ธํฐํ์ด์ค ์คํ
|
| 198 |
if __name__ == "__main__":
|
| 199 |
+
iface.launch()
|