File size: 8,349 Bytes
772f8cb
 
 
 
 
 
 
6713a4c
772f8cb
 
 
 
 
 
 
 
9ad1d3a
 
 
 
 
 
772f8cb
 
 
6713a4c
25f7781
 
 
 
772f8cb
 
8a4c9d9
 
772f8cb
8a4c9d9
 
 
 
 
 
 
 
d29770b
8a4c9d9
d29770b
8a4c9d9
 
 
 
 
 
68501d0
8a4c9d9
 
 
772f8cb
 
 
 
 
 
 
 
c145ae6
772f8cb
c145ae6
772f8cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c145ae6
772f8cb
 
 
 
 
 
 
c145ae6
772f8cb
 
c145ae6
25f7781
772f8cb
c145ae6
 
 
772f8cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c145ae6
 
d29770b
c145ae6
 
 
 
 
9e70b10
c145ae6
772f8cb
 
 
 
 
 
 
 
 
 
 
c145ae6
772f8cb
 
 
 
c145ae6
772f8cb
 
 
 
 
c145ae6
772f8cb
c145ae6
772f8cb
 
c145ae6
772f8cb
 
 
 
 
 
 
 
 
 
 
 
 
 
c145ae6
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import warnings
warnings.filterwarnings("ignore")

import os, json
import gradio as gr
import pandas as pd

from backend import Backend

QUESTIONS = [
    "Animal Type",
    "Exposure Age",
    "Behavior Test",
    "Intervention 1",
    "Intervention 2",
    "Genetic Chain",
    "Issues or Challenge Resolved",
    "Innovations in Methodology",
    "Impact of Findings",
    "limitations",
    "Potential Applications",

]

