Spaces:
Running
Running
vlff李飞飞
commited on
Commit
·
68743ec
1
Parent(s):
83f4493
add b2
Browse files- qwen_server/utils.py +48 -0
- requirements.txt +2 -1
qwen_server/utils.py
CHANGED
@@ -10,6 +10,54 @@ from qwen_agent.log import logger
|
|
10 |
from qwen_agent.utils.doc_parser import parse_doc, parse_html_bs
|
11 |
from qwen_agent.utils.utils import print_traceback, save_text_to_file
|
12 |
from qwen_server.schema import Record
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def is_local_path(path):
|
|
|
10 |
from qwen_agent.utils.doc_parser import parse_doc, parse_html_bs
|
11 |
from qwen_agent.utils.utils import print_traceback, save_text_to_file
|
12 |
from qwen_server.schema import Record
|
13 |
+
from b2sdk.v2 import B2Api
|
14 |
+
from b2sdk.v2 import InMemoryAccountInfo
|
15 |
+
import hashlib
|
16 |
+
from datetime import datetime
|
17 |
+
from io import BytesIO
|
18 |
+
|
19 |
+
|
20 |
+
class B2Manager():
|
21 |
+
def __init__(self):
|
22 |
+
info = InMemoryAccountInfo()
|
23 |
+
b2_api = B2Api(info)
|
24 |
+
application_key_id = os.environ.get("b2_key_id")
|
25 |
+
application_key = os.environ.get("b2_key")
|
26 |
+
b2_api.authorize_account("production", application_key_id, application_key)
|
27 |
+
self.b2_bucket = b2_api.get_bucket_by_name(os.environ.get("b2_bucket_name"))
|
28 |
+
self.b2_api = b2_api
|
29 |
+
self.file_name = None
|
30 |
+
|
31 |
+
def gen_file_name(self, access_token, url):
|
32 |
+
url_md5 = hashlib.md5(b'%s' % url.encode(encoding='UTF-8')).hexdigest()
|
33 |
+
self.file_name = f"{access_token}/{url_md5}"
|
34 |
+
|
35 |
+
def get(self):
|
36 |
+
in_memory_file = BytesIO()
|
37 |
+
self.b2_bucket.download_file_by_name(self.file_name).save(in_memory_file)
|
38 |
+
# export_file = self.b2_bucket.download_file_by_name(self.file_name)
|
39 |
+
# export_file.save(in_memory_file)
|
40 |
+
in_memory_file.seek(0)
|
41 |
+
return in_memory_file.read()
|
42 |
+
|
43 |
+
def upsert(self, file_path, content):
|
44 |
+
with open(file_path, 'rb') as file_data:
|
45 |
+
# file_info = {'description': ''}
|
46 |
+
self.b2_bucket.upload_bytes(file_data.read(), self.file_name, file_infos=None)
|
47 |
+
|
48 |
+
def delete(self):
|
49 |
+
file_version_info = self.b2_bucket.get_file_info_by_name(self.file_name)
|
50 |
+
self.b2_bucket.hide_file(file_version_info.file_name)
|
51 |
+
# for version in self.b2_bucket.list_file_versions(self.file_name):
|
52 |
+
# self.b2_bucket.delete_file_version(version.id_, version.file_name)
|
53 |
+
|
54 |
+
def list_files(self, access_token):
|
55 |
+
files = []
|
56 |
+
for file_version_info, folder_name in self.b2_bucket.ls(folder_to_list="access_token/", show_versions=False):
|
57 |
+
# The upload timestamp is in milliseconds, so we divide by 1000 to convert it to seconds
|
58 |
+
upload_timestamp = datetime.fromtimestamp(file_version_info.upload_timestamp / 1000.0)
|
59 |
+
files.append(f"File Name: {file_version_info.file_name}, \nUpload timestamp: {upload_timestamp}, \nMetadata: {file_version_info.file_info}")
|
60 |
+
return files
|
61 |
|
62 |
|
63 |
def is_local_path(path):
|
requirements.txt
CHANGED
@@ -35,4 +35,5 @@ torch==2.0.1
|
|
35 |
transformers_stream_generator
|
36 |
einops
|
37 |
accelerate
|
38 |
-
itsdangerous
|
|
|
|
35 |
transformers_stream_generator
|
36 |
einops
|
37 |
accelerate
|
38 |
+
itsdangerous
|
39 |
+
b2sdk
|