/
voicecontrol.py
204 lines (160 loc) · 6.13 KB
/
voicecontrol.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
import os
import sys
import time
import openai
import tempfile
import requests
import urllib.request
import speech_recognition as sr
from pydub import AudioSegment
from octorest import OctoRest
import nltk
from io import BytesIO
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from urllib3.exceptions import InsecureRequestWarning
from bs4 import BeautifulSoup
from thingiverse import Thingiverse
if sys.version_info >= (3, 6):
import zipfile
else:
import zipfile36 as zipfile
import pygcode
import io
from os import path
import asyncio
from stl import mesh
import py3d
from pydantic import BaseModel
import pydantic as pyd
from pygcode import Line
import time
import spacy
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
octoprint_address = '{octoprint url)'
api_key = '{octoprint api key}'
thingverse_api = '{thingiverse api key}'
openai.api_key = '{openai api key}'
access_token ="{thingiverse access token}"
BUFFER_DURATION = 5
TRIGGER_PHRASE = 'hey jarvis'
REQUEST_DELAY = 1
trigger_word = "start"
nlp = spacy.load("en_core_web_sm")
pp = "Fabricator"
def make_client(url, apikey):
try:
client = OctoRest(url=url, apikey=apikey)
return client
except ConnectionError as ex:
print(ex)
def wait_for_trigger_word(trigger):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
while True:
audio_data = recognizer.record(source, duration=BUFFER_DURATION)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_audio_file:
tmp_audio_file.write(audio_data.get_wav_data())
transcription = transcribe_whisper(tmp_audio_file.name)
print(f"Transcription: {transcription}")
os.remove(tmp_audio_file.name)
if trigger.lower() in transcription.lower():
print(f"Trigger phrase '{TRIGGER_PHRASE}' detected!")
break
def add_suffix(strings):
suffixed = [s + '.gco' for s in strings]
return suffixed
def subtract_suffix(strings):
subtracted = [s[:-4] if s.endswith('.stl') else s for s in strings]
return subtracted
def sort_files_by_size(file_paths):
file_sizes = [(path, os.path.getsize(path)) for path in file_paths]
sorted_sizes = sorted(file_sizes, key=lambda x: x[1])
sorted_paths = [path for path, _ in sorted_sizes]
return sorted_paths
def download_stl(search_term, octoprint_address, api_key, access_token):
cwd = os.getcwd()
term = search_term
trigger = "start"
octo = make_client(url=octoprint_address, apikey=api_key)
thingy = Thingiverse(access_token=access_token)
search_results = thingy.search_term(term)
print(search_results)
id_num = search_results["hits"][0]['id']
print(id_num)
thing_url = "https://www.thingiverse.com/thing:" + str(id_num) + "/zip"
response = requests.get(thing_url)
zipf = zipfile.ZipFile(io.BytesIO(response.content))
os.chdir('models')
if not os.path.exists(str(term)):
os.mkdir(str(term))
os.chdir(str(term))
else:
os.chdir(str(term))
path_str = 'C:/Users/Manav/Desktop/VOICEPRINT/models/' + str(term)
zipf.extractall(path_str)
os.chdir("files")
path_str += "/files/"
gcode_files = []
stl_files = [f for f in os.listdir(path_str) if f.endswith(".stl")]
sorted_files = sort_files_by_size(stl_files)
print(sorted_files)
file_names = subtract_suffix(sorted_files)
print(file_names)
files = add_suffix(file_names)
gcode_paths = []
for i in range(0, len(stl_files), 1):
upload = octo.upload(stl_files[i])
file_url = upload['files']['local']['name']
filename = upload['files']['local']['refs']['resource']
octo.slice(file_url, print=True, printer_profile=pp)
print(octo.state())
time.sleep(30)
if octo.state()== "Operational":
print("Say the command to start printing next slice")
wait_for_trigger_word(trigger)
print("command next recieved")
if octo.state() != "Printing":
octo.home()
octo.select(files[i])
resp = octo.start()
while octo.state() == "Printing":
print("waiting...")
time.sleep(200)
os.chdir('..')
os.chdir('..')
os.chdir('..')
return(stl_files)
def get_noun_phrase_complement(sentence):
doc = nlp(sentence)
noun_phrase_complements = []
for token in doc:
if token.head.pos_ == "VERB" and token.dep_ == "dobj":
noun_phrase = " ".join([child.text for child in token.subtree])
noun_phrase_complements.append(noun_phrase)
return noun_phrase_complements
def transcribe_whisper(audio_file_path):
with open(audio_file_path, "rb") as audio_file:
transcript = openai.Audio.transcribe("whisper-1", audio_file)
print(transcript)
return transcript["text"]
def listen_and_transcribe():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
while True:
audio_data = recognizer.record(source, duration=BUFFER_DURATION)
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio_file:
temp_audio_file.write(audio_data.get_wav_data())
transcription = transcribe_whisper(temp_audio_file.name)
print(f"Transcription: {transcription}")
os.remove(temp_audio_file.name)
if TRIGGER_PHRASE.lower() in transcription.lower():
print(f"Trigger phrase '{TRIGGER_PHRASE}' detected!")
sentence = transcription.lower()
nouns = get_noun_phrase_complement(sentence)
print(nouns)
files = download_stl(nouns, octoprint_address, api_key, access_token)
if __name__ == "__main__":
listen_and_transcribe()