sanjayw commited on
Commit
1ab62ff
·
1 Parent(s): 7a743a3

app.py created

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()