Jeonghwanny commited on
Commit
02926d5
·
verified ·
1 Parent(s): a5f8c36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -33,7 +33,6 @@ import gradio as gr
33
  from moviepy.editor import VideoFileClip
34
 
35
 
36
-
37
  # 오디오 변환 mp4 --> wav
38
  def extract_audio_from_video(video_file_path, audio_file_path):
39
  # mp4 파일 불러오기
@@ -68,14 +67,13 @@ 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
- output_path = 'wav'
74
  os.makedirs(output_path, exist_ok=True) # 경로가 없으면 생성
75
  output_filename = os.path.join(output_path,f"{speaker}.wav")
76
 
77
  torchaudio.save(output_filename, combined_waveform, sample_rate) #오디오 파일 저장
78
-
79
 
80
 
81
  # 간단한 DeepVoice 스타일 모델 정의
@@ -121,29 +119,38 @@ def real_fake_check(list_dir, path, model):
121
  f_cnt = 0
122
  prob = {}
123
  for i in list_dir: # real / fake 선택
 
124
  input_data = extract_mfcc_path(os.path.join(path, i))
125
- input_data = torch.tensor(input_data).unsqueeze(0).to('cuda') # 배치 차원을 추가하여 (1, input_dim, sequence_length)로 맞춤
126
  result = model(input_data.float())
 
127
  probabilities = F.softmax(result, dim=1)
128
  prob[i]='%.2f'%probabilities[0][1].item()
129
- predicted_class = 0 if probabilities[0][0] >= THRESHOLD else 1 # 확률값이 기준치보다 크다면 real, 아니면 fake
130
 
 
 
131
  if predicted_class == 0:
 
132
  r_cnt += 1
133
  else:
 
134
  f_cnt += 1
135
-
 
 
136
  return {'real: ':f'{r_cnt}/{len(list_dir)}', 'fake: ':f'{f_cnt}/{len(list_dir)}', 'prob: ': prob}
137
 
138
 
139
  def main(file_name):
140
- pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1")
141
- device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
 
 
 
142
 
143
  video_file = file_name #deepfake #meganfox.mp4'
144
- #current_path = os.getcwd()
145
- # audio_file = '/tmp/output_audio.wav' # 저장할 오디오 파일의 경로, 이름 지정
146
- audio_file = 'output_audio.wav'
147
  extract_audio_from_video(video_file, audio_file)
148
 
149
  seprate_speaker(audio_file,pipeline) # 발화자 분리해서 파일로 만들기
@@ -156,37 +163,39 @@ def main(file_name):
156
  l2_reg = 0.01
157
 
158
  # 모델
159
- model_name = "deepvoice_model_girl.pth"
160
  model = DeepVoiceModel(input_dim, hidden_dim, num_classes, dropout_rate, l2_reg).to(device)
161
- model.load_state_dict(torch.load(model_name))
162
  model.eval() # 평가 모드로 설정
163
 
164
 
165
  #real,fake 폴더
166
- #real_path = '/content/drive/MyDrive/캡스톤 1조/data/deepvoice/real'
167
  #real_path = '/content/drive/MyDrive/Celeb-DF-v2/Celeb-real'
168
 
169
  #real = os.listdir(real_path)
170
- #current_path = os.getcwd()
171
- # fake_path = '/tmp/wav'
172
- fake_path = 'wav'
173
  fake = os.listdir(fake_path)
174
 
 
 
 
175
  rf_check = real_fake_check(fake, fake_path,model) #fake dataset\
176
  return rf_check
177
 
 
178
  def deepvoice_check(video_file):
179
  results = main(video_file)
180
  return results
181
 
182
  # Gradio 인터페이스 생성
183
- deepfake = gr.Interface(
184
- fn=deepvoice_check,
185
  inputs=gr.Video(label="Upload mp4 File"),
186
- outputs=gr.Textbox(label="DeepFaKeVoice Detection Result"),
187
- title="DeepFaKeVoice Check",
188
- description="Upload an mp4 file to check."
189
  )
190
 
191
- if __name__ == "__main__":
192
- deepfake.launch(share=True, debug=True)
 
33
  from moviepy.editor import VideoFileClip
34
 
35
 
 
36
  # 오디오 변환 mp4 --> wav
37
  def extract_audio_from_video(video_file_path, audio_file_path):
38
  # mp4 파일 불러오기
 
67
  for speaker, segments in speaker_segments.items():
68
  # 화자의 모든 발화 구간을 이어붙임
