Update app.py
Browse files
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
|
| 17 |
|
| 18 |
-
# Upload
|
| 19 |
-
|
| 20 |
|
| 21 |
-
# Function to process audio
|
| 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 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 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],
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
with open(file_path, "wb") as f:
|
| 60 |
-
f.write(uploaded_file.getbuffer())
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
ax.
|
| 87 |
-
|
| 88 |
-
ax.
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
st.pyplot(fig)
|
| 92 |
|
| 93 |
-
# Clean up temp
|
| 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)
|