Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -43,6 +43,20 @@ def calculate(course_input, course_profile, course_hour):
|
|
43 |
except requests.exceptions.RequestException as e:
|
44 |
return f"請求失敗: {str(e)}", "無職能項目", "無職能項目"
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def setup_gradio_interface():
|
47 |
with gr.Blocks() as demo:
|
48 |
with gr.Row():
|
@@ -53,28 +67,27 @@ def setup_gradio_interface():
|
|
53 |
with gr.Row():
|
54 |
submit_button = gr.Button("計算職能項目")
|
55 |
with gr.Row():
|
56 |
-
txt_response = gr.Textbox(label="計算狀態", placeholder="計算結果")
|
57 |
-
course_competencies = gr.Textbox(label="職能項目", placeholder="職能項目")
|
58 |
with gr.Row():
|
59 |
-
json_competencies = gr.Textbox(label="JSON內容", placeholder="職能項目")
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
submit_button.click(
|
62 |
-
|
63 |
inputs=[course_input, course_profile, course_hour],
|
64 |
outputs=[txt_response, course_competencies, json_competencies]
|
65 |
)
|
66 |
|
67 |
return demo
|
68 |
|
69 |
-
|
70 |
-
import gradio as gr
|
71 |
-
except ImportError:
|
72 |
-
import sys
|
73 |
-
import gradio as gr
|
74 |
-
|
75 |
-
# Run the interface
|
76 |
if __name__ == "__main__":
|
77 |
demo = setup_gradio_interface()
|
78 |
-
#port = int(os.environ.get("PORT", 7860))
|
79 |
demo.launch()
|
80 |
-
#demo.launch(server_name="0.0.0.0", server_port=port)
|
|
|
43 |
except requests.exceptions.RequestException as e:
|
44 |
return f"請求失敗: {str(e)}", "無職能項目", "無職能項目"
|
45 |
|
46 |
+
# 驗證輸入欄位是否為空
|
47 |
+
def validate_input(course_input, course_profile, course_hour):
|
48 |
+
errors = []
|
49 |
+
if not course_input.strip():
|
50 |
+
errors.append("課程名稱為必填項目")
|
51 |
+
if not course_profile.strip():
|
52 |
+
errors.append("課程簡介為必填項目")
|
53 |
+
if not course_hour.strip():
|
54 |
+
errors.append("課程時數為必填項目")
|
55 |
+
|
56 |
+
if errors:
|
57 |
+
return "\n".join(errors), "", "" # 直接回傳錯誤訊息,阻止請求
|
58 |
+
return None # 表示輸入有效,允許繼續執行
|
59 |
+
|
60 |
def setup_gradio_interface():
|
61 |
with gr.Blocks() as demo:
|
62 |
with gr.Row():
|
|
|
67 |
with gr.Row():
|
68 |
submit_button = gr.Button("計算職能項目")
|
69 |
with gr.Row():
|
70 |
+
txt_response = gr.Textbox(label="計算狀態", placeholder="計算結果", interactive=False)
|
71 |
+
course_competencies = gr.Textbox(label="職能項目", placeholder="職能項目", interactive=False)
|
72 |
with gr.Row():
|
73 |
+
json_competencies = gr.Textbox(label="JSON內容", placeholder="職能項目", interactive=False)
|
74 |
+
|
75 |
+
# 先驗證輸入,再進行計算
|
76 |
+
def validate_and_calculate(course_input, course_profile, course_hour):
|
77 |
+
validation_result = validate_input(course_input, course_profile, course_hour)
|
78 |
+
if validation_result:
|
79 |
+
return validation_result # 若輸入無效,回傳錯誤訊息
|
80 |
+
return calculate(course_input, course_profile, course_hour) # 若有效則繼續計算
|
81 |
+
|
82 |
submit_button.click(
|
83 |
+
validate_and_calculate,
|
84 |
inputs=[course_input, course_profile, course_hour],
|
85 |
outputs=[txt_response, course_competencies, json_competencies]
|
86 |
)
|
87 |
|
88 |
return demo
|
89 |
|
90 |
+
# 運行 Gradio 介面
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
if __name__ == "__main__":
|
92 |
demo = setup_gradio_interface()
|
|
|
93 |
demo.launch()
|
|