Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper # Import whisper from OpenAI
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 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 Analysis")
|
| 15 |
+
st.write("Upload multiple MP3 files to analyze sentiment and tone.")
|
| 16 |
+
|
| 17 |
+
# Upload multiple audio files
|
| 18 |
+
uploaded_files = st.file_uploader("Choose MP3 files", type=["mp3"], accept_multiple_files=True)
|
| 19 |
+
|
| 20 |
+
# Function to process audio, get sentiment, and transcribe to text using Whisper
|
| 21 |
+
def analyze_audio(file_path):
|
| 22 |
+
# Convert MP3 to WAV
|
| 23 |
+
audio = AudioSegment.from_mp3(file_path)
|
| 24 |
+
wav_path = file_path.replace(".mp3", ".wav")
|
| 25 |
+
audio.export(wav_path, format="wav")
|
| 26 |
+
|
| 27 |
+
# Load audio
|
| 28 |
+
y, sr = librosa.load(wav_path, sr=None)
|
| 29 |
+
|
| 30 |
+
# Extract MFCCs (Mel-frequency cepstral coefficients)
|
| 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 |
+
# Perform speech-to-text transcription using Whisper
|
| 35 |
+
model = whisper.load_model("base") # Load Whisper model (change model size as needed)
|
| 36 |
+
result = model.transcribe(wav_path)
|
| 37 |
+
transcription = result['text']
|
| 38 |
+
|
| 39 |
+
# Perform sentiment analysis on the transcription
|
| 40 |
+
sentiment_result = sentiment_analyzer(transcription) if transcription else [{"label": "NEGATIVE", "score": 0.5}]
|
| 41 |
+
|
| 42 |
+
# Remove WAV file after processing
|
| 43 |
+
os.remove(wav_path)
|
| 44 |
+
|
| 45 |
+
return sentiment_result[0], mfccs_mean, mfccs, y, sr, transcription
|
| 46 |
+
|
| 47 |
+
# Process and plot if files are uploaded
|
| 48 |
+
if uploaded_files:
|
| 49 |
+
# Create a directory to store temporary files
|
| 50 |
+
os.makedirs("temp", exist_ok=True)
|
| 51 |
+
|
| 52 |
+
# Prepare a single plot
|
| 53 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 54 |
+
|
| 55 |
+
for uploaded_file in uploaded_files:
|
| 56 |
+
# Save the uploaded file
|
| 57 |
+
file_path = f"temp/{uploaded_file.name}"
|
| 58 |
+
with open(file_path, "wb") as f:
|
| 59 |
+
f.write(uploaded_file.getbuffer())
|
| 60 |
+
|
| 61 |
+
# Analyze sentiment, extract features, and get transcription
|
| 62 |
+
sentiment, mfccs_mean, mfccs, audio_data, sample_rate, transcription = analyze_audio(file_path)
|
| 63 |
+
|
| 64 |
+
# Display sentiment and transcription result
|
| 65 |
+
st.subheader(f"📊 Sentiment Analysis Result for {uploaded_file.name}")
|
| 66 |
+
st.write(f"**Transcription:** {transcription}")
|
| 67 |
+
st.write(f"**Sentiment:** {sentiment['label']}")
|
| 68 |
+
st.write(f"**Confidence:** {sentiment['score']:.2f}")
|
| 69 |
+
|
| 70 |
+
# Normalize sentiment score to range from 0 to 1
|
| 71 |
+
sentiment_score = sentiment['score'] if sentiment['label'] == 'POSITIVE' else 1 - sentiment['score']
|
| 72 |
+
|
| 73 |
+
# Plotting both curves in a single plot
|
| 74 |
+
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')
|
| 75 |
+
|
| 76 |
+
# Ensure that MFCCs are being averaged across time correctly
|
| 77 |
+
# Take the mean along the time axis (axis=1) to get one value per MFCC coefficient
|
| 78 |
+
mfccs_mean = np.mean(mfccs, axis=1)
|
| 79 |
+
|
| 80 |
+
# Plot Patient's Tone Curve (MFCC mean value per coefficient)
|
| 81 |
+
ax.plot(np.linspace(0, len(mfccs_mean), len(mfccs_mean)), mfccs_mean, label="Patient's Tone Curve (MFCCs)", linestyle='--', color='r')
|
| 82 |
+
|
| 83 |
+
# Customize plot
|
| 84 |
+
ax.set_xlabel("Time (seconds) / MFCC Coefficients")
|
| 85 |
+
ax.set_ylabel("Score / Mean MFCC Value")
|
| 86 |
+
ax.set_title("Agent Tone & Patient Tone Curve")
|
| 87 |
+
ax.legend()
|
| 88 |
+
|
| 89 |
+
# Show plot in Streamlit
|
| 90 |
+
st.pyplot(fig)
|
| 91 |
+
|
| 92 |
+
# Clean up temp files
|
| 93 |
+
os.remove(file_path)
|