File size: 3,074 Bytes
02a0872
 
 
 
8275bff
 
02a0872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8275bff
 
02a0872
 
8275bff
 
02a0872
8275bff
02a0872
 
8275bff
02a0872
 
8275bff
02a0872
 
8275bff
02a0872
8275bff
02a0872
 
 
 
 
 
 
 
 
 
 
 
 
8275bff
 
02a0872
 
8275bff
02a0872
8275bff
02a0872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
import json
import gradio as gr

def calculate(course_input, course_profile, course_hour):
    url = "https://rd-ai-knowledge-navigator.onrender.com/process"

    headers = {
        "Content-Type": "application/json"
    }

    data = {
        "title": course_input,
        "content": course_profile,
        "hour": course_hour
    }

    try:
        response = requests.post(url, json=data, headers=headers, timeout=50)

        if response.status_code == 200:
            try:
                response_json = response.json()
                success = response_json.get("success", False)
                title = response_json.get("title", "未知課程")
                top_competencies = response_json.get("top_competencies", [])
                competencies = response_json.get("competencies", [])

                if success:
                    competencies_list = [f"{item['item']} ({item['score']})" for item in competencies]
                    return (f"課程 '{title}' 職能匹配成功!", top_competencies, "\n".join(competencies_list))
                else:
                    return "匹配失敗,請稍後再試", "無職能項目", "無職能項目"

            except json.JSONDecodeError:
                return "錯誤: 伺服器回應非 JSON 格式", "無職能項目", "無職能項目"

        else:
            return f"錯誤: 伺服器返回 HTTP {response.status_code}", "無職能項目", "無職能項目"

    except requests.exceptions.Timeout:
        return "錯誤: 伺服器回應超時", "無職能項目", "無職能項目"
    except requests.exceptions.RequestException as e:
        return f"請求失敗: {str(e)}", "無職能項目", "無職能項目"

def setup_gradio_interface():
    with gr.Blocks() as demo:
        with gr.Row():
            course_input = gr.Textbox(label="課程名稱", placeholder="請輸入課程名稱")
            course_hour = gr.Textbox(label="課程時數", placeholder="請輸入課程時數")
        with gr.Row():
            course_profile = gr.Textbox(label="課程簡介", placeholder="請輸入課程簡介")
        with gr.Row():
            submit_button = gr.Button("計算職能項目")
        with gr.Row():
            txt_response = gr.Textbox(label="計算狀態", placeholder="計算結果")
            course_competencies = gr.Textbox(label="職能項目", placeholder="職能項目")
        with gr.Row():
            json_competencies = gr.Textbox(label="JSON內容", placeholder="職能項目")
        # 修正 inputs 和 outputs
        submit_button.click(
            calculate,
            inputs=[course_input, course_profile, course_hour],
            outputs=[txt_response, course_competencies, json_competencies]
        )

    return demo

try:
    import gradio as gr
except ImportError:
    import sys
    import gradio as gr

# Run the interface
if __name__ == "__main__":
    demo = setup_gradio_interface()
    #port = int(os.environ.get("PORT", 7860))
    demo.launch()
    #demo.launch(server_name="0.0.0.0", server_port=port)