Spaces:
Runtime error
Runtime error
File size: 1,017 Bytes
1ab62ff 96e1642 1ab62ff 96e1642 1ab62ff 96e1642 1ab62ff 96e1642 1ab62ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import speech_recognition as sr
import matplotlib.pyplot as plt
import numpy as np
import sounddevice as sd
from textblob import TextBlob
def record_audio():
duration = 5 # in seconds
sample_rate = 44100 # in Hz
channels = 1
recording = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=channels)
sd.wait()
return np.squeeze(recording)
def transcribe_audio(audio):
r = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_data = r.record(source)
return r.recognize_google(audio_data)
def get_sentiment(text):
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
return sentiment
def plot_sentiment(sentiment):
plt.plot(sentiment)
plt.xlabel("Time (s)")
plt.ylabel("Sentiment")
plt.show()
def main():
audio = record_audio()
text = transcribe_audio(audio)
sentiment = [get_sentiment(sentence) for sentence in text.split('.')]
plot_sentiment(sentiment)
if __name__ == "__main__":
main()
|