nhathuy07 commited on
Commit
ca2193d
·
verified ·
1 Parent(s): 491df38

Update flashcard_util.py

Browse files
Files changed (1) hide show
  1. flashcard_util.py +16 -16
flashcard_util.py CHANGED
@@ -38,28 +38,28 @@ async def tldr(content, l=lang.VI_VN):
38
  # print(_summary)
39
  return _summary['choices'][0]['message']['content'].split('\n',1)[-1].strip()
40
 
41
- import io
42
- import asyncio
43
- from functools import partial
44
- def optimize_image(data, _fn):
45
- with Image.open(io.BytesIO(data)) as img:
46
- img.save(f'{_fn}.webp', optimize=True)
47
 
48
- async def download_img(_url, _prefix=HOST_URL):
49
- _r = None
50
- async with httpx.AsyncClient() as client:
51
- _r = await client.get(url=_url, timeout=None)
52
 
53
- _fn = f'img_{str(hash(_url))}'
54
- if _r.status_code//100 == 2:
55
- _loop = asyncio.get_running_loop()
56
- _out = await _loop.run_in_executor(None, partial(optimize_image, _r.content, _fn))
57
 
58
- return f"{_prefix}/images/{_fn}.webp"
59
 
60
  async def fetch_img_for_words(words: list[str], __url_prefix=None):
61
  print("fetching images...")
62
- _img_link = [await download_img(search_img(r)) for r in words]
63
  return [(word,img) for (word, img) in zip(words, _img_link)]
64
 
65
  async def get_definitions_from_words(words: list[str], summary: str = "", lang: str = lang.VI_VN):
 
38
  # print(_summary)
39
  return _summary['choices'][0]['message']['content'].split('\n',1)[-1].strip()
40
 
41
+ # import io
42
+ # import asyncio
43
+ # from functools import partial
44
+ # def optimize_image(data, _fn):
45
+ # with Image.open(io.BytesIO(data)) as img:
46
+ # img.save(f'{_fn}.webp', optimize=True)
47
 
48
+ # async def download_img(_url, _prefix=HOST_URL):
49
+ # _r = None
50
+ # async with httpx.AsyncClient() as client:
51
+ # _r = await client.get(url=_url, timeout=None)
52
 
53
+ # _fn = f'img_{str(hash(_url))}'
54
+ # if _r.status_code//100 == 2:
55
+ # _loop = asyncio.get_running_loop()
56
+ # _out = await _loop.run_in_executor(None, partial(optimize_image, _r.content, _fn))
57
 
58
+ # return f"{_prefix}/images/{_fn}.webp"
59
 
60
  async def fetch_img_for_words(words: list[str], __url_prefix=None):
61
  print("fetching images...")
62
+ _img_link = [search_img(r) for r in words]
63
  return [(word,img) for (word, img) in zip(words, _img_link)]
64
 
65
  async def get_definitions_from_words(words: list[str], summary: str = "", lang: str = lang.VI_VN):