Spaces:
Sleeping
Sleeping
Initial commit with FastAPI + Gradio app
Browse files
app.py
CHANGED
@@ -29,21 +29,28 @@ def get_similarity(audio1, audio2, sample_rate=16000):
|
|
29 |
score, prediction = speaker_verification.verify_batch(signal1, signal2)
|
30 |
return float(score), "Yes" if prediction else "No"
|
31 |
except Exception as e:
|
32 |
-
return str(e)
|
33 |
|
34 |
# API function to compare voices
|
35 |
def compare_voices(file1, file2):
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
|
|
42 |
|
43 |
-
|
44 |
-
return "
|
45 |
|
46 |
-
|
|
|
|
|
47 |
|
48 |
# FastAPI app
|
49 |
app = FastAPI()
|
@@ -53,24 +60,18 @@ async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile =
|
|
53 |
"""
|
54 |
Compare two audio files and return the similarity score and prediction.
|
55 |
"""
|
56 |
-
# Gradio uses numpy arrays directly, so no need to save the files
|
57 |
-
# You'd need to process the audio files here, but in FastAPI you need to convert file to numpy first.
|
58 |
try:
|
|
|
59 |
file1_data = await file1.read()
|
60 |
file2_data = await file2.read()
|
61 |
|
62 |
-
#
|
63 |
-
#
|
64 |
-
|
65 |
-
#
|
66 |
-
#
|
67 |
-
# numpy1 = torchaudio.load(io.BytesIO(file1_data))[0].numpy()
|
68 |
-
# numpy2 = torchaudio.load(io.BytesIO(file2_data))[0].numpy()
|
69 |
-
|
70 |
-
# For this example, the audio should be pre-converted to numpy arrays before processing.
|
71 |
|
72 |
-
|
73 |
-
return {"message": "Processing files directly (no save to disk)"}
|
74 |
|
75 |
except Exception as e:
|
76 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
29 |
score, prediction = speaker_verification.verify_batch(signal1, signal2)
|
30 |
return float(score), "Yes" if prediction else "No"
|
31 |
except Exception as e:
|
32 |
+
return None, str(e) # Return error message if any exception
|
33 |
|
34 |
# API function to compare voices
|
35 |
def compare_voices(file1, file2):
|
36 |
+
try:
|
37 |
+
# Gradio Audio returns a tuple of (audio, sample_rate)
|
38 |
+
audio1, _ = file1 # Audio1 is a tuple (numpy_array, sample_rate)
|
39 |
+
audio2, _ = file2 # Audio2 is a tuple (numpy_array, sample_rate)
|
40 |
+
|
41 |
+
# Get similarity score
|
42 |
+
score, is_same_user = get_similarity(audio1, audio2)
|
43 |
|
44 |
+
if score is None:
|
45 |
+
# Return the error message if processing fails
|
46 |
+
return {"error": is_same_user}
|
47 |
|
48 |
+
# Return a dictionary with the similarity score and prediction
|
49 |
+
return {"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user}
|
50 |
|
51 |
+
except Exception as e:
|
52 |
+
# Handle unexpected errors
|
53 |
+
return {"error": str(e)}
|
54 |
|
55 |
# FastAPI app
|
56 |
app = FastAPI()
|
|
|
60 |
"""
|
61 |
Compare two audio files and return the similarity score and prediction.
|
62 |
"""
|
|
|
|
|
63 |
try:
|
64 |
+
# Process the audio files and return them as numpy arrays
|
65 |
file1_data = await file1.read()
|
66 |
file2_data = await file2.read()
|
67 |
|
68 |
+
# You need to process these byte strings into numpy arrays
|
69 |
+
# Assuming the audio is decoded into numpy arrays here (e.g., using torchaudio)
|
70 |
+
# For example:
|
71 |
+
# audio1 = torchaudio.load(io.BytesIO(file1_data))[0].numpy()
|
72 |
+
# audio2 = torchaudio.load(io.BytesIO(file2_data))[0].numpy()
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
return {"message": "Processing files directly without saving them."}
|
|
|
75 |
|
76 |
except Exception as e:
|
77 |
raise HTTPException(status_code=400, detail=str(e))
|