69
  combined_waveform = torch.cat(segments, dim=1)
70
+ # output_path = "/content/wav" # 경로
71
+ output_path = './output'
 
72
  os.makedirs(output_path, exist_ok=True) # 경로가 없으면 생성
73
  output_filename = os.path.join(output_path,f"{speaker}.wav")
74
 
75
  torchaudio.save(output_filename, combined_waveform, sample_rate) #오디오 파일 저장
76
+ #print(f"Saved {output_filename} for speaker {speaker}")
77
 
78
 
79
  # 간단한 DeepVoice 스타일 모델 정의
 
119
  f_cnt = 0
120
  prob = {}
121
  for i in list_dir: # real / fake 선택
122
+ #print('------',i)
123
  input_data = extract_mfcc_path(os.path.join(path, i))
124
+ input_data = torch.tensor(input_data).unsqueeze(0).to(device) # 배치 차원을 추가하여 (1, input_dim, sequence_length)로 맞춤
125
  result = model(input_data.float())
126
+ # predicted_class = torch.argmax(result, dim=1).item()
127
  probabilities = F.softmax(result, dim=1)
128
  prob[i]='%.2f'%probabilities[0][1].item()
 
129
 
130
+ predicted_class = 0 if probabilities[0][0] >= THRESHOLD else 1 # 확률값이 기준치보다 크다면 real, 아니면 fake
131
+ # print('-- %.2f'%probabilities[0][0].item()) #확률 값 출력
132
  if predicted_class == 0:
133
+ # print("REAL")
134
  r_cnt += 1
135
  else:
136
+ # print("FAKE")
137
  f_cnt += 1
138
+ #print()
139
+ #print('real: ',r_cnt,'/',len(list_dir))
140
+ #print('fake: ',f_cnt,'/',len(list_dir))
141
  return {'real: ':f'{r_cnt}/{len(list_dir)}', 'fake: ':f'{f_cnt}/{len(list_dir)}', 'prob: ': prob}
142
 
143
 
144
  def main(file_name):
145
+ my_key = os.getenv("my_key")
146
+ pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1",
147
+ use_auth_token=my_key)
148
+ # pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization-3.1")
149
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
150
 
151
  video_file = file_name #deepfake #meganfox.mp4'
152
+ audio_file = './output_audio.wav' # 저장할 오디오 파일의 경로, 이름 지정
153
+
 
154
  extract_audio_from_video(video_file, audio_file)
155
 
156
  seprate_speaker(audio_file,pipeline) # 발화자 분리해서 파일로 만들기
 
163
  l2_reg = 0.01
164
 
165
  # 모델
166
+ model_name = './deepvoice_model_girl.pth'
167
  model = DeepVoiceModel(input_dim, hidden_dim, num_classes, dropout_rate, l2_reg).to(device)
168
+ model.load_state_dict(torch.load(model_name, map_location=torch.device(device)))#("/content/drive/MyDrive/캡스톤 1조/model/deepvoice_model_girl.pth"))
169
  model.eval() # 평가 모드로 설정
170
 
171
 
172
  #real,fake 폴더
173
+ #real_path = '/content/drive/MyDrive/캡스톤 1/data/deepvoice/real'
174
  #real_path = '/content/drive/MyDrive/Celeb-DF-v2/Celeb-real'
175
 
176
  #real = os.listdir(real_path)
177
+ fake_path = './output'#'/content/drive/MyDrive/캡스톤 1조/data/deepvoice/fake'
 
 
178
  fake = os.listdir(fake_path)
179
 
180
+ #print("\n-------real data---------")
181
+ #real_fake_check(real, real_path, model) #real dataset
182
+ #print("\n-------fake data---------")
183
  rf_check = real_fake_check(fake, fake_path,model) #fake dataset\
184
  return rf_check
185
 
186
+ #Gradio 메인 함수
187
  def deepvoice_check(video_file):
188
  results = main(video_file)
189
  return results
190
 
191
  # Gradio 인터페이스 생성
192
+ iface = gr.Interface(
193
+ fn=main,
194
  inputs=gr.Video(label="Upload mp4 File"),
195
+ outputs=gr.Textbox(label="Deepfake Detection Result"),
196
+ title="DeepVoice Check",
197
+ description="Upload an mp4 file to check for DeepVoice indicators."
198
  )
199
 
200
+ # Gradio 인터페이스 실행
201
+ iface.launch(share=True, debug=True)