Spaces:
Running
Running
Initial commit with FastAPI + Gradio app
Browse files- app.py +48 -12
- requirements.txt +1 -9
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import os
|
2 |
-
import shutil
|
3 |
import torchaudio
|
4 |
import gradio as gr
|
|
|
5 |
from speechbrain.inference import SpeakerRecognition
|
6 |
-
from fastapi import
|
7 |
|
8 |
# Initialize the speaker verification model
|
9 |
speaker_verification = SpeakerRecognition.from_hparams(
|
@@ -53,16 +53,52 @@ def compare_voices(file1, file2):
|
|
53 |
|
54 |
return {"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user}
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
gr.Audio(source="upload", type="file", label="First Audio File"),
|
61 |
gr.Audio(source="upload", type="file", label="Second Audio File")
|
62 |
-
],
|
63 |
-
outputs="json", # Output results as JSON
|
64 |
-
live=False # No live interface, just the API
|
65 |
-
)
|
66 |
|
67 |
-
# Launch
|
68 |
-
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import torchaudio
|
3 |
import gradio as gr
|
4 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile
|
5 |
from speechbrain.inference import SpeakerRecognition
|
6 |
+
from fastapi.responses import JSONResponse
|
7 |
|
8 |
# Initialize the speaker verification model
|
9 |
speaker_verification = SpeakerRecognition.from_hparams(
|
|
|
53 |
|
54 |
return {"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user}
|
55 |
|
56 |
+
# FastAPI app
|
57 |
+
app = FastAPI()
|
58 |
+
|
59 |
+
@app.post("/compare_voices/")
|
60 |
+
async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile = File(...)):
|
61 |
+
"""
|
62 |
+
Compare two audio files and return the similarity score and prediction.
|
63 |
+
"""
|
64 |
+
# Save uploaded files temporarily
|
65 |
+
file1_path = os.path.join(UPLOAD_FOLDER, file1.filename)
|
66 |
+
file2_path = os.path.join(UPLOAD_FOLDER, file2.filename)
|
67 |
+
|
68 |
+
with open(file1_path, "wb") as f1:
|
69 |
+
f1.write(await file1.read())
|
70 |
+
with open(file2_path, "wb") as f2:
|
71 |
+
f2.write(await file2.read())
|
72 |
+
|
73 |
+
# Get similarity score
|
74 |
+
score, is_same_user = get_similarity(file1_path, file2_path)
|
75 |
+
|
76 |
+
if is_same_user is None:
|
77 |
+
raise HTTPException(status_code=500, detail="Error in processing files: " + score)
|
78 |
+
|
79 |
+
return JSONResponse(content={"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user})
|
80 |
+
|
81 |
+
# Gradio interface function
|
82 |
+
def gradio_interface():
|
83 |
+
return gr.Interface(
|
84 |
+
fn=compare_voices,
|
85 |
+
inputs=[
|
86 |
+
gr.Audio(source="upload", type="file", label="First Audio File"),
|
87 |
+
gr.Audio(source="upload", type="file", label="Second Audio File")
|
88 |
+
],
|
89 |
+
outputs="json", # Output results as JSON
|
90 |
+
live=False # No live interface, just the API
|
91 |
+
)
|
92 |
+
|
93 |
+
# Launch Gradio as a web interface
|
94 |
+
@app.on_event("startup")
|
95 |
+
async def startup():
|
96 |
+
gr.Interface(fn=compare_voices, inputs=[
|
97 |
gr.Audio(source="upload", type="file", label="First Audio File"),
|
98 |
gr.Audio(source="upload", type="file", label="Second Audio File")
|
99 |
+
], outputs="json", live=False).launch(share=True, inline=True)
|
|
|
|
|
|
|
100 |
|
101 |
+
# Launch FastAPI with Gradio and FastAPI routes
|
102 |
+
if __name__ == "__main__":
|
103 |
+
import uvicorn
|
104 |
+
uvicorn.run(app, host="0.0.0.0", port=5000)
|
requirements.txt
CHANGED
@@ -1,9 +1 @@
|
|
1 |
-
|
2 |
-
uvicorn
|
3 |
-
numpy
|
4 |
-
scikit-learn
|
5 |
-
joblib
|
6 |
-
torchaudio
|
7 |
-
speechbrain
|
8 |
-
python-multipart
|
9 |
-
gradio
|
|
|
1 |
+
git+https://github.com/speechbrain/speechbrain.git@develop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|