sssssungk commited on
Commit
a065eb5
ยท
verified ยท
1 Parent(s): a447191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -20
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
- #current_path = os.getcwd()
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
- device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
 
 
141
 
142
  video_file = file_name #deepfake #meganfox.mp4'
143
- #current_path = os.getcwd()
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
- #current_path = os.getcwd()
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
- deepfake = gr.Interface(
182
- fn=deepvoice_check,
183
  inputs=gr.Video(label="Upload mp4 File"),
184
- outputs=gr.Textbox(label="DeepFaKeVoice Detection Result"),
185
- title="DeepFaKeVoice Check",
186
- description="Upload an mp4 file to check."
187
  )
188
 
 
189
  if __name__ == "__main__":
190
- deepfake.launch(share=True, debug=True)
 
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()