adi-123 commited on
Commit
f197f2f
·
verified ·
1 Parent(s): 7a3f98d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -7,40 +7,39 @@ API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audiose
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
9
 
10
- # Function to send audio file to the Hugging Face model for classification
11
- def query(filename):
12
- with open(filename, "rb") as f:
13
- data = f.read()
14
- response = requests.post(API_URL, headers=headers, files={"file": f})
 
 
 
15
  return response.json()
16
 
17
- # Pre-uploaded audio files (assuming these files are stored in a directory named '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 Hugging Face Inference API")
29
 
30
- # Audio file selection
31
- selected_audio_name = st.selectbox("Select an audio file", list(audio_files.keys()))
32
- audio_file_path = os.path.join("path_to_your_audio_files", audio_files[selected_audio_name]) # Update path as necessary
33
 
34
- # Perform classification
35
- if st.button("Classify"):
36
- results = query(audio_file_path)
37
 
38
- # Displaying results
39
- st.write("Classification Results:")
40
- if isinstance(results, list): # Check if the response is as expected
41
- for result in results:
42
- label = result['label']
43
- score = round(result['score'], 4) # Adjust rounding as needed
44
- st.write(f"Label: {label}, Score: {score}")
45
- else:
46
- st.write("An error occurred:", results)
 
 
 
 
 
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
9
 
10
+ # Function to send the audio file to the Hugging Face API and get the classification result
11
+ def classify_audio(audio_file_path):
12
+ with open(audio_file_path, "rb") as audio_file:
13
+ response = requests.post(
14
+ "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593",
15
+ headers=headers,
16
+ files={"file": audio_file}
17
+ )
18
  return response.json()
19
 
20
+ # Streamlit interface
21
+ st.title("Audio Classifier")
 
 
 
 
 
 
 
22
 
23
+ # Define the folder where your audio files are located
24
+ audio_folder = "audio_files"
25
 
26
+ # List the audio files in the folder
27
+ audio_files = os.listdir(audio_folder)
28
+ audio_file_options = [f for f in audio_files if f.endswith(('.mp3', '.wav'))]
29
 
30
+ # Dropdown to select an audio file
31
+ selected_file = st.selectbox("Select an audio file:", audio_file_options)
 
32
 
33
+ # Button to classify the selected audio file
34
+ if st.button("Classify"):
35
+ # Get the full path of the selected audio file
36
+ audio_file_path = os.path.join(audio_folder, selected_file)
37
+
38
+ # Show the audio player
39
+ st.audio(audio_file_path)
40
+
41
+ # Get and display the classification results
42
+ results = classify_audio(audio_file_path)
43
+ st.write("Results:")
44
+ for result in results:
45
+ st.write(f"Label: {result['label']}, Confidence: {result['score']:.2f}")