mirror of
https://github.com/wassname/BitLit_test1.git
synced 2026-06-27 16:43:35 +08:00
rename file, and avoid import *
This commit is contained in:
+83
-102
@@ -1,32 +1,57 @@
|
|||||||
'''
|
"""
|
||||||
Voice to text to poem to speech
|
Voice to text to poem to speech
|
||||||
Credits: Michel, Lauren, Thomas
|
Credits: Michel, Lauren, Thomas
|
||||||
'''
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from gtts import gTTS ## Packages for Text to voice
|
from gtts import gTTS ## Packages for Text to voice
|
||||||
import os
|
import os
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import speech_recognition as sr ## Packages for voice recognizer
|
import speech_recognition as sr ## Packages for voice recognizer
|
||||||
|
|
||||||
os.environ['CUDA_VISIBLE_DEVICES']=""
|
# To try without cuda
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
|
|
||||||
tf.enable_eager_execution()
|
tf.enable_eager_execution()
|
||||||
from tensorflow.keras.layers import Embedding, GRU, Dense
|
from tensorflow.keras.layers import Embedding, GRU, Dense
|
||||||
import re
|
import re
|
||||||
from textblob import TextBlob
|
from textblob import TextBlob
|
||||||
import random
|
import random
|
||||||
from BitLit_param import *
|
from BitLit_model_param import (
|
||||||
|
parameters_rhymes,
|
||||||
|
parameters_poems,
|
||||||
|
char2idx_poems,
|
||||||
|
units_poems,
|
||||||
|
embedding_dim_poems,
|
||||||
|
gru_weights_poems,
|
||||||
|
fc_weights_poems,
|
||||||
|
embedding_weights_poems,
|
||||||
|
embedding_weights_rhymes,
|
||||||
|
word2idx_rhymes,
|
||||||
|
fc_weights_rhymes,
|
||||||
|
gru_weights_rhymes,
|
||||||
|
units_rhymes,
|
||||||
|
idx2word_rhymes,
|
||||||
|
idx2char_poems,
|
||||||
|
)
|
||||||
|
|
||||||
# Architechture of the GRU
|
# Architechture of the GRU
|
||||||
|
|
||||||
|
|
||||||
class Model(tf.keras.Model):
|
class Model(tf.keras.Model):
|
||||||
def __init__(self, vocab_size, embedding_dim, units, batch_size):
|
def __init__(self, vocab_size, embedding_dim, units, batch_size):
|
||||||
super(Model, self).__init__()
|
super(Model, self).__init__()
|
||||||
self.units = units
|
self.units = units
|
||||||
self.batch_sz = batch_size
|
self.batch_sz = batch_size
|
||||||
self.embedding = Embedding(vocab_size, embedding_dim)
|
self.embedding = Embedding(vocab_size, embedding_dim)
|
||||||
self.gru = GRU(self.units, return_sequences=True, return_state=True, recurrent_activation='sigmoid', recurrent_initializer='glorot_uniform')
|
self.gru = GRU(
|
||||||
|
self.units,
|
||||||
|
return_sequences=True,
|
||||||
|
return_state=True,
|
||||||
|
recurrent_activation="sigmoid",
|
||||||
|
recurrent_initializer="glorot_uniform",
|
||||||
|
)
|
||||||
self.fc = Dense(vocab_size)
|
self.fc = Dense(vocab_size)
|
||||||
|
|
||||||
def call(self, x, hidden):
|
def call(self, x, hidden):
|
||||||
@@ -36,14 +61,15 @@ class Model(tf.keras.Model):
|
|||||||
x = self.fc(output)
|
x = self.fc(output)
|
||||||
return x, states
|
return x, states
|
||||||
|
|
||||||
|
|
||||||
# Creation of the poem models and rhymes model
|
# Creation of the poem models and rhymes model
|
||||||
model_poems = Model(vocab_size_poems, embedding_dim_poems, units_poems, BATCH_SIZE_poems)
|
model_poems = Model(**parameters_poems)
|
||||||
model_rhymes = Model(vocab_size_rhymes, embedding_dim_rhymes, units_rhymes, BATCH_SIZE_rhymes)
|
model_rhymes = Model(**parameters_rhymes)
|
||||||
|
|
||||||
|
|
||||||
# Set the weights for the poems model
|
# Set the weights for the poems model
|
||||||
num_generate = 1
|
num_generate = 1
|
||||||
start_string = 'child'[::-1]
|
start_string = "child"[::-1]
|
||||||
input_eval = [char2idx_poems[s] for s in start_string]
|
input_eval = [char2idx_poems[s] for s in start_string]
|
||||||
input_eval = tf.expand_dims(input_eval, 0)
|
input_eval = tf.expand_dims(input_eval, 0)
|
||||||
hidden = [tf.zeros((1, units_poems))]
|
hidden = [tf.zeros((1, units_poems))]
|
||||||
@@ -56,8 +82,10 @@ model_poems.fc.set_weights(fc_weights_poems)
|
|||||||
|
|
||||||
# Set the weights for the rhymes model
|
# Set the weights for the rhymes model
|
||||||
num_generate = 1 # number of characters to generate
|
num_generate = 1 # number of characters to generate
|
||||||
start_string = ['fell'] # beginning of the generated text. TODO: try start_string = ' '
|
start_string = ["fell"] # beginning of the generated text. TODO: try start_string = ' '
|
||||||
input_eval = [word2idx_rhymes[s] for s in start_string] # converts start_string to numbers the model understands
|
input_eval = [
|
||||||
|
word2idx_rhymes[s] for s in start_string
|
||||||
|
] # converts start_string to numbers the model understands
|
||||||
input_eval = tf.expand_dims(input_eval, 0)
|
input_eval = tf.expand_dims(input_eval, 0)
|
||||||
hidden = [tf.zeros((1, units_rhymes))]
|
hidden = [tf.zeros((1, units_rhymes))]
|
||||||
predictions, hidden = model_rhymes(input_eval, hidden)
|
predictions, hidden = model_rhymes(input_eval, hidden)
|
||||||
@@ -66,135 +94,88 @@ model_rhymes.embedding.set_weights(np.asarray(embedding_weights_rhymes))
|
|||||||
model_rhymes.gru.set_weights(gru_weights_rhymes)
|
model_rhymes.gru.set_weights(gru_weights_rhymes)
|
||||||
model_rhymes.fc.set_weights(fc_weights_rhymes)
|
model_rhymes.fc.set_weights(fc_weights_rhymes)
|
||||||
|
|
||||||
def poem(USER_INPUT):
|
|
||||||
###https://pythonprogramminglanguage.com/text-to-speech/
|
|
||||||
#### cmd 1:::: sudo pip install gTTS
|
|
||||||
#### cmd 2:::: sudo pip install pyttsx
|
|
||||||
|
|
||||||
|
def poem(USER_INPUT):
|
||||||
#######################################################
|
|
||||||
##sys.path
|
|
||||||
##sys.path.append('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python')
|
|
||||||
##sys.path.append('/Users/ShebMichel/Library/Python/2.7/lib/python/site-packages'
|
|
||||||
################################################################################
|
|
||||||
############ AUDIO CONVERSION TO TEST
|
|
||||||
#r = sr.Recognizer()
|
|
||||||
#with sr.Microphone() as source:
|
|
||||||
## tts = gTTS(text='HELLO! My Name is BIT-LIT. PLEASE SPEAK IN ABOUT 3 SECONDS.', lang='en')
|
|
||||||
## tts.save("hello.mp3")
|
|
||||||
## os.system("start hello.mp3")
|
|
||||||
## ######
|
|
||||||
#
|
|
||||||
# print("SPEAK NOW-SPEAK NOW-SPEAK NOW:")
|
|
||||||
# audio = r.listen(source)
|
|
||||||
# tts = gTTS(text='THANK YOU! GIVE ME A SECOND TO READ OUT YOUR POEM', lang='en')
|
|
||||||
# tts.save("thanks.mp3")
|
|
||||||
# os.system("start thanks.mp3")
|
|
||||||
#try:
|
|
||||||
# # for testing purposes, we're just using the default API key
|
|
||||||
# # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
|
|
||||||
# # instead of `r.recognize_google(audio)
|
|
||||||
# AA0=r.recognize_google(audio)
|
|
||||||
# USER_INPUT=AA0
|
|
||||||
# print("You said: " + r.recognize_google(audio))
|
|
||||||
#except sr.UnknownValueError:
|
|
||||||
# print("Could not understand audio")
|
|
||||||
#except sr.RequestError as e:
|
|
||||||
# print("Could not request results; {0}".format(e))
|
|
||||||
#################################################################################
|
|
||||||
### ML POEM PREDICTOR
|
### ML POEM PREDICTOR
|
||||||
|
|
||||||
|
|
||||||
###########################
|
###########################
|
||||||
# USER INPUT a line #
|
# USER INPUT a line #
|
||||||
###########################
|
###########################
|
||||||
USER_INPUT = USER_INPUT.lower()
|
USER_INPUT = USER_INPUT.lower()
|
||||||
USER_INPUT = re.sub('[^a-z\n]', ' ', USER_INPUT)
|
USER_INPUT = re.sub("[^a-z\n]", " ", USER_INPUT)
|
||||||
text_generated = USER_INPUT[::-1]
|
text_generated = USER_INPUT[::-1]
|
||||||
first_rhyme = USER_INPUT.split(' ')[-1] # Michel's magic
|
first_rhyme = USER_INPUT.split(" ")[-1] # Michel's magic
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
######################
|
######################
|
||||||
# RHYMES GENERATION #
|
# RHYMES GENERATION #
|
||||||
######################
|
######################
|
||||||
temperature = 0.09
|
temperature = 0.09
|
||||||
|
|
||||||
num_generate = 5 # number of characters to generate
|
num_generate = 5 # number of characters to generate
|
||||||
if first_rhyme in idx2word_rhymes.values():
|
if first_rhyme in idx2word_rhymes.values():
|
||||||
start_string = [first_rhyme]
|
start_string = [first_rhyme]
|
||||||
else:
|
else:
|
||||||
start_string = [random.choice(list(word2idx_rhymes.keys()))]
|
start_string = [random.choice(list(word2idx_rhymes.keys()))]
|
||||||
print('The word {} is not in our corpus of rhymes yet.'.format(first_rhyme))
|
print("The word {} is not in our corpus of rhymes yet.".format(first_rhyme))
|
||||||
input_eval = [word2idx_rhymes[s] for s in start_string] # converts start_string to numbers the model understands
|
input_eval = [
|
||||||
input_eval = tf.expand_dims(input_eval, 0)
|
word2idx_rhymes[s] for s in start_string
|
||||||
|
] # converts start_string to numbers the model understands
|
||||||
|
input_eval = tf.expand_dims(input_eval, 0)
|
||||||
|
|
||||||
rhymes = []
|
rhymes = []
|
||||||
|
|
||||||
|
|
||||||
hidden = [tf.zeros((1, units_rhymes))]
|
hidden = [tf.zeros((1, units_rhymes))]
|
||||||
for i in range(num_generate):
|
for i in range(num_generate):
|
||||||
predictions, hidden = model_rhymes(input_eval, hidden) # predictions holds the probabily for each character to be most adequate continuation
|
predictions, hidden = model_rhymes(
|
||||||
|
input_eval, hidden
|
||||||
predictions = predictions / temperature # alters characters' probabilities to be picked (but keeps the order)
|
) # predictions holds the probabily for each character to be most adequate continuation
|
||||||
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy() # picks the next character for the generated text
|
|
||||||
|
predictions = (
|
||||||
|
predictions / temperature
|
||||||
|
) # alters characters' probabilities to be picked (but keeps the order)
|
||||||
|
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][
|
||||||
|
0
|
||||||
|
].numpy() # picks the next character for the generated text
|
||||||
input_eval = tf.expand_dims([predicted_id], 0)
|
input_eval = tf.expand_dims([predicted_id], 0)
|
||||||
rhymes += [idx2word_rhymes[predicted_id]]
|
rhymes += [idx2word_rhymes[predicted_id]]
|
||||||
|
|
||||||
print('rhymes:', rhymes)
|
print("rhymes:", rhymes)
|
||||||
|
|
||||||
|
|
||||||
####################
|
####################
|
||||||
# POEM GENERATION #
|
# POEM GENERATION #
|
||||||
####################
|
####################
|
||||||
|
|
||||||
|
|
||||||
temperature = 0.8
|
temperature = 0.8
|
||||||
text_generated = USER_INPUT
|
text_generated = USER_INPUT
|
||||||
text_generated = text_generated[::-1] + '\n'
|
text_generated = text_generated[::-1] + "\n"
|
||||||
num_generate = 150
|
num_generate = 150
|
||||||
for rhyme in rhymes:
|
for rhyme in rhymes:
|
||||||
start_string = text_generated + rhyme[::-1]
|
start_string = text_generated + rhyme[::-1]
|
||||||
input_eval = [char2idx_poems[s] for s in start_string]
|
input_eval = [char2idx_poems[s] for s in start_string]
|
||||||
input_eval = tf.expand_dims(input_eval, 0)
|
input_eval = tf.expand_dims(input_eval, 0)
|
||||||
hidden = [tf.zeros((1, units_poems))]
|
hidden = [tf.zeros((1, units_poems))]
|
||||||
|
|
||||||
b = True
|
b = True
|
||||||
c = 1
|
c = 1
|
||||||
added_text = ' '
|
added_text = " "
|
||||||
while b == True:
|
while b == True:
|
||||||
|
|
||||||
predictions, hidden = model_poems(input_eval, hidden)
|
predictions, hidden = model_poems(input_eval, hidden)
|
||||||
predictions = predictions / temperature
|
predictions = predictions / temperature
|
||||||
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][0].numpy()
|
predicted_id = tf.multinomial(tf.exp(predictions), num_samples=1)[0][
|
||||||
|
0
|
||||||
|
].numpy()
|
||||||
input_eval = tf.expand_dims([predicted_id], 0)
|
input_eval = tf.expand_dims([predicted_id], 0)
|
||||||
added_text += idx2char_poems[predicted_id]
|
added_text += idx2char_poems[predicted_id]
|
||||||
c += 1
|
c += 1
|
||||||
if idx2char_poems[predicted_id] == '\n' or c > num_generate:
|
if idx2char_poems[predicted_id] == "\n" or c > num_generate:
|
||||||
text_generated = rhyme[::-1] + added_text + text_generated
|
text_generated = rhyme[::-1] + added_text + text_generated
|
||||||
b = False
|
b = False
|
||||||
|
|
||||||
text_generated = text_generated[::-1] # That's the poem to return to the user in voice format
|
text_generated = text_generated[
|
||||||
|
::-1
|
||||||
text_generated = re.sub(' +',' ',text_generated)
|
] # That's the poem to return to the user in voice format
|
||||||
|
|
||||||
|
text_generated = re.sub(" +", " ", text_generated)
|
||||||
text_generated = str(TextBlob(text_generated).correct())
|
text_generated = str(TextBlob(text_generated).correct())
|
||||||
return text_generated
|
return text_generated
|
||||||
|
|
||||||
#### END CODE
|
|
||||||
#########################################################
|
|
||||||
################# TEXT CONVERSION IN AUDIO
|
|
||||||
################# FEED POEM TO TRANSCRIBER
|
|
||||||
# print('ML POEM is:', text_generated)
|
|
||||||
# tts = gTTS(text=text_generated, lang='en')
|
|
||||||
# tts.save("poem.mp3")
|
|
||||||
# os.system("start poem.mp3")
|
|
||||||
# #########################################################
|
|
||||||
# ####
|
|
||||||
# print("BIT-LIT ENDING STATEMENT:")
|
|
||||||
# tts = gTTS(text='THANK YOU! CHECK ME OUT IN THE NEWS SOON.', lang='en')
|
|
||||||
# tts.save("goodbye.mp3")
|
|
||||||
# #os.system("start goodbye.mp3")
|
|
||||||
# ### USING JUPITER
|
|
||||||
# # import IPython.display as ipd
|
|
||||||
# # ipd.Audio(filename='path/to/file.mp3')
|
|
||||||
# #tk.mainloop()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user