Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,85 +10,104 @@ import threading
|
|
10 |
model_ids = [
|
11 |
"OpenVINO/Qwen3-0.6B-int4-ov",
|
12 |
"OpenVINO/Qwen3-1.7B-int4-ov",
|
13 |
-
#"OpenVINO/Qwen3-4B-int4-ov"
|
14 |
"OpenVINO/Qwen3-8B-int4-ov",
|
15 |
"OpenVINO/Qwen3-14B-int4-ov",
|
16 |
-
|
17 |
]
|
18 |
|
19 |
-
model_name_to_full_id = {model_id.split("/")[-1]: model_id for model_id in model_ids} #Create Dictionary
|
20 |
|
21 |
-
|
22 |
model_path = model_id.split("/")[-1] # Extract model name
|
23 |
try:
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# 建立推理管線 (Initialize with a default model first)
|
30 |
device = "CPU"
|
31 |
default_model_name = "Qwen3-0.6B-int4-ov" # Choose a default model
|
|
|
32 |
# 全局变量,用于存储推理管线、分词器、Markdown 组件和累计文本
|
33 |
pipe = None
|
34 |
tokenizer = None
|
35 |
-
markdown_component = None # 初始化
|
36 |
accumulated_text = ""
|
37 |
|
|
|
|
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
global markdown_component
|
42 |
-
if markdown_component:
|
43 |
-
markdown_component.update(value=text)
|
44 |
|
45 |
# 创建 streamer 函数 (保持原有架构)
|
46 |
def streamer(subword):
|
47 |
global accumulated_text
|
48 |
accumulated_text += subword
|
49 |
print(subword, end='', flush=True) # 保留打印到控制台
|
50 |
-
#
|
51 |
-
|
52 |
-
return ov_genai.StreamingStatus.RUNNING
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
|
|
55 |
def generate_response(prompt, model_name):
|
56 |
-
global pipe, tokenizer
|
57 |
-
|
58 |
-
model_path = model_name
|
59 |
|
60 |
-
|
61 |
-
pipe
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
try:
|
66 |
-
#generated = pipe.generate([prompt], max_length=1024)
|
67 |
generated = pipe.generate(prompt, streamer=streamer, max_new_tokens=100)
|
68 |
-
tokenpersec=f'{generated.perf_metrics.get_throughput().mean:.2f}'
|
69 |
-
|
70 |
-
return tokenpersec, generated
|
71 |
except Exception as e:
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
gr.
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
10 |
model_ids = [
|
11 |
"OpenVINO/Qwen3-0.6B-int4-ov",
|
12 |
"OpenVINO/Qwen3-1.7B-int4-ov",
|
13 |
+
#"OpenVINO/Qwen3-4B-int4-ov", #不可用
|
14 |
"OpenVINO/Qwen3-8B-int4-ov",
|
15 |
"OpenVINO/Qwen3-14B-int4-ov",
|
|
|
16 |
]
|
17 |
|
18 |
+
model_name_to_full_id = {model_id.split("/")[-1]: model_id for model_id in model_ids} # Create Dictionary
|
19 |
|
20 |
+
def download_model(model_id):
|
21 |
model_path = model_id.split("/")[-1] # Extract model name
|
22 |
try:
|
23 |
+
hf_hub.snapshot_download(model_id, local_dir=model_path, local_dir_use_symlinks=False)
|
24 |
+
print(f"Successfully downloaded {model_id} to {model_path}")
|
25 |
+
# 檢查模型檔案是否完整 (可以加入具體的檔案檢查)
|
26 |
+
# 例如,檢查必須存在的檔案是否存在,或驗證檔案大小
|
27 |
+
return True
|
28 |
except Exception as e:
|
29 |
+
print(f"Error downloading {model_id}: {e}")
|
30 |
+
return False
|
31 |
+
|
32 |
+
# 下載所有模型
|
33 |
+
for model_id in model_ids:
|
34 |
+
if not download_model(model_id):
|
35 |
+
print(f"Failed to download {model_id}, skipping.")
|
36 |
|
37 |
# 建立推理管線 (Initialize with a default model first)
|
38 |
device = "CPU"
|
39 |
default_model_name = "Qwen3-0.6B-int4-ov" # Choose a default model
|
40 |
+
|
41 |
# 全局变量,用于存储推理管线、分词器、Markdown 组件和累计文本
|
42 |
pipe = None
|
43 |
tokenizer = None
|
|
|
44 |
accumulated_text = ""
|
45 |
|
46 |
+
# 初始化 Markdown 组件
|
47 |
+
markdown_component = None # 在全局範圍初始化
|
48 |
|
49 |
+
# 建立 Gradio 介面
|
50 |
+
model_choices = list(model_name_to_full_id.keys())
|
|
|
|
|
|
|
51 |
|
52 |
# 创建 streamer 函数 (保持原有架构)
|
53 |
def streamer(subword):
|
54 |
global accumulated_text
|
55 |
accumulated_text += subword
|
56 |
print(subword, end='', flush=True) # 保留打印到控制台
|
57 |
+
return accumulated_text # 返回更新後的文字,Gradio會自動更新Markdown元件
|
58 |
+
|
|
|
59 |
|
60 |
+
# 模型載入函數
|
61 |
+
def load_model(model_name):
|
62 |
+
global pipe, tokenizer
|
63 |
+
model_path = model_name
|
64 |
+
print(f"Loading model: {model_name}")
|
65 |
+
try:
|
66 |
+
pipe = ov_genai.LLMPipeline(model_path, device)
|
67 |
+
tokenizer = pipe.get_tokenizer()
|
68 |
+
tokenizer.set_chat_template(tokenizer.chat_template) # 確保 chat template 已設定
|
69 |
+
print(f"Model {model_name} loaded successfully.")
|
70 |
+
return True
|
71 |
+
except Exception as e:
|
72 |
+
print(f"Error loading model {model_name}: {e}")
|
73 |
+
return False
|
74 |
|
75 |
+
# 產生回應的函數
|
76 |
def generate_response(prompt, model_name):
|
77 |
+
global pipe, tokenizer, accumulated_text
|
|
|
|
|
78 |
|
79 |
+
# 如果模型尚未載入,或需要切換模型,則載入模型
|
80 |
+
if pipe is None or pipe.model_name != model_name:
|
81 |
+
if not load_model(model_name):
|
82 |
+
return "模型載入失敗", "模型載入失敗", "模型載入失敗"
|
83 |
+
|
84 |
+
accumulated_text = "" #重置累積文字
|
85 |
|
86 |
try:
|
|
|
87 |
generated = pipe.generate(prompt, streamer=streamer, max_new_tokens=100)
|
88 |
+
tokenpersec = f'{generated.perf_metrics.get_throughput().mean:.2f}'
|
89 |
+
return tokenpersec, accumulated_text
|
|
|
90 |
except Exception as e:
|
91 |
+
error_message = f"生成回應時發生錯誤:{e}"
|
92 |
+
print(error_message)
|
93 |
+
return "發生錯誤", "發生錯誤", error_message
|
94 |
+
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
markdown_component = gr.Markdown(label="回应") # 在Blocks內部初始化
|
97 |
+
with gr.Row():
|
98 |
+
prompt_textbox = gr.Textbox(lines=5, label="輸入提示 (Prompt)")
|
99 |
+
model_dropdown = gr.Dropdown(choices=model_choices, value=default_model_name, label="選擇模型")
|
100 |
+
with gr.Row():
|
101 |
+
token_per_sec_textbox = gr.Textbox(label="tokens/sec")
|
102 |
+
|
103 |
+
def process_input(prompt, model_name):
|
104 |
+
tokens_sec, response = generate_response(prompt, model_name)
|
105 |
+
return tokens_sec, response
|
106 |
+
|
107 |
+
prompt_textbox.submit(
|
108 |
+
fn=process_input,
|
109 |
+
inputs=[prompt_textbox, model_dropdown],
|
110 |
+
outputs=[token_per_sec_textbox, markdown_component]
|
111 |
+
)
|
112 |
+
|
113 |
+
demo.launch()
|