Leiyan525 commited on
Commit
9fced2b
·
1 Parent(s): c64eb1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -5
app.py CHANGED
@@ -1,14 +1,90 @@
1
  import openai
2
  from llama_index import SimpleDirectoryReader, VectorStoreIndex
 
3
 
4
- openai.api_key = openai_apikey
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def up_file(files):
8
  Doc_text_list = []
9
  for file in emunerate(files):
10
  print(file.name)
11
- receipts_index = VectorStoreIndex.from_documents(file)
12
- query_engine = receipts_index.as_query_engine()
13
- receipts_response = query_engine.query(message)
14
- return receipts_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import openai
2
  from llama_index import SimpleDirectoryReader, VectorStoreIndex
3
+ from cnocr import CnOcr
4
 
5
+ ocr = CnOcr()
6
+ history_max_len = 500
7
+ all_max_len = 2000
8
 
9
+ def get_embeddings_from_texts(openai_apikey,text):
10
+ openai.api_key = openai_apikey
11
+ response = openai.Embedding.create(
12
+ input = text,
13
+ model = "text-embedding-ada-002"
14
+
15
+ )
16
+ return reponse['data'][0]['embedding']
17
+
18
+ def doc_index(txt, openai_apikey):
19
+
20
+ path = str(time.time())
21
+ import os
22
+ os.mkdir(path)
23
+ with open(path + '/doc.txt', mode = 'w', encoding = 'utf-8') as f:
24
+ f.write(txt)
25
+ openai.api_key = openai_apikey
26
+ documents = SimpleDirectoryReader(path).load_data()
27
+ index = GPTVectorStoreIndex.from_documents(documents)
28
+ template = (
29
+ "你是一个有用的助手,能够根据图片内容准确地回答问题,并且尽量做到简洁明了:\n"
30
+ "\n"
31
+ "{context_str}\n"
32
+ "\n"
33
+ "{query_str}\n"
34
+ "请你回复用户。\n"
35
+ )
36
+ qa_template = Prompt(template)
37
+ query_engine = index.as_query_engine(text_qa_template = qa_template)
38
+ return query_engine, gr.Textbox.update(visible = True), gr.Button.update(visible = True), gr.Markdown.update(
39
+ value = '''建立索引成功,可以开始对话咯'''), gr.Chatbot.update(visible = True),0
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+ def get_response(openai_apikey, msg, bot, query_engine):
48
+ openai.api_key = openai_apikey
49
+
50
+ query_str = '历史对话如下:\n'
51
+ for his in bot:
52
+ query_str += '用户:' + his[0] + '\n'
53
+ query_str += 'bot:' + his[1] + '\n'
54
+ query_str += '用户:' + msg + '\n'
55
+ res = query_engine.query(query_str)
56
+ print(res)
57
+ bot.append([msg, str(res)])
58
+ return bot[max(0, len(bot) - 3):]
59
+
60
 
61
  def up_file(files):
62
  Doc_text_list = []
63
  for file in emunerate(files):
64
  print(file.name)
65
+ return gr.Textbox.update(visible = True), gr.Button.update(visible = True), gr.Markdown.update(value = '点击“建立索引”开始对话',)
66
+
67
+
68
+ with gr.Blocks() as demo:
69
+ with gr.Row():
70
+ with gr.Column():
71
+ openai_apikey = gr.Textbox(label = 'openai api key', placeholder = '输入你的openai apikey')
72
+ file = gr.File(file_types = ['.jpg'], label = '点击上传图片,格式需为jpg', file_count = 'multiple')
73
+ txt = gr.Textbox(label = '图片解析结果', visible = False)
74
+ with gr.Row():
75
+ index_bu = gr.Button(value = '点击建立索引', visible = False)
76
+ query_engine = gr.State([])
77
+ with gr.Column():
78
+ md = gr.Markdown('点击左侧区域上传图片进行解析')
79
+ chat_bot = gr.Chatbot(visible = False)
80
+ msg_txt = gr.Textbox(value = '消息框', placeholder = '输入信息发送', visible = False)
81
+ chat_bu = gr.Button(visible = False)
82
+
83
+ file.change(up_file, [file], [txt, index_bu, md])
84
+ index_bu.click(doc_index, [openai_apikey, txt],[query_engine, msg_txt, chat_bu, md, chat_bot])
85
+ chat_bu.click(get_response, [openai_apikey, msg_txt, chat_bot, query_engine], [chat_bot])
86
+
87
+
88
+ if __name__ == "__main__":
89
+ demo.queue().launch()
90
+