YashsharmaPhD commited on
Commit
7a65103
Β·
verified Β·
1 Parent(s): ee6df06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -55
app.py CHANGED
@@ -2,23 +2,22 @@ import streamlit as st
2
  import librosa
3
  import numpy as np
4
  import matplotlib.pyplot as plt
 
5
  from pydub import AudioSegment
6
  from transformers import pipeline
7
  import os
8
- import openai_whisper as whisper
9
- import whisper # OpenAI Whisper for transcription
10
 
11
  # Load pre-trained sentiment analysis model
12
  sentiment_analyzer = pipeline("sentiment-analysis")
13
 
14
  # Streamlit UI
15
- st.title("🎀 Audio Sentiment Analysis")
16
- st.write("Upload multiple MP3 files to analyze sentiment and tone.")
17
 
18
- # Upload multiple audio files
19
- uploaded_files = st.file_uploader("Choose MP3 files", type=["mp3"], accept_multiple_files=True)
20
 
21
- # Function to process audio, get sentiment, and transcribe to text using Whisper
22
  def analyze_audio(file_path):
23
  # Convert MP3 to WAV
24
  audio = AudioSegment.from_mp3(file_path)
@@ -32,63 +31,69 @@ def analyze_audio(file_path):
32
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
33
  mfccs_mean = np.mean(mfccs, axis=1) # Take mean across time axis
34
 
35
- # Perform speech-to-text transcription using Whisper
36
- model = whisper.load_model("base") # Load Whisper model
37
- result = model.transcribe(wav_path)
38
- transcription = result['text']
39
-
40
- # Perform sentiment analysis on the transcription
41
- sentiment_result = sentiment_analyzer(transcription) if transcription else [{"label": "NEGATIVE", "score": 0.5}]
42
-
43
  # Remove WAV file after processing
44
  os.remove(wav_path)
45
 
46
- return sentiment_result[0], mfccs_mean, mfccs, y, sr, transcription
47
-
48
- # Process and plot if files are uploaded
49
- if uploaded_files:
50
- # Create a directory to store temporary files
51
- os.makedirs("temp", exist_ok=True)
52
-
53
- # Prepare a single plot
54
- fig, ax = plt.subplots(figsize=(10, 6))
55
 
56
- for uploaded_file in uploaded_files:
57
- # Save the uploaded file
58
- file_path = f"temp/{uploaded_file.name}"
59
- with open(file_path, "wb") as f:
60
- f.write(uploaded_file.getbuffer())
61
 
62
- # Analyze sentiment, extract features, and get transcription
63
- sentiment, mfccs_mean, mfccs, audio_data, sample_rate, transcription = analyze_audio(file_path)
64
-
65
- # Display sentiment and transcription result
66
- st.subheader(f"πŸ“Š Sentiment Analysis Result for {uploaded_file.name}")
67
- st.write(f"**Transcription:** {transcription}")
68
- st.write(f"**Sentiment:** {sentiment['label']}")
69
- st.write(f"**Confidence:** {sentiment['score']:.2f}")
70
-
71
- # Normalize sentiment score to range from 0 to 1
72
- sentiment_score = sentiment['score'] if sentiment['label'] == 'POSITIVE' else 1 - sentiment['score']
73
 
