sanjayw commited on
Commit
96e1642
·
1 Parent(s): 9bf4a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -1,37 +1,43 @@
1
- import streamlit as st
2
- import pyaudio
3
  import speech_recognition as sr
4
- from textblob import TextBlob
5
  import matplotlib.pyplot as plt
6
-
7
- def sentiment_analysis(text):
8
- blob = TextBlob(text)
9
- sentiment_score = blob.sentiment.polarity
10
- return sentiment_score
11
 
12
  def record_audio():
 
 
 
 
 
 
 
 
 
 
13
  r = sr.Recognizer()
14
- with sr.Microphone() as source:
15
- st.write("Say something!")
16
- audio = r.listen(source)
17
- try:
18
- text = r.recognize_google(audio)
19
- sentiment_score = sentiment_analysis(text)
20
- st.write("Transcription: ", text)
21
- st.write("Sentiment Score: ", sentiment_score)
22
- fig, ax = plt.subplots()
23
- ax.bar(["Sentiment"], [sentiment_score])
24
- st.pyplot(fig)
25
- except sr.UnknownValueError:
26
- st.write("Could not understand audio")
27
- except sr.RequestError as e:
28
- st.write("Could not request results from Google Speech Recognition service; {0}".format(e))
 
29
 
30
  def main():
31
- st.title("Speech to Sentiment Analysis")
32
- st.write("Press 'Record' to start recording audio.")
33
- if st.button("Record"):
34
- record_audio()
35
 
36
  if __name__ == "__main__":
37
  main()
 
 
 
1
  import speech_recognition as sr
 
2
  import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ import sounddevice as sd
5
+ from textblob import TextBlob
 
 
6
 
7
  def record_audio():
8
+ duration = 5 # in seconds
9
+ sample_rate = 44100 # in Hz
10
+ channels = 1
11
+
12
+ recording = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=channels)
13
+ sd.wait()
14
+
15
+ return np.squeeze(recording)
16
+
17
+ def transcribe_audio(audio):
18
  r = sr.Recognizer()
19
+ with sr.AudioFile(audio) as source:
20
+ audio_data = r.record(source)
21
+
22
+ return r.recognize_google(audio_data)
23
+
24
+ def get_sentiment(text):
25
+ blob = TextBlob(text)
26
+ sentiment = blob.sentiment.polarity
27
+
28
+ return sentiment
29
+
30
+ def plot_sentiment(sentiment):
31
+ plt.plot(sentiment)
32
+ plt.xlabel("Time (s)")
33
+ plt.ylabel("Sentiment")
34
+ plt.show()
35
 
36
  def main():
37
+ audio = record_audio()
38
+ text = transcribe_audio(audio)
39
+ sentiment = [get_sentiment(sentence) for sentence in text.split('.')]
40
+ plot_sentiment(sentiment)
41
 
42
  if __name__ == "__main__":
43
  main()