fosters commited on
Commit
f6c5111
·
verified ·
1 Parent(s): 5742c13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -16
app.py CHANGED
@@ -20,37 +20,26 @@ pipe = pipeline(
20
 
21
 
22
  # --- Core Logic Function ---
23
- def classify_audio(audio_filepath):
24
  """
25
  Classifies the audio, takes the top 3 predictions,
26
  and formats them into a single, human-readable string.
27
  """
28
- if audio_filepath is None:
29
  return "Please upload an audio file first."
30
 
31
- preds = pipe(audio_filepath)
32
-
33
- # Format the output as a string instead of a dictionary
34
- # This is the key change to fix the TypeError
35
- output_str = ""
36
- for i, pred in enumerate(preds[:3]):
37
- label = pred["label"]
38
- score = pred["score"]
39
- output_str += f"{i+1}. {label}: {score:.4f}\n"
40
-
41
- return output_str.strip()
42
-
43
 
44
  # --- Gradio Interface ---
45
  # Create the Gradio app interface
46
  app = gr.Interface(
47
  fn=classify_audio,
48
  inputs=gr.Audio(type="filepath", label="Upload Audio File"),
49
- outputs=gr.Label(label="Top 3 Predictions"), # This will now receive a simple string
50
  title="Audio Classification with MIT/AST",
51
  description=(
52
  "Upload an audio file to classify it. The model will identify the top 3 most likely sound categories. "
53
- "This version is corrected to avoid common Gradio backend errors."
54
  ),
55
  cache_examples=False,
56
  )
 
20
 
21
 
22
  # --- Core Logic Function ---
23
+ def classify_audio(audio):
24
  """
25
  Classifies the audio, takes the top 3 predictions,
26
  and formats them into a single, human-readable string.
27
  """
28
+ if audio is None:
29
  return "Please upload an audio file first."
30
 
31
+ result = pipe(audio)
32
+ return {label['label']: label['score'] for label in result}
 
 
 
 
 
 
 
 
 
 
33
 
34
  # --- Gradio Interface ---
35
  # Create the Gradio app interface
36
  app = gr.Interface(
37
  fn=classify_audio,
38
  inputs=gr.Audio(type="filepath", label="Upload Audio File"),
39
+ outputs=gr.Label(num_top_classes=3), # This will now receive a simple string
40
  title="Audio Classification with MIT/AST",
41
  description=(
42
  "Upload an audio file to classify it. The model will identify the top 3 most likely sound categories. "
 
43
  ),
44
  cache_examples=False,
45
  )