Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def calculate1(course_input, course_profile, course_hour):
|
6 |
+
url = "https://ai-knowledge-navigator.onrender.com/process"
|
7 |
+
|
8 |
+
headers = {
|
9 |
+
"Content-Type": "application/json"
|
10 |
+
}
|
11 |
+
|
12 |
+
data = {
|
13 |
+
"title": course_input,
|
14 |
+
"content": course_profile,
|
15 |
+
"hour": course_hour
|
16 |
+
}
|
17 |
+
|
18 |
+
try:
|
19 |
+
response = requests.post(url, json=data, headers=headers, timeout=50)
|
20 |
+
|
21 |
+
if response.status_code == 200:
|
22 |
+
try:
|
23 |
+
response_json = response.json()
|
24 |
+
# 解析回傳結果
|
25 |
+
success = response_json.get("success", False)
|
26 |
+
title = response_json.get("title", "未知課程")
|
27 |
+
competencies = response_json.get("competencies", ["未能成功匹配"])
|
28 |
+
|
29 |
+
if success:
|
30 |
+
return f"課程 '{title}' 職能匹配成功!", ", ".join(competencies)
|
31 |
+
else:
|
32 |
+
return "匹配失敗,請稍後再試", "無職能項目"
|
33 |
+
|
34 |
+
except json.JSONDecodeError:
|
35 |
+
return "錯誤: 伺服器回應非 JSON 格式", "無職能項目"
|
36 |
+
|
37 |
+
else:
|
38 |
+
return f"錯誤: 伺服器返回 HTTP {response.status_code}", "無職能項目"
|
39 |
+
|
40 |
+
except requests.exceptions.Timeout:
|
41 |
+
return "錯誤: 伺服器回應超時", "無職能項目"
|
42 |
+
except requests.exceptions.RequestException as e:
|
43 |
+
return f"請求失敗: {str(e)}", "無職能項目"
|
44 |
+
|
45 |
+
def setup_gradio_interface():
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
with gr.Row():
|
48 |
+
course_input = gr.Textbox(label="課程名稱", placeholder="請輸入課程名稱")
|
49 |
+
course_hour = gr.Textbox(label="課程時數", placeholder="請輸入課程時數")
|
50 |
+
with gr.Row():
|
51 |
+
course_profile = gr.Textbox(label="課程簡介", placeholder="請輸入課程簡介")
|
52 |
+
with gr.Row():
|
53 |
+
submit_button = gr.Button("計算職能項目")
|
54 |
+
with gr.Row():
|
55 |
+
txt_response = gr.Textbox(label="計算狀態", placeholder="計算結果")
|
56 |
+
course_competencies = gr.Textbox(label="職能項目", placeholder="職能項目")
|
57 |
+
|
58 |
+
# 修正 inputs 和 outputs
|
59 |
+
submit_button.click(
|
60 |
+
calculate1,
|
61 |
+
inputs=[course_input, course_profile, course_hour],
|
62 |
+
outputs=[txt_response, course_competencies]
|
63 |
+
)
|
64 |
+
|
65 |
+
return demo
|
66 |
+
|
67 |
+
try:
|
68 |
+
import gradio as gr
|
69 |
+
except ImportError:
|
70 |
+
import sys
|
71 |
+
import gradio as gr
|
72 |
+
|
73 |
+
# Run the interface
|
74 |
+
if __name__ == "__main__":
|
75 |
+
demo = setup_gradio_interface()
|
76 |
+
#port = int(os.environ.get("PORT", 7860))
|
77 |
+
demo.launch()
|
78 |
+
#demo.launch(server_name="0.0.0.0", server_port=port)
|