YashsharmaPhD commited on
Commit
828e49c
·
verified ·
1 Parent(s): d791332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -94
app.py CHANGED
@@ -4,50 +4,35 @@ 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
  import librosa.display
10
  import whisper
11
- import textwrap
12
  from collections import Counter
13
  from wordcloud import WordCloud
 
14
 
15
- # Load pre-trained sentiment analysis model
16
- sentiment_analyzer = pipeline("sentiment-analysis")
 
 
 
 
 
 
 
 
 
17
 
18
  # Load Whisper model
19
  whisper_model = whisper.load_model("base")
20
 
21
- # Positive & Negative Word Lists
22
- positive_words = ["good", "excellent", "happy", "positive", "great", "success", "love", "joy", "fantastic"]
23
- negative_words = ["bad", "poor", "angry", "negative", "sad", "failure", "hate", "terrible", "awful"]
24
-
25
  # Streamlit UI
26
  st.title("🎤 Audio Sentiment & Feature Analysis")
27
  st.write("Upload an MP3 file to analyze its sentiment and audio features.")
28
 
29
- # Upload audio file
30
  uploaded_file = st.file_uploader("Choose an MP3 file", type=["mp3"])
31
 
32
- # Function to split transcriptions into chunks (≤512 tokens)
33
- def split_text_into_chunks(text, max_length=512):
34
- """Splits text into smaller chunks for sentiment analysis."""
35
- words = text.split()
36
- chunks = []
37
- while words:
38
- chunk = words[:max_length]
39
- chunks.append(" ".join(chunk))
40
- words = words[max_length:]
41
- return chunks
42
-
43
- # Function to extract words and categorize them
44
- def extract_words_from_text(text):
45
- """Extracts words and categorizes them as positive or negative."""
46
- words = text.lower().split()
47
- good_words = [word for word in words if word in positive_words]
48
- bad_words = [word for word in words if word in negative_words]
49
- return good_words, bad_words
50
-
51
  if uploaded_file:
52
  file_path = f"temp/{uploaded_file.name}"
53
  os.makedirs("temp", exist_ok=True)
@@ -63,93 +48,39 @@ if uploaded_file:
63
  # Load audio
64
  y, sr = librosa.load(wav_path, sr=None)
65
 
66
- # Extract MFCCs (Mel-frequency cepstral coefficients)
67
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
68
- mfccs_mean = np.mean(mfccs, axis=1)
69
-
70
  # Transcribe with Whisper
71
  result = whisper_model.transcribe(wav_path)
72
  transcribed_text = result["text"]
73
 
74
- # Split transcription into smaller chunks for sentiment analysis
75
- text_chunks = split_text_into_chunks(transcribed_text)
76
-
77
- # Analyze sentiment for each chunk and determine overall sentiment
78
- sentiment_labels = []
79
- for chunk in text_chunks:
80
- sentiment_result = sentiment_analyzer(chunk)
81
- sentiment_labels.append(sentiment_result[0]["label"])
82
 
83
- # Majority voting for overall sentiment
84
- sentiment_counts = Counter(sentiment_labels)
85
- overall_sentiment = max(sentiment_counts, key=sentiment_counts.get)
86
- sentiment_color = "green" if overall_sentiment == "POSITIVE" else "red"
87
-
88
- # Extract words and categorize them
89
- good_words, bad_words = extract_words_from_text(transcribed_text)
90
-
91
- # Display sentiment result
92
  st.subheader("📊 Sentiment Analysis Result")
93
- st.markdown(f"**Overall Sentiment:** <span style='color:{sentiment_color}; font-size:20px;'>{overall_sentiment}</span>", unsafe_allow_html=True)
94
-
95
- # Display Positive & Negative Words in a Table
96
- st.subheader("🗣️ Positive & Negative Words in Transcription")
97
- col1, col2 = st.columns(2)
98
 
99
- with col1:
100
- st.markdown("### ✅ Good Words")
101
- st.write(", ".join(good_words) if good_words else "No good words detected.")
102
-
103
- with col2:
104
- st.markdown("### ❌ Bad Words")
105
- st.write(", ".join(bad_words) if bad_words else "No bad words detected.")
106
-
107
  # Display full transcription
108
  st.subheader("📝 Full Transcription")
109
  st.write(transcribed_text)
