Spaces:
Sleeping
Sleeping
Initial commit with FastAPI + Gradio app
Browse files
app.py
CHANGED
@@ -1,87 +1,62 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import gradio as gr
|
4 |
import torch
|
5 |
-
|
|
|
6 |
from speechbrain.inference import SpeakerRecognition
|
7 |
from fastapi.responses import JSONResponse
|
8 |
-
import numpy as np
|
9 |
|
10 |
# Initialize the speaker verification model
|
11 |
-
|
12 |
source="speechbrain/spkrec-ecapa-voxceleb",
|
13 |
savedir="tmp_model"
|
14 |
)
|
15 |
|
|
|
|
|
|
|
16 |
# Function to calculate similarity score
|
17 |
-
def get_similarity(
|
18 |
try:
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Get similarity score and prediction
|
30 |
-
score, prediction = speaker_verification.verify_batch(signal1, signal2)
|
31 |
-
return float(score), "Yes" if prediction else "No"
|
32 |
except Exception as e:
|
33 |
-
return None, str(e) # Return error message if any exception
|
34 |
-
|
35 |
-
# API function to compare voices
|
36 |
-
def compare_voices(file1, file2):
|
37 |
-
try:
|
38 |
-
# Debugging: Check the types of inputs
|
39 |
-
print(f"Received file1: {type(file1)}")
|
40 |
-
print(f"Received file2: {type(file2)}")
|
41 |
-
|
42 |
-
# Ensure file1 and file2 are numpy arrays
|
43 |
-
if isinstance(file1, np.ndarray) and isinstance(file2, np.ndarray):
|
44 |
-
audio1, audio2 = file1, file2
|
45 |
-
else:
|
46 |
-
return {"error": "Invalid input format. Both inputs must be numpy arrays."}
|
47 |
-
|
48 |
-
# Get similarity score
|
49 |
-
score, is_same_user = get_similarity(audio1, audio2)
|
50 |
-
|
51 |
-
if score is None:
|
52 |
-
# Return the error message if processing fails
|
53 |
-
return {"error": is_same_user}
|
54 |
-
|
55 |
-
# Return a dictionary with the similarity score and prediction
|
56 |
-
return {"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user}
|
57 |
-
|
58 |
-
except Exception as e:
|
59 |
-
# Handle unexpected errors
|
60 |
return {"error": str(e)}
|
61 |
|
62 |
-
#
|
63 |
-
app = FastAPI()
|
64 |
-
|
65 |
@app.post("/compare_voices/")
|
66 |
async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
67 |
"""
|
68 |
Compare two audio files and return the similarity score and prediction.
|
69 |
"""
|
70 |
try:
|
71 |
-
#
|
72 |
file1_data = await file1.read()
|
73 |
file2_data = await file2.read()
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
audio1 = audio1.numpy()
|
81 |
-
audio2 = audio2.numpy()
|
82 |
-
|
83 |
-
# Compare the two audio files and return the result
|
84 |
-
return compare_voices(audio1, audio2)
|
85 |
|
86 |
except Exception as e:
|
87 |
raise HTTPException(status_code=400, detail=str(e))
|
@@ -89,21 +64,21 @@ async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile =
|
|
89 |
# Gradio interface function
|
90 |
def gradio_interface():
|
91 |
return gr.Interface(
|
92 |
-
fn=
|
93 |
inputs=[
|
94 |
-
gr.Audio(type="
|
95 |
-
gr.Audio(type="
|
96 |
],
|
97 |
-
outputs="json", # Output
|
98 |
live=False # No live interface, just the API
|
99 |
)
|
100 |
|
101 |
-
# Launch Gradio
|
102 |
@app.on_event("startup")
|
103 |
async def startup():
|
104 |
-
gr.Interface(fn=
|
105 |
-
gr.Audio(type="
|
106 |
-
gr.Audio(type="
|
107 |
], outputs="json", live=False).launch(share=True, inline=True)
|
108 |
|
109 |
# Running the FastAPI app with Gradio
|
|
|
1 |
+
import io
|
2 |
+
import tempfile
|
|
|
3 |
import torch
|
4 |
+
import gradio as gr
|
5 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
6 |
from speechbrain.inference import SpeakerRecognition
|
7 |
from fastapi.responses import JSONResponse
|
|
|
8 |
|
9 |
# Initialize the speaker verification model
|
10 |
+
verification = SpeakerRecognition.from_hparams(
|
11 |
source="speechbrain/spkrec-ecapa-voxceleb",
|
12 |
savedir="tmp_model"
|
13 |
)
|
14 |
|
15 |
+
# FastAPI app
|
16 |
+
app = FastAPI()
|
17 |
+
|
18 |
# Function to calculate similarity score
|
19 |
+
def get_similarity(file1_data, file2_data):
|
20 |
try:
|
21 |
+
# Create temporary files for the uploaded audio
|
22 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile1, \
|
23 |
+
tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile2:
|
24 |
+
|
25 |
+
# Write audio data to the temporary files
|
26 |
+
tmpfile1.write(file1_data)
|
27 |
+
tmpfile2.write(file2_data)
|
28 |
+
|
29 |
+
# Get the file paths
|
30 |
+
file1_path = tmpfile1.name
|
31 |
+
file2_path = tmpfile2.name
|
32 |
+
|
33 |
+
# Use `verify_files` to compare the audio files
|
34 |
+
score, prediction = verification.verify_files(file1_path, file2_path)
|
35 |
+
|
36 |
+
# Return the result as a dictionary
|
37 |
+
return {
|
38 |
+
"Similarity Score": f"{score:.4f}",
|
39 |
+
"Same User Prediction": "Yes" if prediction else "No"
|
40 |
+
}
|
41 |
|
|
|
|
|
|
|
42 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return {"error": str(e)}
|
44 |
|
45 |
+
# API function to compare voices
|
|
|
|
|
46 |
@app.post("/compare_voices/")
|
47 |
async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
48 |
"""
|
49 |
Compare two audio files and return the similarity score and prediction.
|
50 |
"""
|
51 |
try:
|
52 |
+
# Read the uploaded file data
|
53 |
file1_data = await file1.read()
|
54 |
file2_data = await file2.read()
|
55 |
|
56 |
+
# Call the get_similarity function with file data
|
57 |
+
result = get_similarity(file1_data, file2_data)
|
58 |
+
|
59 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
except Exception as e:
|
62 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
64 |
# Gradio interface function
|
65 |
def gradio_interface():
|
66 |
return gr.Interface(
|
67 |
+
fn=compare_voices_api, # FastAPI function is wrapped here
|
68 |
inputs=[
|
69 |
+
gr.Audio(type="file", label="First Audio File"), # Audio file input
|
70 |
+
gr.Audio(type="file", label="Second Audio File") # Audio file input
|
71 |
],
|
72 |
+
outputs="json", # Output as JSON
|
73 |
live=False # No live interface, just the API
|
74 |
)
|
75 |
|
76 |
+
# Launch Gradio interface
|
77 |
@app.on_event("startup")
|
78 |
async def startup():
|
79 |
+
gr.Interface(fn=compare_voices_api, inputs=[
|
80 |
+
gr.Audio(type="file", label="First Audio File"), # Audio file input
|
81 |
+
gr.Audio(type="file", label="Second Audio File") # Audio file input
|
82 |
], outputs="json", live=False).launch(share=True, inline=True)
|
83 |
|
84 |
# Running the FastAPI app with Gradio
|