Nusri7 commited on
Commit
3b9b39b
·
1 Parent(s): 1cd14c6

Initial commit with FastAPI + Gradio app

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -14,20 +14,8 @@ verification = SpeakerRecognition.from_hparams(
14
  app = FastAPI()
15
 
16
  # Function to calculate similarity score
17
- def get_similarity(file1_data, file2_data):
18
  try:
19
- # Create temporary files for the uploaded audio
20
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile1, \
21
- tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile2:
22
-
23
- # Write audio data to the temporary files
24
- tmpfile1.write(file1_data)
25
- tmpfile2.write(file2_data)
26
-
27
- # Get the file paths
28
- file1_path = tmpfile1.name
29
- file2_path = tmpfile2.name
30
-
31
  # Use `verify_files` to compare the audio files
32
  score, prediction = verification.verify_files(file1_path, file2_path)
33
 
@@ -47,12 +35,20 @@ async def compare_voices_api(file1: UploadFile = File(...), file2: UploadFile =
47
  Compare two audio files and return the similarity score and prediction.
48
  """
49
  try:
50
- # Read the uploaded file data
51
- file1_data = await file1.read()
52
- file2_data = await file2.read()
 
 
 
 
 
 
 
 
53
 
54
- # Call the get_similarity function with file data
55
- result = get_similarity(file1_data, file2_data)
56
 
57
  return result
58
 
@@ -64,8 +60,8 @@ def gradio_interface():
64
  return gr.Interface(
65
  fn=compare_voices_api, # FastAPI function is wrapped here
66
  inputs=[
67
- gr.Audio(type="file", label="First Audio File"), # Audio file input
68
- gr.Audio(type="file", label="Second Audio File") # Audio file input
69
  ],
70
  outputs="json", # Output as JSON
71
  live=False # No live interface, just the API
@@ -75,8 +71,8 @@ def gradio_interface():
75
  @app.on_event("startup")
76
  async def startup():
77
  gr.Interface(fn=compare_voices_api, inputs=[
78
- gr.Audio(type="file", label="First Audio File"), # Audio file input
79
- gr.Audio(type="file", label="Second Audio File") # Audio file input
80
  ], outputs="json", live=False).launch(share=True, inline=True)
81
 
82
  # Running the FastAPI app with Gradio
 
14
  app = FastAPI()
15
 
16
  # Function to calculate similarity score
17
+ def get_similarity(file1_path, file2_path):
18
  try:
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Use `verify_files` to compare the audio files
20
  score, prediction = verification.verify_files(file1_path, file2_path)
21
 
 
35
  Compare two audio files and return the similarity score and prediction.
36
  """
37
  try:
38
+ # Create temporary files for the uploaded audio
39
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile1, \
40
+ tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile2:
41
+
42
+ # Write audio data to the temporary files
43
+ tmpfile1.write(await file1.read())
44
+ tmpfile2.write(await file2.read())
45
+
46
+ # Get the file paths
47
+ file1_path = tmpfile1.name
48
+ file2_path = tmpfile2.name
49
 
50
+ # Call the get_similarity function with file paths
51
+ result = get_similarity(file1_path, file2_path)
52
 
53
  return result
54
 
 
60
  return gr.Interface(
61
  fn=compare_voices_api, # FastAPI function is wrapped here
62
  inputs=[
63
+ gr.Audio(type="filepath", label="First Audio File"), # Audio file input
64
+ gr.Audio(type="filepath", label="Second Audio File") # Audio file input
65
  ],
66
  outputs="json", # Output as JSON
67
  live=False # No live interface, just the API
 
71
  @app.on_event("startup")
72
  async def startup():
73
  gr.Interface(fn=compare_voices_api, inputs=[
74
+ gr.Audio(type="filepath", label="First Audio File"), # Audio file input
75
+ gr.Audio(type="filepath", label="Second Audio File") # Audio file input
76
  ], outputs="json", live=False).launch(share=True, inline=True)
77
 
78
  # Running the FastAPI app with Gradio