adi-123 commited on
Commit
9833711
·
verified ·
1 Parent(s): 2ba30ea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import os
4
+
5
+ # Define your Hugging Face token; secure this appropriately
6
+ HF_TOKEN = os.getenv('HF_TOKEN')
7
+
8
+ # Set up Hugging Face pipeline for audio classification using the specified model
9
+ model_name = "MIT/ast-finetuned-audioset-10-10-0.4593"
10
+
11
+ @st.cache(allow_output_mutation=True)
12
+ def load_model(token, model_name):
13
+ return pipeline("audio-classification", model=model_name, use_auth_token=token)
14
+
15
+ audio_classifier = load_model(HF_TOKEN, model_name)
16
+
17
+ # Pre-uploaded audio files
18
+ audio_files = {
19
+ "Labrador Barking": "labrador-barking.mp3",
20
+ "Tolling Bell": "tolling-bell.mp3",
21
+ "Airplane Landing": "airplane-landing.mp3",
22
+ "Old Car Engine": "old-car-engine.mp3",
23
+ "Hard Shoes": "hard_shoes.mp3",
24
+ "Alien Spaceship": "alien-spaceship.mp3",
25
+ }
26
+
27
+ # Streamlit UI
28
+ st.title("Audio Classification with Pre-uploaded Files")
29
+
30
+ # Audio file selection
31
+ selected_audio_name = st.selectbox("Select an audio file", list(audio_files.keys()))
32
+ audio_file_path = audio_files[selected_audio_name]
33
+
34
+ # Perform classification
35
+ if st.button("Classify"):
36
+ # Read audio file
37
+ with open(audio_file_path, "rb") as audio_file:
38
+ audio_bytes = audio_file.read()
39
+
40
+ results = audio_classifier(audio_bytes)
41
+
42
+ # Displaying results
43
+ st.write("Classification Results:")
44
+ for result in results:
45
+ label = result['label']
46
+ score = round(result['score'], 4) # Adjust rounding as needed
47
+ st.write(f"Label: {label}, Score: {score}")