Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
27c4f4a
1
Parent(s):
2dab185
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import soundfile as sf
|
5 |
+
import io
|
6 |
+
import librosa
|
7 |
+
import numpy as np
|
8 |
+
from pytube import YouTube
|
9 |
+
import os
|
10 |
+
import random
|
11 |
+
|
12 |
+
FS=16000
|
13 |
+
MAX_SIZE = FS * 30
|
14 |
+
MODEL = torch.jit.load('model.pt')
|
15 |
+
|
16 |
+
|
17 |
+
def reformat_freq(sr, y):
|
18 |
+
if len(y.shape)==1 or y.shape[1]==1:
|
19 |
+
pass
|
20 |
+
#print("monocanal")
|
21 |
+
else:
|
22 |
+
# Avg two channels
|
23 |
+
y=y.mean(axis=1)
|
24 |
+
|
25 |
+
y = y.astype(np.float32)
|
26 |
+
if sr not in (
|
27 |
+
FS,
|
28 |
+
):
|
29 |
+
y = librosa.resample(y, orig_sr=sr, target_sr=FS)
|
30 |
+
|
31 |
+
return sr, y
|
32 |
+
|
33 |
+
|
34 |
+
def preprocess_audio(audio):
|
35 |
+
_, y = reformat_freq(*audio)
|
36 |
+
y = y[:MAX_SIZE]
|
37 |
+
y=torch.as_tensor(y,dtype=torch.float32)
|
38 |
+
y=torch.unsqueeze(y,0)
|
39 |
+
|
40 |
+
return y
|
41 |
+
|
42 |
+
def postprocess_output(score):
|
43 |
+
out=score.item()
|
44 |
+
out = round(100*out,2)
|
45 |
+
return "{:.2f}%".format(out)
|
46 |
+
|
47 |
+
def process_youtube_address(youtube_address):
|
48 |
+
print("Downloading youtube audio from video...")
|
49 |
+
|
50 |
+
try:
|
51 |
+
selected_video = YouTube(youtube_address)
|
52 |
+
audio=selected_video.streams.filter(only_audio=True, file_extension='mp4').first()
|
53 |
+
nrand=round(random.random()*1000)
|
54 |
+
audioname="audio-"+str(nrand)+".mp4a"
|
55 |
+
audiowav="audio-"+str(nrand)+".wav"
|
56 |
+
audiomp4a=audio.download('tmp',audioname)
|
57 |
+
os.system("ffmpeg -i " + audiomp4a + " -ac 1 -ar {} ".format(FS) + audiowav + "; rm tmp/" + audioname )
|
58 |
+
except Exception as inst:
|
59 |
+
print("Exception: {}".format(inst))
|
60 |
+
print("ERROR while downloading audio from " + youtube_address)
|
61 |
+
audiowav=None
|
62 |
+
return audiowav
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
def process_micro(micro):
|
68 |
+
x=preprocess_audio(micro)
|
69 |
+
output = MODEL(x)
|
70 |
+
print(output)
|
71 |
+
result = postprocess_output(output)
|
72 |
+
|
73 |
+
return result
|
74 |
+
|
75 |
+
def process_file(file):
|
76 |
+
x,fs = librosa.load(file, sr=FS)
|
77 |
+
x=preprocess_audio((fs,x))
|
78 |
+
print("Running model")
|
79 |
+
output = MODEL(x)
|
80 |
+
print(output)
|
81 |
+
result = postprocess_output(output)
|
82 |
+
|
83 |
+
return result
|
84 |
+
|
85 |
+
def process_youtube(youtube_address):
|
86 |
+
audiofile=process_youtube_address(youtube_address)
|
87 |
+
|
88 |
+
if audiofile is not None:
|
89 |
+
result = process_file(audiofile)
|
90 |
+
return result
|
91 |
+
else:
|
92 |
+
return "Could not get audio from {}".format(youtube_address)
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
with gr.Blocks(title="Audio Fake Detector") as demo:
|
100 |
+
gr.Markdown("# Welcome to Loccus.ai synthetic voice detection demo!")
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
with gr.Row():
|
105 |
+
with gr.Column():
|
106 |
+
m = gr.Audio(source="microphone", type="numpy",label="Micro")
|
107 |
+
f = gr.Audio(source="upload", type="filepath", label="Audio file")
|
108 |
+
y = gr.Textbox(label="Enter YouTube address here")
|
109 |
+
|
110 |
+
|
111 |
+
with gr.Column():
|
112 |
+
with gr.Row(equal_height=True):
|
113 |
+
|
114 |
+
text = gr.Textbox(label="Probability of Real Voice")
|
115 |
+
|
116 |
+
#file= gr.Audio(source="upload", type="filepath", optional=True)
|
117 |
+
button_clear = gr.ClearButton([m,f,y,text])
|
118 |
+
m.stop_recording(process_micro, inputs=[m], outputs=text)
|
119 |
+
f.upload(process_file,inputs=[f], outputs=text)
|
120 |
+
y.submit(process_youtube, inputs=[y], outputs=text)
|
121 |
+
|
122 |
+
|
123 |
+
#btn = gr.Button("Run")
|
124 |
+
#btn.click(fn=update, inputs=inp, outputs=out)
|
125 |
+
|
126 |
+
demo.launch()
|
127 |
+
|