110
-
111
- # 1️⃣ Sentiment Trend Over Time
112
- sentiment_numeric = [1 if s == "POSITIVE" else -1 for s in sentiment_labels]
113
- fig, ax = plt.subplots(figsize=(8, 4))
114
- ax.scatter(range(len(sentiment_numeric)), sentiment_numeric, c=sentiment_numeric, cmap="coolwarm")
115
- ax.set_title("Sentiment Trend (Per Chunk)")
116
- ax.set_xticks(range(0, len(sentiment_labels), max(1, len(sentiment_labels)//5)))
117
- ax.set_yticks([-1, 1], labels=["Negative", "Positive"])
118
- st.pyplot(fig)
119
-
120
- # 2️⃣ MFCC Heatmap
121
  fig, ax = plt.subplots(figsize=(10, 4))
122
  sns.heatmap(mfccs, cmap="coolwarm", xticklabels=False, yticklabels=False)
123
  ax.set_title("MFCC Heatmap")
124
  st.pyplot(fig)
125
-
126
- # 3️⃣ Waveform Plot
127
- fig, ax = plt.subplots(figsize=(10, 4))
128
- librosa.display.waveshow(y, sr=sr, alpha=0.5)
129
- ax.set_title("Waveform of Audio")
130
- st.pyplot(fig)
131
-
132
- # 4️⃣ Spectrogram
133
- fig, ax = plt.subplots(figsize=(10, 4))
134
- D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
135
- librosa.display.specshow(D, sr=sr, x_axis="time", y_axis="log", cmap="coolwarm")
136
- ax.set_title("Spectrogram")
137
- st.pyplot(fig)
138
-
139
- # 5️⃣ Positive vs Negative Word Count Bar Chart
140
- fig, ax = plt.subplots(figsize=(6, 4))
141
- ax.bar(["Positive Words", "Negative Words"], [len(good_words), len(bad_words)], color=["green", "red"])
142
- ax.set_title("Positive vs Negative Word Count")
143
- st.pyplot(fig)
144
-
145
- # 6️⃣ Word Cloud of Transcription
146
  wordcloud = WordCloud(width=800, height=400, background_color="white").generate(transcribed_text)
147
  fig, ax = plt.subplots(figsize=(10, 5))
148
  ax.imshow(wordcloud, interpolation="bilinear")
149
  ax.axis("off")
150
  ax.set_title("Word Cloud of Transcription")
151
  st.pyplot(fig)
152
-
153
  # Clean up temp files
154
  os.remove(wav_path)
155
  os.remove(file_path)
 
4
  import matplotlib.pyplot as plt
5
  import seaborn as sns
6
  from pydub import AudioSegment
7
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
8
  import os
9
  import librosa.display
10
  import whisper
 
11
  from collections import Counter
12
  from wordcloud import WordCloud
13
+ import torch
14
 
15
+ # Load T5 model and tokenizer
16
+ tokenizer = T5Tokenizer.from_pretrained("t5-small")
17
+ model = T5ForConditionalGeneration.from_pretrained("t5-small")
18
+
19
+ def analyze_sentiment_t5(text):
20
+ """Analyzes sentiment using the T5 model."""
21
+ input_text = f"sst2 sentence: {text}" # Formatting input for T5 model
22
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
23
+ output = model.generate(input_ids)
24
+ sentiment = tokenizer.decode(output[0], skip_special_tokens=True)
25
+ return "POSITIVE" if "positive" in sentiment.lower() else "NEGATIVE"
26
 
27
  # Load Whisper model
28
  whisper_model = whisper.load_model("base")
29
 
 
 
 
 
30
  # Streamlit UI
31
  st.title("🎤 Audio Sentiment & Feature Analysis")
32
  st.write("Upload an MP3 file to analyze its sentiment and audio features.")
33
 
 
34
  uploaded_file = st.file_uploader("Choose an MP3 file", type=["mp3"])
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  if uploaded_file:
37
  file_path = f"temp/{uploaded_file.name}"
38
  os.makedirs("temp", exist_ok=True)
 
48
  # Load audio
49
  y, sr = librosa.load(wav_path, sr=None)
50
 
51
+ # Extract MFCCs
52
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
53
+
 
54
  # Transcribe with Whisper
55
  result = whisper_model.transcribe(wav_path)
56
  transcribed_text = result["text"]
57
 
58
+ # Analyze sentiment
59
+ sentiment = analyze_sentiment_t5(transcribed_text)
60
+ sentiment_color = "green" if sentiment == "POSITIVE" else "red"
 
 
 
 
 
61
 
62
+ # Display results
 
 
 
 
 
 
 
 
63
  st.subheader("📊 Sentiment Analysis Result")
64
+ st.markdown(f"**Overall Sentiment:** <span style='color:{sentiment_color}; font-size:20px;'>{sentiment}</span>", unsafe_allow_html=True)
 
 
 
 
65
 
 
 
 
 
 
 
 
 
66
  # Display full transcription
67
  st.subheader("📝 Full Transcription")
68
  st.write(transcribed_text)
69
+
70
+ # 1️⃣ MFCC Heatmap
 
 
 
 
 
 
 
 
 
71
  fig, ax = plt.subplots(figsize=(10, 4))
72
  sns.heatmap(mfccs, cmap="coolwarm", xticklabels=False, yticklabels=False)
73
  ax.set_title("MFCC Heatmap")
74
  st.pyplot(fig)
75
+
76
+ # 2️⃣ Word Cloud
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  wordcloud = WordCloud(width=800, height=400, background_color="white").generate(transcribed_text)
78
  fig, ax = plt.subplots(figsize=(10, 5))
79
  ax.imshow(wordcloud, interpolation="bilinear")
80
  ax.axis("off")
81
  ax.set_title("Word Cloud of Transcription")
82
  st.pyplot(fig)
83
+
84
  # Clean up temp files
85
  os.remove(wav_path)
86
  os.remove(file_path)