Skip to content

Commit 0964cd4

Browse files
committed
Support download tokenizer automatically.
1 parent b99c58c commit 0964cd4

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

samples/genie/python/GenieAPIClient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
parser = argparse.ArgumentParser()
1919
parser.add_argument("--stream", action="store_true")
2020
parser.add_argument("--prompt", default="你好", type=str)
21+
parser.add_argument("--model", default="IBM-Granite-v3.1-8B", type=str)
2122
args = parser.parse_args()
2223

2324
client = OpenAI(base_url="http://" + HOST + ":" + PORT + "/v1", api_key="123")
@@ -28,8 +29,7 @@
2829
messages = [{"role": "system", "content": "You are a math teacher who teaches algebra."}, {"role": "user", "content": args.prompt}]
2930
extra_body = {"size": 4096, "seed": 146, "temp": 1.5, "top_k": 13, "top_p": 0.6, "penalty_last_n": 64, "penalty_repeat": 1.3}
3031

31-
model_name = "IBM-Granite-v3.1-8B"
32-
# model_name = "Phi-3.5-mini"
32+
model_name = args.model
3333

3434
if args.stream:
3535
response = client.chat.completions.create(model=model_name, stream=True, messages=messages, extra_body=extra_body)

samples/genie/python/GenieAPIService.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
sys.path.append("python")
2222

23+
import utils.install as install
2324
import stable_diffusion_v2_1.stable_diffusion_v2_1 as stable_diffusion
2425

2526
sys.stdin.reconfigure(encoding='utf-8')
@@ -429,13 +430,37 @@ def shutdown():
429430

430431
app.add_api_route('/shutdown', shutdown, methods=['GET'])
431432

433+
def download():
434+
PATH_Tokenizer = APP_PATH + "\\models\\IBM-Granite-v3.1-8B\\tokenizer.json"
435+
URL_Tokenizer = "https://gitee.com/hf-models/granite-3.1-8b-base/raw/main/tokenizer.json"
436+
437+
if not os.path.exists(PATH_Tokenizer):
438+
install.download_url(URL_Tokenizer, PATH_Tokenizer)
439+
440+
PATH_Tokenizer = APP_PATH + "\\models\\Phi-3.5-mini\\tokenizer.json"
441+
URL_Tokenizer = "https://gitee.com/hf-models/Phi-3.5-mini-instruct/raw/main/tokenizer.json"
442+
443+
if not os.path.exists(PATH_Tokenizer):
444+
install.download_url(URL_Tokenizer, PATH_Tokenizer)
445+
446+
import re
447+
with open(PATH_Tokenizer, 'r', encoding='utf-8') as f:
448+
content = f.read()
449+
pattern = r',\s*{\s*"type":\s*"Strip",\s*"content":\s*"\s*",\s*"start":\s*\d+,\s*"stop":\s*\d+\s*}'
450+
new_content = re.sub(pattern, '', content)
451+
with open(PATH_Tokenizer, 'w', encoding='utf-8') as f:
452+
f.write(new_content)
453+
454+
432455
def model_load(model_name):
433456
global llm, _current_model, _model_list, _max_query_times
434457
find_model = False
435458

436459
if model_name.lower() in _current_model.lower():
437460
return False
438461

462+
download()
463+
439464
if _model_list is None:
440465
model_root = APP_PATH + "models"
441466
_model_list = [f for f in os.listdir(model_root) if os.path.isdir(os.path.join(model_root, f))]

samples/python/utils/install.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ def verify_package(url, filepath, filesize, desc=None, fail=None):
107107
else:
108108
is_continue = True
109109

110-
print("If the download speed is too slow, you can download them from one of below URLs and install it manually before you run this 'setup.py'.")
111-
print("https://softwarecenter.qualcomm.com/#/catalog/item/Qualcomm_AI_Runtime_SDK")
112-
print("https://qpm.qualcomm.com/#/main/tools/details/Qualcomm_AI_Runtime_SDK")
113110
print()
114111

115112
if is_continue:
@@ -404,6 +401,11 @@ def install_qai_sdk(version):
404401
f"If the downloading speed is too slow, please download it manually from {QNN_DOWNLOAD_URL} and install it. " + TEXT_RUN_SCRIPT_AGAIN
405402
fail = f"Failed to download file from {url}: \n\t1. Please try again a few times.\n\t2. If still doesn't work, please try to download it manually"\
406403
f" from {QNN_DOWNLOAD_URL} and install it. " + TEXT_RUN_SCRIPT_AGAIN
404+
405+
print("If the download speed is too slow, you can download them from one of below URLs and install it manually before you run this 'setup.py'.")
406+
print("https://softwarecenter.qualcomm.com/#/catalog/item/Qualcomm_AI_Runtime_SDK")
407+
print("https://qpm.qualcomm.com/#/main/tools/details/Qualcomm_AI_Runtime_SDK")
408+
407409
ret = download_url(url, qnn_zip_path, desc=desc, fail=fail)
408410
# install_clean(QNN_SDK_ROOT, zip_name)
409411
if not ret:

0 commit comments

Comments
 (0)