xzg82465 commited on
Commit
917b23a
·
verified ·
1 Parent(s): 4549088

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -39
app.py CHANGED
@@ -3,41 +3,24 @@ from datetime import datetime
3
  import time
4
 
5
 
 
6
  def run_setup(tickersymbol, download_data, download_update, use_train, method):
7
- # --- 1. 產生設定參數摘要 (顯示在第一個 Textbox) ---
8
  config_summary = f"股票代碼:{tickersymbol}\n"
9
  config_summary += f"更新資料:{'是' if download_data else '否'} (日期: {download_update})\n"
10
  config_summary += f"啟用訓練:{'是' if use_train else '否'} (方式: {method})"
11
-
12
- # --- 2. 模擬終端機運行狀態 (顯示在第二個 Textbox) ---
13
- logs = "系統啟動中...\n"
14
- yield config_summary, logs # 第一次回傳:顯示設定並初始化 Log
15
-
16
- time.sleep(0.5)
17
- logs += f"> 正在檢查 {tickersymbol} 歷史數據...\n"
18
- yield config_summary, logs
19
 
20
- if download_data:
21
- logs += f"> 正在從伺服器下載更新 (至 {download_update})...\n"
22
- yield config_summary, logs
23
- time.sleep(1.2)
24
- logs += "> [OK] 數據更新完成。\n"
25
- yield config_summary, logs
26
-
27
- if use_train:
28
- logs += f"> 進入訓練模式:使用 {method} 演算法...\n"
29
- yield config_summary, logs
30
- for epoch in range(1, 4):
31
- time.sleep(1)
32
- logs += f" - Epoch {epoch}/3: Loss 降低中...\n"
33
- yield config_summary, logs
34
- logs += "> [OK] 模型訓練完畢並已儲存。\n"
35
- yield config_summary, logs
36
 
37
- logs += "\n>>> 任務全部執行成功!"
38
- yield config_summary, logs
 
 
39
 
40
-
41
  def update_setup(is_checked):
42
  # 根據 Checkbox 的布林值決定 Radio 是否顯示
43
  return gr.update(visible=is_checked)
@@ -92,21 +75,20 @@ with gr.Blocks() as demo:
92
  outputs = train_method
93
  )
94
 
95
- # --- 兩個輸出欄位 ---
96
- config_display = gr.Textbox(label="目前設定參數", lines=3) # 第一個:顯示設定
97
- terminal_logs = gr.Textbox(
98
- label="終端機運行狀態",
99
- lines=10,
100
- interactive=False
101
- ) # 第二個:模擬終端機
102
 
103
- submit_btn = gr.Button("提交", variant="primary")
104
 
105
- # 提交按鈕的邏輯
106
- submit_btn.click(
107
  fn = run_setup,
108
  inputs = [ticker_list, UPDATE_DATA, update_input, TRAIN, train_method],
109
- outputs = [config_display, terminal_logs] # 這裡依序對應 yield 的回傳值
110
  )
111
 
112
  demo.launch()
 
3
  import time
4
 
5
 
6
+
7
  def run_setup(tickersymbol, download_data, download_update, use_train, method):
8
+ # --- 產生設定參數摘要 (顯示在第一個 Textbox) ---
9
  config_summary = f"股票代碼:{tickersymbol}\n"
10
  config_summary += f"更新資料:{'是' if download_data else '否'} (日期: {download_update})\n"
11
  config_summary += f"啟用訓練:{'是' if use_train else '否'} (方式: {method})"
12
+ yield config_summary
 
 
 
 
 
 
 
13
 
14
+ # 模擬執行指令並逐行獲取輸出
15
+ process = subprocess.Popen(
16
+ command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
17
+ )
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ full_output = ""
20
+ for line in process.stdout:
21
+ full_output += line
22
+ yield full_output # 即時回傳目前的累積輸出
23
 
 
24
  def update_setup(is_checked):
25
  # 根據 Checkbox 的布林值決定 Radio 是否顯示
26
  return gr.update(visible=is_checked)
 
75
  outputs = train_method
76
  )
77
 
78
+ # --- 兩個輸出欄位 ---
79
+ # 一:顯示設定
80
+ config_display = gr.Textbox(label="目前設定參數", lines=3)
81
+ # 二:終端機
82
+ gr.Markdown("### Gradio 終端機模擬器")
83
+ output_log = gr.Textbox(label="輸出日誌", lines=20, interactive=False)
 
84
 
85
+ run_btn = gr.Button("執行", variant="primary")
86
 
87
+ # 提交按鈕的邏輯
88
+ run_btn.click(
89
  fn = run_setup,
90
  inputs = [ticker_list, UPDATE_DATA, update_input, TRAIN, train_method],
91
+ outputs = [config_display, output_log] # 這裡依序對應 yield 的回傳值
92
  )
93
 
94
  demo.launch()