hsuwill000 commited on
Commit
3e39aa5
·
verified ·
1 Parent(s): b307fb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -44
app.py CHANGED
@@ -11,30 +11,21 @@ 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,9 +35,9 @@ def generate_stream(prompt):
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,38 +46,15 @@ with gr.Blocks(css="""
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()
 
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
+ # 建立推論函式:使用 streamer 並回傳 generator 結果
 
 
 
 
 
 
 
 
 
 
 
17
  def generate_stream(prompt):
18
  q = queue.Queue()
19
 
20
  def streamer(subword):
21
+ print(subword, end='', flush=True)
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
  if token is None:
36
  break
37
  result += token
38
+ yield result # 把逐步結果傳給 output textbox
39
+
40
 
 
41
  with gr.Blocks(css="""
42
  #scrollbox textarea {
43
  overflow-y: auto !important;
 
46
  white-space: pre-wrap;
47
  }
48
  """) as demo:
49
+ gr.Markdown("## 🧠 OpenVINO Streaming Demo with Gradio Textbox")
 
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
  button = gr.Button("Submit")
 
56
 
57
+ # 當按鈕被按下時,呼叫 generate_stream 並更新 textbox_output
58
+ button.click(fn=generate_stream, inputs=textbox_input, outputs=textbox_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ demo.launch()