File size: 1,686 Bytes
9833711
44209c6
9833711
 
44209c6
 
 
 
9833711
44209c6
 
 
 
 
 
9833711
44209c6
9833711
 
 
 
 
 
 
 
 
 
44209c6
9833711
 
 
44209c6
9833711
 
 
44209c6
9833711
 
 
44209c6
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)