5 from tkinter.filedialog
import askdirectory
6 from tkinter
import ttk
7 import tkinter.filedialog
as filedialog
17 from bs4
import BeautifulSoup
18 from threading
import Thread
22 rospack = rospkg.RosPack()
25 model_path =
'/models/'
29 if not os.path.isdir(model_folder):
30 os.mkdir(model_folder)
48 style.map(
'TCombobox', fieldbackground=[(
'readonly',
'white')])
49 style.map(
'TCombobox', selectbackground=[(
'readonly',
'white')])
50 style.map(
'TCombobox', selectforeground=[(
'readonly',
'black')])
53 self.
gui_master.grid_columnconfigure(0, weight=1)
68 self.
lbl_welcome.grid(row=0, columnspan=2, sticky=
"news", pady=10)
74 self.
lbl_model.grid(row=1, column=1, sticky=
"w")
84 self.
combo_models.grid(row=2, column=1, sticky=
"news", pady=2)
89 self.
lbl_info.grid(row=3, columnspan=2, sticky=
"w", pady=10)
92 model_info =
"Name: %s\nSize: %s\nWord error rate/Speed: %s\nNotes: %s\nLicense: %s\n" % (self.
model_to_download, size, error, notes, license)
100 self.
lbl_download.grid(row=0, column=0, sticky=
"w", pady=10)
106 self.
btn_browse.grid(row=1, column=1, sticky=
"news", pady=10)
109 self.
btn_download.grid(row=2, column=1, sticky=
"news",pady=10)
112 self.
progressbar.grid(row=2, column=0, sticky=
"news", pady=10)
115 value = self.listbox_languages.get(tk.ANCHOR)
118 value = self.listbox_model.get(tk.ANCHOR)
121 path = filedialog.askdirectory()
129 if model[
"Language"] == language:
130 models.append(model[
'Model'])
134 http = urllib3.PoolManager()
136 self.
url =
"https://alphacephei.com/vosk/models"
138 self.
r = http.request(
'GET', self.
url)
139 soup = BeautifulSoup(self.
r.data,
'html.parser')
147 for table
in soup.find_all(
'table'):
148 for head
in table.find_all(
'thead'):
149 for column
in head.find_all(
'th'):
150 head_list.append(column.text)
151 for table_body
in table.find_all(
'tbody'):
152 for row
in table_body.find_all(
'tr'):
154 for column
in row.find_all(
'td'):
155 row_list.append(column.text)
156 item_list.append(row_list)
159 for item
in item_list:
161 if item[0] !=
"\xa0" and item[1] ==
"\xa0" and (item[2] ==
"\xa0" or item[2] ==
"Older Models")
and item[3] ==
"\xa0":
163 if language_list.count(language) == 0:
164 language_list.append(language)
166 model_dict = {
"Language": language, head_list[0]: item[0], head_list[1]: item[1], head_list[2]: item[2], head_list[3]: item[3], head_list[4]: item[4]}
167 model_list.append(model_dict)
168 return language_list, model_list
176 if model[
"Language"] == language:
177 if model[
"Model"] == selected_model:
179 error = model[
"Word error rate/Speed"]
180 notes = model[
"Notes"]
181 license = model[
"License"]
183 return size, error, notes, license
186 downloadThread= Thread(target=
lambda:self.
download(model_to_download))
187 downloadThread.start()
195 req=requests.get(model_url,stream=
True)
197 if "Content-Length" in req.headers:
198 total_size=req.headers[
'Content-Length']
201 with open(self.
model_dir+filename,
"wb")
as fileobj:
202 for chunk
in req.iter_content(chunk_size=1024):
205 current_size=os.path.getsize(self.
model_dir+filename)
209 percentage = round((int(current_size)/int(total_size))*100)
213 percentage =
"Infinite"
219 current_size=os.path.getsize(self.
model_dir+filename)
222 percentage=round((int(current_size)/int(total_size))*100)
233 current_size=os.path.getsize(self.
model_dir+filename)
249 info =
"Name: %s\nSize: %s\nWord error rate/Speed: %s\nNotes: %s\nLicense: %s\n" % (self.
model_to_download, size, error, notes, license)
253 soup = BeautifulSoup(self.
r.data,
"lxml")
255 for link
in soup.findAll(
'a'):
256 model_link = link.get(
'href')
257 if model_link.startswith(self.
url):
258 if model_to_download+
".zip" in model_link:
259 file_name = model_link.split(self.
url+
'/', 1)
260 file_name = file_name[1]
261 return model_link, file_name
267 info =
"Name: %s\nSize: %s\nWord error rate/Speed: %s\nNotes: %s\nLicense: %s\n" % (self.
model_to_download, size, error, notes, license)
270 def unzip(self, directory, filename):
271 with zipfile.ZipFile(directory+filename,
'r')
as zip_ref:
272 zip_ref.extractall(directory)
273 os.remove(directory+filename)
283 if __name__ ==
'__main__':
285 downloader.execute_standalone()