74
- # Plotting both curves in a single plot
75
- ax.plot(np.linspace(0, len(audio_data) / sample_rate, len(audio_data)), [sentiment_score] * len(audio_data), label="Agent's Tone", linestyle='-', color='b')
76
-
77
- # Ensure that MFCCs are being averaged across time correctly
78
- # Take the mean along the time axis (axis=1) to get one value per MFCC coefficient
79
- mfccs_mean = np.mean(mfccs, axis=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- # Plot Patient's Tone Curve (MFCC mean value per coefficient)
82
- ax.plot(np.linspace(0, len(mfccs_mean), len(mfccs_mean)), mfccs_mean, label="Patient's Tone Curve (MFCCs)", linestyle='--', color='r')
 
 
 
 
 
 
83
 
84
- # Customize plot
85
- ax.set_xlabel("Time (seconds) / MFCC Coefficients")
86
- ax.set_ylabel("Score / Mean MFCC Value")
87
- ax.set_title("Agent Tone & Patient Tone Curve")
88
- ax.legend()
 
 
 
89
 
90
- # Show plot in Streamlit
 
 
 
 
 
 
 
91
  st.pyplot(fig)
92
 
93
- # Clean up temp files
94
  os.remove(file_path)
 
2
  import librosa
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
+ import seaborn as sns
6
  from pydub import AudioSegment
7
  from transformers import pipeline
8
  import os
 
 
9
 
10
  # Load pre-trained sentiment analysis model
11
  sentiment_analyzer = pipeline("sentiment-analysis")
12
 
13
  # Streamlit UI
14
+ st.title("🎀 Audio Sentiment & Feature Analysis")
15
+ st.write("Upload an MP3 file to analyze its sentiment and audio features.")
16
 
17
+ # Upload audio file
18
+ uploaded_file = st.file_uploader("Choose an MP3 file", type=["mp3"])
19
 
20
+ # Function to process audio and get sentiment
21
  def analyze_audio(file_path):
22
  # Convert MP3 to WAV
23
  audio = AudioSegment.from_mp3(file_path)
 
31
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
32
  mfccs_mean = np.mean(mfccs, axis=1) # Take mean across time axis
33
 
34
+ # Dummy text for sentiment analysis (replace with actual text from speech-to-text if needed)
35
+ sentiment_result = sentiment_analyzer("This is a placeholder for sentiment analysis based on audio!")
36
+
 
 
 
 
 
37
  # Remove WAV file after processing
38
  os.remove(wav_path)
39
 
40
+ return y, sr, sentiment_result[0], mfccs, mfccs_mean
 
 
 
 
 
 
 
 
41
 
42
+ # Process and plot if a file is uploaded
43
+ if uploaded_file:
44
+ file_path = f"temp/{uploaded_file.name}"
 
 
45
 
46
+ # Ensure temp directory exists
47
+ os.makedirs("temp", exist_ok=True)
 
 
 
 
 
 
 
 
 
48
 
49
+ # Save the uploaded file
50
+ with open(file_path, "wb") as f:
51
+ f.write(uploaded_file.getbuffer())
52
+
53
+ # Analyze sentiment and extract features
54
+ y, sr, sentiment, mfccs, mfccs_mean = analyze_audio(file_path)
55
+
56
+ # Display sentiment result
57
+ st.subheader("πŸ“Š Sentiment Analysis Result")
58
+ st.write(f"**Sentiment:** {sentiment['label']}")
59
+ st.write(f"**Confidence:** {sentiment['score']:.2f}")
60
+
61
+ # Plot MFCC Bar Chart
62
+ st.subheader("🎡 MFCC Feature Plot")
63
+ fig, ax = plt.subplots()
64
+ ax.bar(range(len(mfccs_mean)), mfccs_mean)
65
+ ax.set_xlabel("MFCC Coefficients")
66
+ ax.set_ylabel("Mean Value")
67
+ ax.set_title("MFCC Feature Extraction")
68
+ st.pyplot(fig)
69
 
70
+ # **Plot Audio Waveform**
71
+ st.subheader("πŸ“ˆ Audio Waveform")
72
+ fig, ax = plt.subplots(figsize=(10, 3))
73
+ librosa.display.waveshow(y, sr=sr, ax=ax, alpha=0.5)
74
+ ax.set_xlabel("Time (s)")
75
+ ax.set_ylabel("Amplitude")
76
+ ax.set_title("Waveform of Audio")
77
+ st.pyplot(fig)
78
 
79
+ # **Plot MFCC Heatmap**
80
+ st.subheader("πŸ”₯ MFCC Heatmap")
81
+ fig, ax = plt.subplots(figsize=(10, 4))
82
+ sns.heatmap(mfccs, cmap="coolwarm", yticklabels=[f"MFCC {i}" for i in range(1, 14)])
83
+ ax.set_xlabel("Time Frames")
84
+ ax.set_ylabel("MFCC Coefficients")
85
+ ax.set_title("MFCC Feature Heatmap")
86
+ st.pyplot(fig)
87
 
88
+ # **Plot Spectrogram**
89
+ st.subheader("🎼 Spectrogram")
90
+ fig, ax = plt.subplots(figsize=(10, 4))
91
+ S = librosa.feature.melspectrogram(y=y, sr=sr)
92
+ S_dB = librosa.power_to_db(S, ref=np.max)
93
+ img = librosa.display.specshow(S_dB, sr=sr, x_axis="time", y_axis="mel", ax=ax)
94
+ fig.colorbar(img, ax=ax, format="%+2.0f dB")
95
+ ax.set_title("Mel Spectrogram")
96
  st.pyplot(fig)
97
 
98
+ # Clean up temp file
99
  os.remove(file_path)