Spaces:
Running
Running
Update flashcard_util.py
Browse files- flashcard_util.py +25 -3
flashcard_util.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import lang
|
2 |
import requests
|
3 |
import json
|
4 |
-
|
|
|
5 |
from imgsearch import search_img
|
6 |
from re import match, sub
|
|
|
7 |
|
8 |
from env import LLM_API_KEY
|
9 |
|
@@ -36,9 +38,29 @@ async def tldr(content, l=lang.VI_VN):
|
|
36 |
# print(_summary)
|
37 |
return _summary['choices'][0]['message']['content'].split('\n',1)[-1].strip()
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
print("fetching images...")
|
41 |
-
_img_link = [search_img(r) for r in words]
|
42 |
return [(word,img) for (word, img) in zip(words, _img_link)]
|
43 |
|
44 |
async def get_definitions_from_words(words: list[str], summary: str = "", lang: str = lang.VI_VN):
|
|
|
1 |
import lang
|
2 |
import requests
|
3 |
import json
|
4 |
+
import httpx
|
5 |
+
import aiofiles
|
6 |
from imgsearch import search_img
|
7 |
from re import match, sub
|
8 |
+
from env import HOST_URL
|
9 |
|
10 |
from env import LLM_API_KEY
|
11 |
|
|
|
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 |
+
|
56 |
+
_loop = asyncio.get_running_loop()
|
57 |
+
_out = await _loop.run_in_executor(None, partial(optimize_image, _fn))
|
58 |
+
|
59 |
+
return f"{_prefix}/images/{_fn}.webp"
|
60 |
+
|
61 |
+
async def fetch_img_for_words(words: list[str], __url_prefix=None):
|
62 |
print("fetching images...")
|
63 |
+
_img_link = [await download_img(search_img(r)) for r in words]
|
64 |
return [(word,img) for (word, img) in zip(words, _img_link)]
|
65 |
|
66 |
async def get_definitions_from_words(words: list[str], summary: str = "", lang: str = lang.VI_VN):
|