Skip to content

Commit b226903

Browse files
committed
CHANGES:
- SkyEye: improved update, whisper-model can be auto-updated also now.
1 parent da15a76 commit b226903

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

extensions/skyeye/extension.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import os
88
import psutil
99
import re
10+
import shutil
1011
import ssl
1112
import subprocess
13+
import tempfile
1214
import zipfile
1315

1416
from contextlib import suppress
@@ -107,14 +109,35 @@ def set_affinity(self, process: psutil.Process, affinity: Union[list[int], str])
107109
async def download_whisper_file(self, name: str):
108110
whisper_path = os.path.join(os.path.dirname(self.get_exe_path()), "whisper.bin")
109111
async with aiohttp.ClientSession() as session:
110-
async with session.get(WHISPER_URL.format(name), raise_for_status=True, proxy=self.node.proxy,
111-
proxy_auth=self.node.proxy_auth) as response:
112-
with open(whisper_path, 'wb') as f:
113-
while True:
114-
chunk = await response.content.read(1024 * 1024)
115-
if not chunk:
116-
break
117-
f.write(chunk)
112+
# Check the size of the remote file
113+
head_resp = await session.head(WHISPER_URL.format(name), allow_redirects=True, proxy=self.node.proxy,
114+
proxy_auth=self.node.proxy_auth)
115+
remote_size = int(head_resp.headers['Content-Length'])
116+
117+
# Check the size of the local file
118+
if os.path.exists(whisper_path):
119+
local_size = os.path.getsize(whisper_path)
120+
else:
121+
local_size = 0
122+
123+
# Download and update the file only if there's a new version available online
124+
if remote_size != local_size:
125+
if local_size == 0:
126+
what = 'download'
127+
else:
128+
what = 'updat'
129+
self.log.info(f" => {self.name}: {what.title()}ing whisper model...")
130+
async with session.get(WHISPER_URL.format(name), raise_for_status=True, proxy=self.node.proxy,
131+
proxy_auth=self.node.proxy_auth) as response:
132+
with open(whisper_path, 'wb') as f:
133+
while True:
134+
chunk = await response.content.read(1024 * 1024)
135+
if not chunk:
136+
break
137+
f.write(chunk)
138+
self.log.info(f" => {self.name}: Whisper model {what}ed.")
139+
else:
140+
self.log.debug(f" => {self.name}: Whisper model up-to-date.")
118141

119142
def _maybe_update_config(self, cfg: dict, key: str, value: Any):
120143
if not value:
@@ -133,12 +156,7 @@ async def _prepare_config(self, cfg: dict):
133156

134157
# make sure we have a local model, unless configured otherwise
135158
if cfg.get('recognizer', 'openai-whisper-local') == 'openai-whisper-local':
136-
self.log.warning(f" => {self.name}: Local Whisper model configured. This has a performance impact on your system!")
137-
whisper_path = os.path.join(os.path.dirname(self.get_exe_path()), "whisper.bin")
138-
if not os.path.exists(whisper_path):
139-
self.log.info(f" => {self.name}: Downloading whisper model...")
140-
await self.download_whisper_file(cfg.get('whisper-model', 'ggml-small.en.bin'))
141-
self.log.info(f" => {self.name}: Whisper model downloaded.")
159+
await self.download_whisper_file(cfg.get('whisper-model', 'ggml-small.en.bin'))
142160
dirty |= self._maybe_update_config(cfg, 'recognizer', 'openai-whisper-local')
143161
dirty |= self._maybe_update_config(cfg,'recognizer-lock-path',
144162
os.path.join(os.path.dirname(self.get_exe_path()), 'recognizer.lck'))
@@ -462,16 +480,17 @@ async def do_update(self, version: str):
462480
async with aiohttp.ClientSession() as session:
463481
async with session.get(SKYEYE_DOWNLOAD_URL.format(version), raise_for_status=True, proxy=self.node.proxy,
464482
proxy_auth=self.node.proxy_auth) as response:
465-
with zipfile.ZipFile(BytesIO(await response.content.read())) as z:
466-
root_folder = z.namelist()[0].split('/')[0]
467-
for member in z.namelist():
468-
relative_path = os.path.relpath(member, start=root_folder)
469-
if relative_path == ".":
470-
continue
471-
destination_path = os.path.join(installation_dir, relative_path)
472-
if member.endswith('/'):
473-
os.makedirs(destination_path, exist_ok=True)
474-
else:
475-
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
476-
with open(destination_path, 'wb') as output_file:
477-
output_file.write(z.read(member))
483+
zipdata = BytesIO(await response.content.read())
484+
with tempfile.TemporaryDirectory() as tmpdir:
485+
with zipfile.ZipFile(zipdata) as z:
486+
z.extractall(tmpdir)
487+
root_folder = os.path.join(tmpdir, 'skyeye-windows-amd64')
488+
for item in os.listdir(root_folder):
489+
source_path = os.path.join(root_folder, item)
490+
destination_path = os.path.join(installation_dir, item)
491+
if os.path.exists(destination_path):
492+
if os.path.isdir(destination_path):
493+
shutil.rmtree(destination_path)
494+
else:
495+
os.remove(destination_path)
496+
shutil.move(source_path, destination_path)

0 commit comments

Comments
 (0)