with gr.Blocks(theme="dark") as demo:
    backend = Backend()
    with gr.Row():
            gr.Markdown(f'<center> <h1> <b> DAN_AI </b> </h1> </center>\

                        <center> <h4> Please follow the Instruction <a href="https://huggingface.co/spaces/oliverwang15/DAN_AI/blob/main/README.md">HERE</a> </h4> </center>')
    
    with gr.Row():
        with gr.Row():
            
            # Update
            with gr.Group():
                gr.Markdown(f'<center><h1>Input</h1></center>')
                gr.Markdown(f'<center><p>Please First Upload the File</p></center>')
                
                with gr.Group():
                    gr.Markdown(f'<center><h2>Request Online</h2></center>')
                    openai_key = gr.Textbox(
                        label='Enter your OpenAI API key here', 
                        type='password')

                    model_selection = gr.Radio(choices = ["ChatGPT", "GPT4"], label="Model Selection", info="Please select the model you want to use")

                    file = gr.File(label='Upload your .txt or .pdf file here', file_types=['.txt', '.pdf'], file_count = 'multiple')
                    btn_submit_txt_online = gr.Button(value='Submit')
                    # btn_submit_txt.style(full_width=True)

                with gr.Group():
                    gr.Markdown(f'<center><h2>Or Load Offline</h2></center>')
                    questions = gr.CheckboxGroup(choices = QUESTIONS, value = QUESTIONS, label="Questions", info="Please select the question you want to ask")
                    answer_type = gr.Radio(choices = ["ChatGPT_txt", "GPT4_txt", 'New_GPT_4_pdf', 'Exp_training', 'Exp_Group_A', 'Exp_Group_B'], label="Answer_type", info="Please select the type of answer you want to show")
                    btn_submit_txt_offline = gr.Button(value='Show Answers')
                    # btn_submit_txt.style(full_width=True)

            # Output
            with gr.Group():
                gr.Markdown(f'<center><h1>Output</h1></center>')
                gr.Markdown(f'<center><p>The answer to your question is :</p></center>')
                filename_box = gr.Textbox(label = "File")
                question_box = gr.Textbox(label='Question')
                answer_box = gr.Textbox(label='Answer')
                reference_box = gr.Textbox(label='Reference')

                
                with gr.Group():
                    gr.Markdown("<center><h4>Please select different questions</h4></center>")
                    with gr.Row():
                        btn_last_question = gr.Button(value='Last Question')
                        btn_next_question = gr.Button(value='Next Question')

                with gr.Group():
                    gr.Markdown("<center><h4>Please select different passages</h4></center>")
                    with gr.Row():
                        btn_last_passage = gr.Button(value='Last Passage')
                        btn_next_passage = gr.Button(value='Next Passage')
            
            # Correctness
            with gr.Group():
                gr.Markdown(f'<center><h1>Correct the Result</h1></center>')
                gr.Markdown(f'<center><p>Please Correct the Results</p></center>')

                with gr.Row():
                    save_results = gr.Textbox(placeholder = "Still need to click the button above to save the results", label = 'Save Results')
                with gr.Group():
                    gr.Markdown(f'<center><p>Please Choose: </p></center>')
                    answer_correct = gr.Radio(choices = ["Correct", "Incorrect"], label='Is the Generated Answer Correct?', info="Pease select whether the generated text is correct")
                    correct_answer = gr.Textbox(placeholder = "Please judge on the generated answer", label = 'Correct Answer', interactive = True)
        
                    reference_correct = gr.Radio(choices = ["Correct", "Incorrect"], label="Is the Reference Correct?", info="Pease select whether the reference is correct")
                    correct_reference = gr.Textbox(placeholder = "Please judge on the generated answer", label = 'Correct Reference', interactive = True)
                
                    btn_submit_correctness = gr.Button(value='Submit Correctness')
                    # btn_submit_correctness.style(full_width=True)
            
            # Download
            with gr.Group():
                gr.Markdown(f'<center><h1>Download</h1></center>')
                gr.Markdown(f'<center><p>Download the original LLM answers and corrected LLM answers</p></center>')
                answer_file = gr.File(label='Download original LLM answers', file_types=['.xlsx'])
                btn_download_answer = gr.Button(value='Download original LLM answers')
                # btn_download_answer.style(full_width=True)
                corrected_file = gr.File(label='Download corrected data', file_types=['.xlsx'])
                btn_download_corrected = gr.Button(value='Download corrected LLM answers')
                # btn_download_corrected.style(full_width=True)
                   
    with gr.Row():
        highlighted_text = gr.HTML(label="Highlighted Text")
        # reset = gr.Button(value='Reset')
        # reset.style(full_width=True)

    # Answer change
    answer_correct.input(
        backend.change_correct_answer,
        inputs = [answer_correct],
        outputs = [correct_answer],
    )

    reference_correct.input(
        backend.change_correct_reference,
        inputs = [reference_correct],
        outputs = [correct_reference],
    )
    
    # Submit button
    btn_submit_txt_online.click(  
            backend.process_file_online,
            inputs=[file, questions, openai_key, model_selection],
            outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference],
    )

    btn_submit_txt_offline.click(
            backend.process_file_offline,
            inputs=[questions, answer_type],
            outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference],
        )
    
    btn_submit_correctness.click(   # TODO
            backend.process_results,
            inputs=[answer_correct, correct_answer, reference_correct, correct_reference],
            outputs=[save_results],
        )
    
    # Switch question button
    btn_last_question.click(
        backend.process_last,
        outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
    )

    btn_next_question.click(
            backend.process_next,
            outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
        )
    
    # Switch passwage button
    btn_last_passage.click(
        backend.switch_last_passage,
        outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
    )

    btn_next_passage.click(
        backend.switch_next_passage,
        outputs=[filename_box, question_box, answer_box, reference_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
    )

    # Download button
    btn_download_answer.click(
            backend.download_answer,
            outputs=[answer_file],
        )
    
    btn_download_corrected.click(
            backend.download_corrected,
            outputs=[corrected_file],
        )

demo.queue()
demo.launch(show_error=True)