File size: 3,209 Bytes
c64eb1f
01a86f9
9fced2b
50f2afb
81d305d
c64eb1f
9fced2b
 
 
c64eb1f
9fced2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c64eb1f
 
 
ff9e37c
c64eb1f
9fced2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931063c
9fced2b
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
import openai
from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, Prompt
from cnocr import CnOcr
import gradio as gr
import time

ocr = CnOcr()
history_max_len = 500
all_max_len = 2000

def get_embeddings_from_texts(openai_apikey,text):
    openai.api_key = openai_apikey
    response = openai.Embedding.create(
        input = text,
        model = "text-embedding-ada-002"
        
    )
    return reponse['data'][0]['embedding']

def doc_index(txt, openai_apikey):

    path = str(time.time())
    import os
    os.mkdir(path)
    with open(path + '/doc.txt', mode = 'w', encoding = 'utf-8') as f:
        f.write(txt)
    openai.api_key = openai_apikey
    documents = SimpleDirectoryReader(path).load_data()
    index = GPTVectorStoreIndex.from_documents(documents)
    template = (
        "你是一个有用的助手,能够根据图片内容准确地回答问题,并且尽量做到简洁明了:\n"
        "\n"
        "{context_str}\n"
        "\n"
        "{query_str}\n"
        "请你回复用户。\n"
    )
    qa_template = Prompt(template)
    query_engine = index.as_query_engine(text_qa_template = qa_template)
    return query_engine, gr.Textbox.update(visible = True), gr.Button.update(visible = True), gr.Markdown.update(
        value = '''建立索引成功,可以开始对话咯'''), gr.Chatbot.update(visible = True),0

    





def get_response(openai_apikey, msg, bot, query_engine):
    openai.api_key = openai_apikey
    
    query_str = '历史对话如下:\n'
    for his in bot:
        query_str += '用户:' + his[0] + '\n'
        query_str += 'bot:' + his[1] + '\n'
    query_str += '用户:' + msg + '\n'
    res = query_engine.query(query_str)
    print(res)
    bot.append([msg, str(res)])
    return bot[max(0, len(bot) - 3):]
        

def up_file(files):
    Doc_text_list = []
    for idx, file in enumerate(files):
        print(file.name)
        return gr.Textbox.update(visible = True), gr.Button.update(visible = True), gr.Markdown.update(value = '点击“建立索引”开始对话',)


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            openai_apikey = gr.Textbox(label = 'openai api key', placeholder = '输入你的openai apikey')
            file = gr.File(file_types = ['.jpg'], label = '点击上传图片,格式需为jpg', file_count = 'multiple')
            txt = gr.Textbox(label = '图片解析结果', visible = False)
            with gr.Row():
                index_bu = gr.Button(value = '点击建立索引', visible = False)
            query_engine = gr.State([])
        with gr.Column():
            md = gr.Markdown('点击左侧区域上传图片进行解析')
            chat_bot = gr.Chatbot(visible = False)
            msg_txt = gr.Textbox(value = '消息框', placeholder = '输入信息发送', visible = False)
            chat_bu = gr.Button(visible = False)

    file.change(up_file, [file], [txt, index_bu, md])
    index_bu.click(doc_index, [openai_apikey, txt],[query_engine, msg_txt, chat_bu, md, chat_bot])
    chat_bu.click(get_response, [openai_apikey, msg_txt, chat_bot, query_engine], [chat_bot])


if __name__ == "__main__":
    demo.queue().launch()