Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
import openvino_genai as ov_genai
|
3 |
-
import huggingface_hub as hf_hub
|
4 |
import queue
|
5 |
import threading
|
6 |
import time
|
7 |
|
|
|
8 |
# 初始化 OpenVINO 模型
|
9 |
model_id = "OpenVINO/Qwen3-0.6B-int4-ov"
|
10 |
model_path = "Qwen3-0.6B-int4-ov"
|
11 |
hf_hub.snapshot_download(model_id, local_dir=model_path)
|
12 |
|
13 |
pipe = ov_genai.LLMPipeline(model_path, "CPU")
|
14 |
-
#pipe.start_chat()
|
15 |
|
16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def generate_stream(prompt):
|
18 |
q = queue.Queue()
|
19 |
|
20 |
def streamer(subword):
|
21 |
-
print(subword, end=
|
22 |
q.put(subword)
|
23 |
return ov_genai.StreamingStatus.RUNNING
|
24 |
|
25 |
def worker():
|
26 |
-
# 在背景 thread 中做推論
|
27 |
pipe.generate([prompt], streamer=streamer, max_new_tokens=4096)
|
28 |
-
q.put(None)
|
29 |
|
30 |
threading.Thread(target=worker).start()
|
31 |
|
@@ -35,9 +44,9 @@ def generate_stream(prompt):
|
|
35 |
if token is None:
|
36 |
break
|
37 |
result += token
|
38 |
-
yield result
|
39 |
-
|
40 |
|
|
|
41 |
with gr.Blocks(css="""
|
42 |
#scrollbox textarea {
|
43 |
overflow-y: auto !important;
|
@@ -46,15 +55,38 @@ with gr.Blocks(css="""
|
|
46 |
white-space: pre-wrap;
|
47 |
}
|
48 |
""") as demo:
|
49 |
-
|
|
|
50 |
|
51 |
textbox_input = gr.Textbox(label="Prompt", lines=1, placeholder="Enter prompt here...")
|
52 |
textbox_output = gr.Textbox(label="Output", elem_id="scrollbox", lines=10)
|
53 |
|
54 |
-
#
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
-
|
|
|
|
|
|
|
58 |
button.click(fn=generate_stream, inputs=textbox_input, outputs=textbox_output)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import openvino_genai as ov_genai
|
|
|
3 |
import queue
|
4 |
import threading
|
5 |
import time
|
6 |
|
7 |
+
import huggingface_hub as hf_hub
|
8 |
# 初始化 OpenVINO 模型
|
9 |
model_id = "OpenVINO/Qwen3-0.6B-int4-ov"
|
10 |
model_path = "Qwen3-0.6B-int4-ov"
|
11 |
hf_hub.snapshot_download(model_id, local_dir=model_path)
|
12 |
|
13 |
pipe = ov_genai.LLMPipeline(model_path, "CPU")
|
|
|
14 |
|
15 |
+
# === Gradio Callback Functions ===
|
16 |
+
|
17 |
+
def start_chat():
|
18 |
+
print("🔄 start_chat() 被觸發")
|
19 |
+
pipe.start_chat()
|
20 |
+
return "Chat session started."
|
21 |
+
|
22 |
+
def finish_chat():
|
23 |
+
print("🛑 finish_chat() 被觸發")
|
24 |
+
pipe.finish_chat()
|
25 |
+
return "Chat session ended."
|
26 |
+
|
27 |
def generate_stream(prompt):
|
28 |
q = queue.Queue()
|
29 |
|
30 |
def streamer(subword):
|
31 |
+
print(subword, end="", flush=True)
|
32 |
q.put(subword)
|
33 |
return ov_genai.StreamingStatus.RUNNING
|
34 |
|
35 |
def worker():
|
|
|
36 |
pipe.generate([prompt], streamer=streamer, max_new_tokens=4096)
|
37 |
+
q.put(None)
|
38 |
|
39 |
threading.Thread(target=worker).start()
|
40 |
|
|
|
44 |
if token is None:
|
45 |
break
|
46 |
result += token
|
47 |
+
yield result
|
|
|
48 |
|
49 |
+
# === Gradio UI ===
|
50 |
with gr.Blocks(css="""
|
51 |
#scrollbox textarea {
|
52 |
overflow-y: auto !important;
|
|
|
55 |
white-space: pre-wrap;
|
56 |
}
|
57 |
""") as demo:
|
58 |
+
|
59 |
+
gr.Markdown("## 🧠 OpenVINO Streaming Chatbot with Lifecycle Control")
|
60 |
|
61 |
textbox_input = gr.Textbox(label="Prompt", lines=1, placeholder="Enter prompt here...")
|
62 |
textbox_output = gr.Textbox(label="Output", elem_id="scrollbox", lines=10)
|
63 |
|
64 |
+
# 隱藏按鈕控制生命周期
|
65 |
+
hidden_start_btn = gr.Button(visible=False)
|
66 |
+
hidden_end_btn = gr.Button(visible=False)
|
67 |
+
|
68 |
+
status_text = gr.Textbox(visible=False)
|
69 |
|
70 |
+
hidden_start_btn.click(fn=start_chat, outputs=status_text)
|
71 |
+
hidden_end_btn.click(fn=finish_chat, outputs=status_text)
|
72 |
+
|
73 |
+
button = gr.Button("Submit")
|
74 |
button.click(fn=generate_stream, inputs=textbox_input, outputs=textbox_output)
|
75 |
|
76 |
+
# JavaScript 在頁面載入/離開時觸發
|
77 |
+
demo.load(
|
78 |
+
None,
|
79 |
+
_js="""
|
80 |
+
() => {
|
81 |
+
// 頁面載入時觸發 start_chat
|
82 |
+
document.querySelector('button[id^="component-"]').click();
|
83 |
+
// 離開時觸發 finish_chat
|
84 |
+
window.addEventListener("beforeunload", () => {
|
85 |
+
document.querySelectorAll('button[id^="component-"]')[1].click();
|
86 |
+
});
|
87 |
+
return;
|
88 |
+
}
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
|
92 |
demo.launch()
|