adi-123's picture
Update app.py
44209c6 verified
raw
history blame
1.69 kB
import streamlit as st
import requests
import os
# Hugging Face API setup
API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
HF_TOKEN = os.getenv("HF_TOKEN")
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
# Function to send audio file to the Hugging Face model for classification
def query(filename):
with open(filename, "rb") as f:
data = f.read()
response = requests.post(API_URL, headers=headers, files={"file": f})
return response.json()
# Pre-uploaded audio files (assuming these files are stored in a directory named 'audio_files')
audio_files = {
"Labrador Barking": "labrador-barking.mp3",
"Tolling Bell": "tolling-bell.mp3",
"Airplane Landing": "airplane-landing.mp3",
"Old Car Engine": "old-car-engine.mp3",
"Hard Shoes": "hard_shoes.mp3",
"Alien Spaceship": "alien-spaceship.mp3",
}
# Streamlit UI
st.title("Audio Classification with Hugging Face Inference API")
# Audio file selection
selected_audio_name = st.selectbox("Select an audio file", list(audio_files.keys()))
audio_file_path = os.path.join("path_to_your_audio_files", audio_files[selected_audio_name]) # Update path as necessary
# Perform classification
if st.button("Classify"):
results = query(audio_file_path)
# Displaying results
st.write("Classification Results:")
if isinstance(results, list): # Check if the response is as expected
for result in results:
label = result['label']
score = round(result['score'], 4) # Adjust rounding as needed
st.write(f"Label: {label}, Score: {score}")
else:
st.write("An error occurred:", results)