/
reorganized.py
248 lines (214 loc) · 7.86 KB
/
reorganized.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
##introduction is only needed in some scenarios
from __future__ import division
import re
import sys
import time
import io
import os
import random
import six
from classify_text_tutorial import classify
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
import pyaudio
import wave
from six.moves import queue
import pygame
import time
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
def twenty(text):
i = 0
str = ""
if len(text) < 20:
while i < 20:
str += text
str += " "
i += 1
return str
def record_file(time):
language_code = 'en-US' # a BCP-47 language tag
words = []
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = time
WAVE_OUTPUT_FILENAME = "voice.wav"
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
return WAVE_OUTPUT_FILENAME
def transcribe_file(speech_file):
"""Transcribe the given audio file."""
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()
with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
# print("read content file")
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
language_code='en-US')
# print("config done")
response = client.recognize(config, audio)
# print("response found")
# Each result is for a consecutive portion of the audio. Iterate through
# them to get the transcripts for the entire audio file.
for result in response.results:
# The first alternative is the most likely one for this portion.
# print(u'Transcript: {}'.format(result.alternatives[0].transcript))
return result.alternatives[0].transcript
def loadmp3player(file):
# print(file)
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
time.sleep(0.5)
def speak(entry, character, counter):
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
speaker = client.synthesize_speech(texttospeech.types.SynthesisInput(text=entry), character, audio_config)
with open(str(counter)+ ".mp3", 'wb') as out:
out.write(speaker.audio_content)
out.close()
file = str(counter)+ ".mp3"
loadmp3player(file)
def respond():
print("Recording...")
answer = transcribe_file(record_file(5))
return answer
#issue: we have to figure out how to get all possible categories without hardcoding at some point
class FriendConvo():
def __init__(self, exposition, intro, questions, counter, coachprompt, required, accepted):
self.exposition = exposition
self.intro = intro
self.questions = questions
self.counter = counter
self.required = required
self.accepted = accepted
self.coachprompt = coachprompt
self.girl = texttospeech.types.VoiceSelectionParams(
language_code='en-US', name = 'en-US-Wavenet-F',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
self.coach = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE)
def introduction(self):
speak(self.intro, self.girl, self.counter)
time.sleep(3.5)
self.counter += 1
response = respond()
speak(response + ". Awesome", self.girl, self.counter)
self.counter += 1
def background(self):
for item in self.exposition:
speak(item, self.girl, self.counter)
self.counter += 1
time.sleep(1.5)
def q(self):
for item in self.questions:
speak(item, self.girl, self.counter)
time.sleep(2)
self.counter += 1
response = respond()
while(self.appropriate(response) == False):
speak("That's not a" + self.required + " . Try again", self.coach, self.counter)
self.counter += 1
time.sleep(1.5)
speak(item, self.girl, self.counter)
time.sleep(2)
self.counter += 1
response = respond()
while(self.complete(response) == False):
speak("That's good. Let's give a better answer. Say" + self.coachprompt + response, self.coach, self.counter)
self.counter += 1
time.sleep(4)
response = respond()
if(self.complete(response) == True):
speak("Awesome job. Now answer the question", self.coach, self.counter)
self.counter += 1
time.sleep(1.5)
speak(item, self.girl, self.counter)
time.sleep(1.5)
self.counter += 1
response = respond()
speak("Cool!", self.girl, self.counter)
self.counter += 1
# speak("That sounds great", self.girl, self.counter)
# self.counter += 1
time.sleep(1)
speak("Well, it was good catching up. See you later!", self.girl, self.counter)
time.sleep(3.5)
print("Finished")
def appropriate(self, response):
classification = classify(twenty(response))
a = ""
for (category, confidence) in classification.items():
try:
end = category[1:].index("/")
broad_category = category[1:end+1]
print(broad_category)
except:
broad_category = category[1:]
print(broad_category)
#
if broad_category in self.accepted:
return True
# print(broad_category)
return False
def complete(self, response):
for item in self.coachprompt.split(" "):
if item not in response:
return False
return True
lastWeekend = FriendConvo(["I had a fun weekend", "I went swimming in our community pool"], "Hey what's up! My name is Sara. What's your name?", ["What did you do over the weekend"], 0, "I went", "action", ["Arts & Entertainment",
"Books & Literature",
"Computers & Electronics",
"Games",
"Hobbies & Leisure",
"Home & Garden",
"Jobs & Education",
"Online Communities",
"People & Society",
"Pets & Animals",
"Shopping",
"Sports",
"Travel"])
lastWeekend.introduction()
time.sleep(1.5)
lastWeekend.background()
time.sleep(1.5)
lastWeekend.q()
#
# moviecategories = ["Arts & Entertainment"]
# movie = FriendConvo(["I just watched the Avengers in 3D!", "It was so awesome!"], "Hey what's up! My name is Sara. What's your name?", ["What's your favorite movie?"], 0, "I like", "movie", moviecategories)
# movie.appropriate("The Polar Express")
# movie.introduction()
# time.sleep(1.5)
# movie.background()
# time.sleep(1.5)
# movie.q()