DavidCombei commited on
Commit
8288053
·
verified ·
1 Parent(s): d8876d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -6,7 +6,7 @@ import librosa
6
  import numpy as np
7
  from sklearn.linear_model import LogisticRegression
8
  import gradio as gr
9
- from pytube import YouTube
10
 
11
  class HuggingFaceFeatureExtractor:
12
  def __init__(self, model_class, name):
@@ -40,9 +40,22 @@ model4 = joblib.load('model4_ensemble.pkl')
40
  final_model = joblib.load('final_model_ensemble.pkl')
41
 
42
  def download_audio_from_youtube(youtube_url, output_path='.'):
43
- yt = YouTube(youtube_url)
44
- audio_stream = yt.streams.filter(only_audio=True).first()
45
- audio_file = audio_stream.download(output_path=output_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  return audio_file
47
 
48
  def segment_audio(audio, sr, segment_duration):
@@ -88,7 +101,7 @@ def process_audio(input_data, segment_duration=3):
88
  # PhantomNet extractor
89
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
90
  model = PhantomNet(feature_size=1920, num_classes=2, conv_projection=False, use_mode='extractor').to(device)
91
- state_dict = torch.load("PhantomNet/saved_models/PhantomNet_Finetuned_V2.pt", map_location=device)
92
  model.load_state_dict(state_dict, strict=False)
93
  model.eval()
94
 
@@ -120,9 +133,9 @@ def process_audio(input_data, segment_duration=3):
120
  y_pred_inference = classify_with_eer_threshold(final_prob, eer_thresh)
121
 
122
  if y_pred_inference == 1:
123
- return f"Fake with a confidence of: {100 - final_prob[0] * 100:.2f}%"
124
  else:
125
- return f"Real with a confidence of: {final_prob[0] * 100:.2f}%"
126
 
127
  def gradio_interface(audio, youtube_link):
128
  if youtube_link:
@@ -137,7 +150,7 @@ interface = gr.Interface(
137
  inputs=[gr.Audio(type="filepath", label="Upload Audio"), gr.Textbox(label="YouTube Link (Optional)")],
138
  outputs="text",
139
  title="AI4TRUST Development",
140
- description="Upload an audio file or provide a YouTube link to check for authenticity.",
141
  )
142
 
143
  interface.launch(share=True)
 
6
  import numpy as np
7
  from sklearn.linear_model import LogisticRegression
8
  import gradio as gr
9
+ import yt_dlp as youtube_dl
10
 
11
  class HuggingFaceFeatureExtractor:
12
  def __init__(self, model_class, name):
 
40
  final_model = joblib.load('final_model_ensemble.pkl')
41
 
42
  def download_audio_from_youtube(youtube_url, output_path='.'):
43
+ ydl_opts = {
44
+ 'format': 'bestaudio/best',
45
+ 'outtmpl': f'{output_path}/%(title)s.%(ext)s',
46
+ 'postprocessors': [{
47
+ 'key': 'FFmpegExtractAudio',
48
+ 'preferredcodec': 'wav',
49
+ 'preferredquality': '192',
50
+ }],
51
+ 'postprocessor_args': ['-ar', '16000'],
52
+ 'prefer_ffmpeg': True,
53
+ }
54
+
55
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
56
+ info_dict = ydl.extract_info(youtube_url, download=True)
57
+ #i have issues with the .webm extension, force replace with .wav
58
+ audio_file = ydl.prepare_filename(info_dict).replace('.webm', '.wav')
59
  return audio_file
60
 
61
  def segment_audio(audio, sr, segment_duration):
 
101
  # PhantomNet extractor
102
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
103
  model = PhantomNet(feature_size=1920, num_classes=2, conv_projection=False, use_mode='extractor').to(device)
104
+ state_dict = torch.load("PhantomNet_Finetuned_V2.pt", map_location=device)
105
  model.load_state_dict(state_dict, strict=False)
106
  model.eval()
107
 
 
133
  y_pred_inference = classify_with_eer_threshold(final_prob, eer_thresh)
134
 
135
  if y_pred_inference == 1:
136
+ return f"Fake with a confidence of: {final_prob[0] * 100:.2f}%"
137
  else:
138
+ return f"Real with a confidence of: {100 - final_prob[0] * 100:.2f}%"
139
 
140
  def gradio_interface(audio, youtube_link):
141
  if youtube_link:
 
150
  inputs=[gr.Audio(type="filepath", label="Upload Audio"), gr.Textbox(label="YouTube Link (Optional)")],
151
  outputs="text",
152
  title="AI4TRUST Development",
153
+ description="Upload an audio file or provide a YouTube link to check if it's AI generated",
154
  )
155
 
156
  interface.launch(share=True)