Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 8 |
-
|
| 9 |
-
sentiment_score = blob.sentiment.polarity
|
| 10 |
-
return sentiment_score
|
| 11 |
|
| 12 |
def record_audio():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
r = sr.Recognizer()
|
| 14 |
-
with sr.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
def main():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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()
|