File size: 7,542 Bytes
474d806
4d7183d
 
474d806
 
4d7183d
 
 
5b45741
4d7183d
 
 
 
d5d0a9d
 
 
4d7183d
474d806
 
 
 
 
 
 
 
 
 
 
 
5b45741
474d806
d5d0a9d
5b45741
 
4d7183d
b80d291
4d7183d
 
 
 
24e2294
4d7183d
d5d0a9d
 
4d7183d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b80d291
 
5b45741
4d7183d
5b45741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b80d291
 
4d7183d
 
 
 
 
b80d291
d5d0a9d
474d806
 
d5d0a9d
474d806
 
5b45741
474d806
 
 
 
 
 
 
 
d5d0a9d
474d806
5b45741
d5d0a9d
474d806
24e2294
4d7183d
d5d0a9d
4d7183d
 
 
b80d291
4d7183d
d5d0a9d
474d806
d5d0a9d
 
 
 
 
 
 
 
 
 
 
4d7183d
24e2294
241d2a5
4d7183d
0f8c9e4
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
import os
import gradio as gr

from datetime import datetime, timezone

from config import check_openai_api_key
from agent.research_agent import ResearchAgent
from agent.toolkits import english_polishing
from agent import prompts
from statics.style import *


check_openai_api_key()
report_history_buffer = ""
report_history_tasks = []
polish_history_buffer = ""

REPORT_HISTORY_FILE_PATH = "./statics/report_history_buffer.md"


def load_report_history():
    global report_history_buffer
    if os.path.exists(REPORT_HISTORY_FILE_PATH):
        with open(REPORT_HISTORY_FILE_PATH, "r") as f:
            report_history_buffer = f.read()
    else:
        open(REPORT_HISTORY_FILE_PATH, "w").close()
    return report_history_buffer

def run_agent(task, agent_type, report_type, system_prompt, extra_prompt):
    global report_history_tasks
    report_history_tasks.append(task)
    assistant = ResearchAgent(task, agent_type, system_prompt)
    yield from assistant.write_report(report_type, extra_prompt)


with gr.Blocks(theme=gr.themes.Base(),
               title="AI Research Assistant",
               css=css) as demo:
    gr.HTML(top_bar)
    with gr.Tab(label="🔦Report"):
        with gr.Column():
            gr.HTML(report_html)
            report = gr.Markdown(value="  Report will appear here...",
                                          elem_classes="output")
            with gr.Row():
                agent_type = gr.Dropdown(label="# Agent Type", 
                                         value="Default Agent",
                                         interactive=True,
                                         allow_custom_value=False,
                                         choices=["Default Agent", 
                                                 "Business Analyst Agent",
                                                 "Finance Agent",
                                                 "Travel Agent",
                                                 "Academic Research Agent",
                                                 "Computer Security Analyst Agent",
                                                 "Clinical Medicine Agent",
                                                 "Basic Medicine Agent",
                                                 "Social Science Research Agent"])
                report_type = gr.Dropdown(label="# Report Type",
                                         value="Research Report",
                                         interactive=True,
                                         allow_custom_value=False,
                                         choices=["Research Report",
                                                  "Resource Report",
                                                  "Outline Report"])

            input_box = gr.Textbox(label="# What would you like to research next?", placeholder="Enter your question here")
            
            with gr.Accordion("# Advanced Settings", open=False):
                system_prompt = gr.TextArea(label="Agent Prompt", 
                                            value=prompts.generate_agent_role_prompt(agent_type.value),
                                            interactive=True, 
                                            show_copy_button=True)
                report_type_prompt = gr.TextArea(label="Report Prompt (not editable)", 
                                                 value=prompts.generate_report_prompt(f'{input_box.value}', report_type.value), 
                                                 interactive=False, 
                                                 show_copy_button=True)
                extra_prompt = gr.TextArea(label="Extra Prompt", interactive=True, show_copy_button=True)

                def on_select_agent(evt: gr.SelectData):
                    return f"{prompts.generate_agent_role_prompt(evt.value)}"

                def on_select_input_box(input, report_type):
                    return f"{prompts.generate_report_prompt(f'{input}', report_type)}"

                def on_select_report_type(evt: gr.SelectData, input_box):
                    return f"{prompts.generate_report_prompt(f'{input_box}', evt.value)}"

                
                agent_type.select(on_select_agent, None, system_prompt)
                input_box.input(on_select_input_box, inputs=[input_box, report_type], outputs=report_type_prompt)
                report_type.select(on_select_report_type, inputs=[input_box], outputs=report_type_prompt)

            submit_btn = gr.Button("Generate Report", elem_id="primary-btn")

            gr.Examples(["Should I invest in the Large Language Model industry in 2023?", 
                         "Is it advisable to make investments in the electric car industry during the year 2023?",
                         "What constitutes the optimal approach for investing in the Bitcoin industry during the year 2023?",
                         "What are the most recent advancements in the domain of superconductors as of 2023?"], 
                         inputs=input_box)
            
            with gr.Accordion(label="# Report History", elem_id="history", open=False):
                report_history = gr.Markdown(value=load_report_history)

            def store_report(content):
                global report_history_tasks, report_history_buffer
                report_task = report_history_tasks[-1][:min(100, len(report_history_tasks[-1]))]
                time_stamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S %p")
                new_report = f'<details> \
                                   <summary>UTC {time_stamp}: \
                                       <i>{report_task}</i></summary> \
                                   <div id="history_box">{content}</div> \
                               </details>'
                report_history_buffer += new_report
                with open("./statics/report_history_buffer.md", "a+") as f:
                    f.write(new_report)
                return report_history_buffer

            submit_btn.click(run_agent, inputs=[input_box, agent_type, report_type, system_prompt, extra_prompt], outputs=report)\
                      .then(store_report, inputs=[report], outputs=report_history)

    with gr.Tab("✒️English Polishing"):
        gr.HTML(english_polishing_html)
        polished_result = gr.Markdown("&nbsp;&nbsp;Polished result will appear here...", elem_classes="output")
        sentences = gr.Textbox(label="# What would you like to polish?", placeholder="Enter your sentence here")
        
        with gr.Row():
            polish_btn = gr.Button("Polish", elem_id="primary-btn")
        
        with gr.Accordion(label="# Polishing History", elem_id="history", open=False):
            polish_history = gr.Markdown()

        def store_polished_result(origin, result):
            global polish_history_buffer
            polish_history_buffer += f'<details> \
                                           <summary><i>{origin}</i></summary> \
                                           <div id="history_box">{result}</div> \
                                       </details>'
            return polish_history_buffer

        polish_btn.click(english_polishing, inputs=[sentences], outputs=polished_result) \
                  .then(store_polished_result, inputs=[sentences, polished_result], outputs=polish_history)

    with gr.Tab("📑Literature Review"):
        gr.HTML(literature_review_html)

demo.queue().launch()