sami606713 commited on
Commit
adf299f
·
verified ·
1 Parent(s): 628b599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -1,13 +1,16 @@
1
  import streamlit as st
2
  import soundfile as sf
3
- from transformers import pipeline, Wav2Vec2ForSequenceClassification, Wav2Vec2Tokenizer
 
 
4
 
5
- # Load the model and tokenizer
6
- model_name = "sami606713/emotion_classification"
7
 
8
- # Initialize the pipeline
9
  try:
10
- classifier = pipeline("audio-classification", model=model_name, tokenizer=model_name)
 
11
  except Exception as e:
12
  st.write(f"Error loading model: {e}")
13
 
@@ -21,6 +24,7 @@ uploaded_file = st.file_uploader("Choose an audio file...", type=["wav", "mp3",
21
  if uploaded_file is not None:
22
  # Load the audio file
23
  audio_input, sample_rate = sf.read(uploaded_file)
 
24
 
25
  # Display the audio player
26
  st.audio(uploaded_file)
@@ -28,10 +32,34 @@ if uploaded_file is not None:
28
  # Perform emotion classification
29
  st.write("Classifying...")
30
  try:
31
- predictions = classifier(audio_input, sampling_rate=sample_rate)
32
-
33
- # Display the results
34
- for prediction in predictions:
35
- st.write(f"Emotion: {prediction['label']}, Score: {prediction['score']:.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  except Exception as e:
37
  st.write(f"Error during classification: {e}")
 
1
  import streamlit as st
2
  import soundfile as sf
3
+ import torch
4
+ from transformers import AutoModel, AutoFeatureExtractor
5
+ import os
6
 
7
+ # Get the Hugging Face API token from environment variables
8
+ token = os.getenv("HF_TOKEN")
9
 
10
+ # Load the model and feature extractor using your token
11
  try:
12
+ model = AutoModel.from_pretrained("sami606713/emotion_classification", use_auth_token=token)
13
+ feature_extractor = AutoFeatureExtractor.from_pretrained("sami606713/emotion_classification", use_auth_token=token)
14
  except Exception as e:
15
  st.write(f"Error loading model: {e}")
16
 
 
24
  if uploaded_file is not None:
25
  # Load the audio file
26
  audio_input, sample_rate = sf.read(uploaded_file)
27
+ sample_rate = 16000 # Ensure the sample rate is 16000
28
 
29
  # Display the audio player
30
  st.audio(uploaded_file)
 
32
  # Perform emotion classification
33
  st.write("Classifying...")
34
  try:
35
+ inputs = feature_extractor(audio_input, sampling_rate=sample_rate, return_tensors="pt")
36
+
37
+ # Make prediction
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
+
41
+ embeddings = outputs.pooler_output
42
+
43
+ # Apply a classification head on top of the embeddings
44
+ id2label={
45
+ 0:"angry",
46
+ 1:'calm',
47
+ 2:'disgust',
48
+ 3:'fearful',
49
+ 4:'happy',
50
+ 5:'neutral',
51
+ 6:'sad',
52
+ 7:'surprised'
53
+ }
54
+ classifier = torch.nn.Linear(embeddings.shape[-1], len(id2label))
55
+
56
+ # Pass embeddings through the classifier
57
+ logits = classifier(embeddings)
58
+
59
+ # Get predicted class
60
+ predicted_class_idx = logits.argmax(-1).item()
61
+ predicted_class = id2label[predicted_class_idx]
62
+
63
+ st.write(f"Predicted Emotion: {predicted_class}")
64
  except Exception as e:
65
  st.write(f"Error during classification: {e}")