File size: 3,204 Bytes
532a759
1543ec3
532a759
 
1543ec3
 
532a759
1543ec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532a759
1543ec3
532a759
 
1543ec3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532a759
1543ec3
739dd38
532a759
 
739dd38
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import gradio as gr
from fastapi import FastAPI

from edu_assistant import version
from webui.coding_problem import CodingProblemUI
from webui.qa import QaUI

app = FastAPI()
demo = gr.Blocks(title="Codedog Edu Assistant", theme="gradio/soft")
qa_ui = QaUI()
cp_ui = CodingProblemUI()


def apply_cfg(
    gpt4_flags: list[int],
    qa_instruction: str,
    cp_instruction: str,
    cp_first_question: str,
    qa_knowledge: str,
    cp_knowledge: str,
):
    qa_ui.ui_reload(
        instruction=qa_instruction,
        knowledge_name=qa_knowledge,
        enable_gpt4=0 in gpt4_flags,
    )
    cp_ui.ui_reload(
        instruction=cp_instruction,
        first_question=cp_first_question,
        knowledge_name=cp_knowledge,
        enable_gpt4=1 in gpt4_flags,
    )
    demo.render()
    gr.update()
    gr.Info("更新配置成功")


def default_cfg():
    qa_ui.ui_reload()
    cp_ui.ui_reload()
    demo.render()
    gr.update()
    gr.Info("恢复默认配置成功")


def get_gpt4_flags():
    result = []
    if qa_ui.enable_gpt4:
        result.append("答疑")
    if cp_ui.enable_gpt4:
        result.append("做题")
    return result


with demo:
    with gr.Row():
        gr.Markdown(f"# Codedog Edu Assistant v{version.VERSION}")

    with gr.Tab(label="答疑"):
        qa_ui.ui_render()

    with gr.Tab(label="做题"):
        cp_ui.ui_render()

    with gr.Tab(label="设置"):
        with gr.Row():
            gr.Markdown("## Prompt 设置")
        with gr.Row():
            qa_instruction = gr.Textbox(
                label="答疑指示Prompt", lines=5, max_lines=20, value=qa_ui.get_instruction, interactive=True
            )
        with gr.Row():
            cp_instruction = gr.Textbox(
                label="做题指示Prompt", lines=5, max_lines=20, value=cp_ui.get_instruction, interactive=True
            )
        with gr.Row():
            cp_first_question = gr.Textbox(
                label="判题Prompt", lines=5, max_lines=20, value=cp_ui.get_first_question, interactive=True
            )
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("## Open AI 设置")
            with gr.Column(scale=2):
                gpt4_flags = gr.CheckboxGroup(
                    value=get_gpt4_flags, choices=["答疑", "做题"], label="启用GPT4", type="index", interactive=True
                )

        with gr.Row():
            gr.Markdown("## 知识库设置")
            qa_knowledge = gr.Textbox(value=qa_ui.knowledge, label="答疑知识库", interactive=True)
            cp_knowledge = gr.Textbox(value=cp_ui.knowledge, label="做题知识库", interactive=True)

        with gr.Row():
            default_btn = gr.Button(value="恢复默认配置", interactive=True, scale=1)
            apply_btn = gr.Button(value="更新配置", interactive=True, variant="primary", scale=1)

    default_btn.click(default_cfg, [], [])
    apply_btn.click(
        apply_cfg, [gpt4_flags, qa_instruction, cp_instruction, cp_first_question, qa_knowledge, cp_knowledge], []
    )

demo.queue()
# app = gr.mount_gradio_app(app, demo, path="/")

if __name__ == "__main__":
    demo.launch()
    # uvicorn.run(app, port=7860)