hsuwill000 commited on
Commit
4b02b47
·
verified ·
1 Parent(s): 63924d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,18 +1,45 @@
1
- import gradio as gr
 
2
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- def generate_markdown():
5
- response = ""
6
- for word in ["Hello", "World", "Gradio", "Markdown", "!"]:
7
- response += word + " "
8
- time.sleep(0.5)
9
- yield response
10
 
11
- with gr.Blocks() as demo:
12
- markdown_output = gr.Markdown(label="回應")
13
- button = gr.Button("生成")
 
14
 
15
- button.click(generate_markdown, outputs=markdown_output)
 
 
 
 
 
 
 
 
 
 
16
 
17
- demo.queue()
18
- demo.launch()
 
1
+
2
+ import huggingface_hub as hf_hub
3
  import time
4
+ import openvino_genai as ov_genai
5
+ import numpy as np
6
+ import gradio as gr
7
+ import re
8
+
9
+ # 下載模型
10
+ model_id = "OpenVINO/Qwen3-0.6B-int4-ov"
11
+ model_path = "Qwen3-0.6B-int4-ov"
12
+
13
+ hf_hub.snapshot_download(model_id, local_dir=model_path, local_dir_use_symlinks=False)
14
+
15
+ # 建立推理管線
16
+ device = "CPU"
17
+ pipe = ov_genai.LLMPipeline(model_path, device)
18
+ tokenizer = pipe.get_tokenizer()
19
+ tokenizer.set_chat_template(tokenizer.chat_template)
20
+
21
 
22
+ def generate_response(prompt):
23
+ try:
24
+ generated = pipe.generate([prompt], max_length=1024)
25
+ tokenpersec=f'{generated.perf_metrics.get_throughput().mean:.2f}'
 
 
26
 
27
+ return tokenpersec, generated
28
+ except Exception as e:
29
+ return "發生錯誤", "發生錯誤", f"生成回應時發生錯誤:{e}"
30
+
31
 
32
+ # 建立 Gradio 介面
33
+ demo = gr.Interface(
34
+ fn=generate_response,
35
+ inputs=gr.Textbox(lines=5, label="輸入提示 (Prompt)"),
36
+ outputs=[
37
+ gr.Textbox(label="tokens/sec"),
38
+ gr.Textbox(label="回應")
39
+ ],
40
+ title="Qwen3-0.6B-int4-ov ",
41
+ description="基於 Qwen3-0.6B-int4-ov 推理應用,支援思考過程分離與 GUI。"
42
+ )
43
 
44
+ if __name__ == "__main__":
45
+ demo.launch()