-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (109 loc) · 4.18 KB
/
main.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
import os
from selenium import webdriver
import time
import json
import requests
import pygame
from gtts import gTTS
pygame.mixer.init()
headless = False
# AWS LOGINS
with open("settings.json", "r") as jsonO:
data = json.loads(jsonO.read())
aws_login = data["aws_email"]
aws_pass = data["aws_password"]
# Setup ML
import gpt_2_simple as gpt2
# Download model if not exists
model_name = "355M"
if not os.path.isdir(os.path.join("models", model_name)):
print(f"Downloading {model_name} model...")
gpt2.download_gpt2(model_name=model_name)
# Downloads some data to fine tune on
file_name = "shakespeare.txt"
if not os.path.isfile(file_name):
url = "https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt"
data = requests.get(url)
with open(file_name, 'w') as f:
f.write(data.text)
# Trains on 1 step just to get a checkpoint to use
sess = gpt2.start_tf_sess()
gpt2.finetune(sess,
file_name,
model_name=model_name,
steps=1)
# Actually train
gpt2.generate(sess)
# Start model instance
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess)
print("Loaded Model")
# Function to generate text
def generateML(primer):
return gpt2.generate(sess, length=75, prefix=primer, return_as_list=True, include_prefix=False)[0].lower().replace(primer.lower(), "")
# Chrome profile to attempt avoid detection
chromeProfile = webdriver.ChromeOptions()
chromeProfile.add_argument("--disable-automation")
chromeProfile.add_argument("--no-sandbox")
chromeProfile.add_argument('--disable-extensions')
chromeProfile.add_argument('--profile-directory=Default')
chromeProfile.add_argument("--incognito")
chromeProfile.add_argument("--disable-plugins-discovery")
chromeProfile.add_argument("--start-maximized")
chromeProfile.add_experimental_option('useAutomationExtension', False)
chromeProfile.add_experimental_option(
"excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(chrome_options=chromeProfile)
# Get website
driver.get("https://console.aws.amazon.com/transcribe/home?region=us-east-1#realTimeTranscription")
# Login
driver.find_element_by_xpath("//input[@placeholder='username@example.com']").send_keys(aws_login)
driver.find_element_by_xpath("//button[@id='next_button']").click()
time.sleep(1)
try:
driver.find_element_by_xpath("//input[@id='password']").send_keys(aws_pass)
driver.find_element_by_xpath("//button[@id='signin_button']").click()
time.sleep(5)
# Click the start streaming button
driver.find_element_by_id("//button[@class='awsui-button awsui-button-variant-primary awsui-hover-child-icons']").click()
# Give time for the user to actually click the allow button to access the microphone
time.sleep(10)
except:
print("Error while logging in please log in and start the streaming button.")
print("Hit enter to continue")
input()
# Access the content of the recognition
previousText = ""
var = False
while True:
try:
recentSection = driver.find_elements_by_xpath("//div[@id='streamingContent']/div")[len(driver.find_elements_by_xpath("//div[@id='streamingContent']/div"))-2].text.strip()
except:
recentSection = driver.find_elements_by_xpath("//div[@id='streamingContent']/div")[len(driver.find_elements_by_xpath("//div[@id='streamingContent']/div"))-1].text.strip()
#if previousText == "":
# newText = recentSection
#else:
# newText = entireScript.replace(previousText, "")
# previousText = entireScript
newText = recentSection
print(newText)
if newText != "":
predictionText = generateML(newText).replace("<|endoftext|>", "")
print("PREDICTION: " + predictionText)
# generate the TTS
myobj = gTTS(text=predictionText, lang='en', slow=False)
# Save voice as either tmp 1 or tmp 2 if other in use
try:
myobj.save("tmp.mp3")
pygame.mixer.music.load("tmp.mp3")
pygame.mixer.music.play()
time.sleep(3)
var = True
except:
myobj.save("tmp2.mp3")
pygame.mixer.music.load("tmp2.mp3")
pygame.mixer.music.play()
time.sleep(3)
var = True
if not var:
time.sleep(2)