-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.py
53 lines (40 loc) · 1.64 KB
/
run.py
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
45
46
47
48
49
50
51
import os
import json
from os.path import join, dirname
from dotenv import load_dotenv
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
from watson_developer_cloud import AlchemyLanguageV1 as AlchemyLanguage
from speech_sentiment_python.recorder import Recorder
def transcribe_audio(path_to_audio_file):
username = os.environ.get("BLUEMIX_USERNAME")
password = os.environ.get("BLUEMIX_PASSWORD")
speech_to_text = SpeechToText(username=username,
password=password)
with open(join(dirname(__file__), path_to_audio_file), 'rb') as audio_file:
return speech_to_text.recognize(audio_file,
content_type='audio/wav')
def get_text_sentiment(text):
alchemy_api_key = os.environ.get("ALCHEMY_API_KEY")
alchemy_language = AlchemyLanguage(api_key=alchemy_api_key)
result = alchemy_language.sentiment(text=text)
if result['docSentiment']['type'] == 'neutral':
return 'netural', 0
return result['docSentiment']['type'], result['docSentiment']['score']
def main():
recorder = Recorder("speech.wav")
print("Please say something nice into the microphone\n")
recorder.record_to_file()
print("Transcribing audio....\n")
result = transcribe_audio('speech.wav')
text = result['results'][0]['alternatives'][0]['transcript']
print("Text: " + text + "\n")
sentiment, score = get_text_sentiment(text)
print(sentiment, score)
if __name__ == '__main__':
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
try:
main()
except:
print("IOError detected, restarting...")
main()