mirror of https://github.com/skygpu/skynet.git
Improve tg frontend ipfs results gathering parallelism
parent
d749dc4f57
commit
16df97d731
|
@ -264,7 +264,6 @@ class SkynetGPUConnector:
|
||||||
ipfs_link_legacy = ipfs_link + '/image.png'
|
ipfs_link_legacy = ipfs_link + '/image.png'
|
||||||
|
|
||||||
async with trio.open_nursery() as n:
|
async with trio.open_nursery() as n:
|
||||||
|
|
||||||
async def get_and_set_results(link: str):
|
async def get_and_set_results(link: str):
|
||||||
res = await get_ipfs_file(link, timeout=1)
|
res = await get_ipfs_file(link, timeout=1)
|
||||||
logging.info(f'got response from {link}')
|
logging.info(f'got response from {link}')
|
||||||
|
|
|
@ -242,66 +242,50 @@ class SkynetTelegramFrontend:
|
||||||
ipfs_link_legacy = ipfs_link + '/image.png'
|
ipfs_link_legacy = ipfs_link + '/image.png'
|
||||||
|
|
||||||
async def get_and_set_results(link: str):
|
async def get_and_set_results(link: str):
|
||||||
results[link] = await get_ipfs_file(link)
|
res = await get_ipfs_file(link)
|
||||||
|
logging.info(f'got response from {link}')
|
||||||
|
if not res or res.status_code != 200:
|
||||||
|
logging.warning(f'couldn\'t get ipfs binary data at {link}!')
|
||||||
|
|
||||||
def get_image_from_resp(resp):
|
else:
|
||||||
png_img = resp.raw
|
try:
|
||||||
with Image.open(io.BytesIO(resp.raw)) as image:
|
with Image.open(io.BytesIO(res.raw)) as image:
|
||||||
w, h = image.size
|
w, h = image.size
|
||||||
|
|
||||||
if w > TG_MAX_WIDTH or h > TG_MAX_HEIGHT:
|
if w > TG_MAX_WIDTH or h > TG_MAX_HEIGHT:
|
||||||
logging.warning(f'result is of size {image.size}')
|
logging.warning(f'result is of size {image.size}')
|
||||||
image.thumbnail((TG_MAX_WIDTH, TG_MAX_HEIGHT))
|
image.thumbnail((TG_MAX_WIDTH, TG_MAX_HEIGHT))
|
||||||
tmp_buf = io.BytesIO()
|
|
||||||
image.save(tmp_buf, format='PNG')
|
|
||||||
png_img = tmp_buf.getvalue()
|
|
||||||
|
|
||||||
return png_img
|
tmp_buf = io.BytesIO()
|
||||||
|
image.save(tmp_buf, format='PNG')
|
||||||
|
png_img = tmp_buf.getvalue()
|
||||||
|
|
||||||
|
results[link] = png_img
|
||||||
|
|
||||||
|
except UnidentifiedImageError:
|
||||||
|
logging.warning(f'couldn\'t get ipfs binary data at {link}!')
|
||||||
|
|
||||||
tasks = [
|
tasks = [
|
||||||
get_and_set_results(ipfs_link),
|
get_and_set_results(ipfs_link),
|
||||||
get_and_set_results(ipfs_link_legacy)
|
get_and_set_results(ipfs_link_legacy)
|
||||||
]
|
]
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
png_img = None
|
png_img = None
|
||||||
|
if ipfs_link_legacy in results:
|
||||||
|
png_img = results[ipfs_link_legacy]
|
||||||
|
|
||||||
resp = results[ipfs_link_legacy]
|
if ipfs_link in results:
|
||||||
if not resp or resp.status_code != 200:
|
png_img = results[ipfs_link]
|
||||||
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link_legacy}!')
|
|
||||||
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
png_img = get_image_from_resp(resp)
|
|
||||||
|
|
||||||
except UnidentifiedImageError:
|
|
||||||
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link_legacy}!')
|
|
||||||
|
|
||||||
if not png_img:
|
if not png_img:
|
||||||
resp = results[ipfs_link]
|
await self.update_status_message(
|
||||||
if not resp or resp.status_code != 200:
|
status_msg,
|
||||||
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!')
|
caption,
|
||||||
await self.update_status_message(
|
reply_markup=build_redo_menu(),
|
||||||
status_msg,
|
parse_mode='HTML'
|
||||||
caption,
|
)
|
||||||
reply_markup=build_redo_menu(),
|
return True
|
||||||
parse_mode='HTML'
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
png_img = get_image_from_resp(resp)
|
|
||||||
|
|
||||||
except UnidentifiedImageError:
|
|
||||||
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!')
|
|
||||||
await self.update_status_message(
|
|
||||||
status_msg,
|
|
||||||
caption,
|
|
||||||
reply_markup=build_redo_menu(),
|
|
||||||
parse_mode='HTML'
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
logging.info(f'success! sending generated image')
|
logging.info(f'success! sending generated image')
|
||||||
await self.bot.delete_message(
|
await self.bot.delete_message(
|
||||||
|
|
|
@ -149,7 +149,6 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'):
|
||||||
user_config = {**user_row}
|
user_config = {**user_row}
|
||||||
del user_config['id']
|
del user_config['id']
|
||||||
|
|
||||||
breakpoint()
|
|
||||||
if user_config['autoconf']:
|
if user_config['autoconf']:
|
||||||
user_config = perform_auto_conf(user_config)
|
user_config = perform_auto_conf(user_config)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue