File size: 2,344 Bytes
e04cd14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hydra
from omegaconf import DictConfig
from demo import App

from llm4scilit_gradio_interface import LLM4SciLitChatInterface

def echo(text, history):
    asdf = "asdf"
    values = [f"{x}\n{x*2}" for x in asdf]
    return text, *values


@hydra.main(version_base=None, config_path="../config", config_name="gradio_config")
def main(cfg : DictConfig) -> None:
    cfg.document_parsing['enabled'] = False
        
    app = App(cfg)
    app._bootstrap()

    def wrapped_ask_chat(text, history):
        result = app.ask_chat(text, history)
        sources = [
            f"{x.metadata['paper_title']}\n{x.page_content}"
            for x in result['source_documents']
        ]
        return result['result'], *sources


    LLM4SciLitChatInterface(wrapped_ask_chat, title="LLM4SciLit").launch()
    # LLM4SciLitChatInterface(echo, title="LLM4SciLit").launch()

    # textbox = gr.Textbox(placeholder="Ask a question about scientific literature", lines=2, label="Question", elem_id="textbox")
    # chatbot = gr.Chatbot(label="LLM4SciLit", elem_id="chat")
    # gr.Interface(fn=echo, inputs=[textbox, chatbot], outputs=[chatbot], title="LLM4SciLit").launch()

    # with gr.Blocks() as demo:
    #     chatbot = gr.Chatbot()
    #     msg = gr.Textbox(container=False)
    #     clear = gr.ClearButton([msg, chatbot])

    #     def respond(message, chat_history):
    #         bot_message = "How are you?"
    #         chat_history.append((message, bot_message))
    #         return "", chat_history

    #     msg.submit(respond, [msg, chatbot], [msg, chatbot])



    # with gr.Blocks(title="LLM4SciLit") as demo:
    #     with gr.Row():
    #         with gr.Column(scale=5):
    #             with gr.Row():
    #                 gr.Chatbot(fn=echo)
    #             with gr.Row():
    #                 gr.Button("Submit")

    #         with gr.Column(scale=5):
    #             with gr.Accordion("Retrieved documents"):
    #                 gr.Label("Document 1")

        # webapp = gr.ChatInterface(fn=app.ask_chat, examples=["hello", "hola", "merhaba"], title="LLM4SciLit")
        # webapp = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="LLM4SciLit")
        # demo.launch()
        # webapp.launch(share=True)

if __name__ == "__main__":
    main() # pylint: disable=no-value-for-parameter