File size: 2,351 Bytes
e0f406c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import core
import openai
import models
import time
import gradio as gr
import os
import asyncio
import time

api_key = os.environ["OPENAI_API_KEY"]
api_base = os.environ["OPENAI_API_BASE"]

def chatbot_initialize():
    retriever = core.retriever.ChromaRetriever(pdf_dir="", 
                                               collection_name="pdfs_1000", 
                                               split_args={"size": 2048, "overlap": 10}, #embedding_model="text-embedding-ada-002"
                                               embed_model=models.BiomedModel()
                                               )
    Chatbot = core.chatbot.RetrievalChatbot(retriever=retriever)
    return Chatbot

async def respond(query, chat_history, img_path_list, chat_history_string):
    time1 = time.time()
    global Chatbot
    result = await Chatbot.response(query, image_paths=img_path_list)
    response = result["answer"]
    logs = result["logs"]
    titles_set = result["titles"]
    titles = "\n".join(list(titles_set))
    chat_history.append((query, response))
    if img_path_list is None:
        chat_history_string += "Query: " + query + "\nImage: None" + "\nResponse: " + response + "\n\n\n"
    else:
        chat_history_string += "Query: " + query + "\nImages: " + "\n".join([path.name for path in img_path_list]) + "\nResponse: " + response + "\n\n\n"
    time2 = time.time()
    print(f"Total: {time2-time1}")
    return "", chat_history, chat_history_string

if __name__ == "__main__":
    global Chatbot
    Chatbot=chatbot_initialize()

    with gr.Blocks() as demo:
        with gr.Row():
            with gr.Column(scale=2):
                chatbot = gr.Chatbot()
                msg = gr.Textbox(label="Query", show_label=True)
                imgs = gr.File(file_count='multiple', file_types=['image'], type="filepath", label='Upload Images')
                clear = gr.ClearButton([msg, chatbot])
            with gr.Column(scale=1):
                # titles = gr.Textbox(label="Referenced Article Titles", show_label=True, show_copy_button=True, interactive=False)
                history = gr.Textbox(label="Copy Chat History", show_label=True, show_copy_button=True, interactive=False, max_lines=5)
            msg.submit(respond, inputs=[msg, chatbot, imgs, history], outputs=[msg, chatbot, history])
    demo.queue().launch()