Nusri7 commited on
Commit
28b1c5e
·
1 Parent(s): d39f82c

Initial commit for Gradio API

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import torchaudio
4
+ import gradio as gr
5
+ from speechbrain.inference import SpeakerRecognition
6
+ from fastapi import HTTPException
7
+
8
+ # Initialize the speaker verification model
9
+ speaker_verification = SpeakerRecognition.from_hparams(
10
+ source="speechbrain/spkrec-ecapa-voxceleb",
11
+ savedir="tmp_model"
12
+ )
13
+
14
+ # Temporary folder to save uploaded files
15
+ UPLOAD_FOLDER = "uploaded_audio"
16
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
17
+
18
+ # Function to calculate similarity score
19
+ def get_similarity(audio_path1: str, audio_path2: str):
20
+ try:
21
+ # Load audio files
22
+ signal1, _ = torchaudio.load(audio_path1)
23
+ signal2, _ = torchaudio.load(audio_path2)
24
+
25
+ # Get similarity score and prediction
26
+ score, prediction = speaker_verification.verify_batch(signal1, signal2)
27
+ return float(score), "Yes" if prediction else "No"
28
+ except Exception as e:
29
+ return str(e), None
30
+ finally:
31
+ # Clean up temporary files
32
+ if os.path.exists(audio_path1):
33
+ os.remove(audio_path1)
34
+ if os.path.exists(audio_path2):
35
+ os.remove(audio_path2)
36
+
37
+ # API function to compare voices
38
+ def compare_voices(file1, file2):
39
+ # Save uploaded files temporarily
40
+ file1_path = os.path.join(UPLOAD_FOLDER, file1.name)
41
+ file2_path = os.path.join(UPLOAD_FOLDER, file2.name)
42
+
43
+ with open(file1_path, "wb") as f1:
44
+ f1.write(file1.read())
45
+ with open(file2_path, "wb") as f2:
46
+ f2.write(file2.read())
47
+
48
+ # Get similarity score
49
+ score, is_same_user = get_similarity(file1_path, file2_path)
50
+
51
+ if is_same_user is None:
52
+ return "Error: " + score # This will return the error message
53
+
54
+ return {"Similarity Score": f"{score:.4f}", "Same User Prediction": is_same_user}
55
+
56
+ # Create Gradio Interface for the API
57
+ api = gr.Interface(
58
+ fn=compare_voices,
59
+ inputs=[
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 the API as an HTTP server
68
+ api.queue().launch(server_name="0.0.0.0", server_port=8080, share=False)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ numpy
4
+ scikit-learn
5
+ joblib
6
+ torchaudio
7
+ speechbrain
8
+ python-multipart
9
+ gradio