Spaces:
Runtime error
Runtime error
| 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() | |