Spaces:
Running
Running
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
|
| 5 |
+
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 6 |
+
|
| 7 |
+
def classify_audio(audio_file):
|
| 8 |
+
"""
|
| 9 |
+
Classify the uploaded audio file using Hugging Face AST model
|
| 10 |
+
"""
|
| 11 |
+
if audio_file is None:
|
| 12 |
+
return "Please upload an audio file."
|
| 13 |
+
|
| 14 |
+
with open(audio_file.name, "rb") as f:
|
| 15 |
+
data = f.read()
|
| 16 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
| 17 |
+
try:
|
| 18 |
+
results = response.json()
|
| 19 |
+
return results
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error processing audio: {str(e)}"
|
| 22 |
+
|
| 23 |
+
# Create Gradio interface
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=classify_audio,
|
| 26 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 27 |
+
outputs=gr.JSON(label="Classification Results"),
|
| 28 |
+
title="Audio Classification using AST Model",
|
| 29 |
+
description="Upload an audio file to get its classification results using the Audio Spectrogram Transformer model.",
|
| 30 |
+
examples=[],
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the interface
|
| 34 |
+
iface.launch()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|