adi-123 commited on
Commit
44209c6
·
verified ·
1 Parent(s): 3a80dfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -1,20 +1,20 @@
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",
@@ -25,23 +25,22 @@ audio_files = {
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}")
 
 
 
 
1
  import streamlit as st
2
+ import requests
3
  import os
4
 
5
+ # Hugging Face API setup
6
+ API_URL = "https://api-inference.huggingface.co/models/MIT/ast-finetuned-audioset-10-10-0.4593"
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",
 
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)