brurei commited on
Commit
0259995
1 Parent(s): c5e25b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf8
2
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
3
+ from langchain import OpenAI
4
+ import gradio as gr
5
+ import sys
6
+ import os
7
+
8
+ os.environ["OPENAI_API_KEY"] = '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'
9
+
10
+
11
+ def construct_index(directory_path):
12
+ max_input_size = 4096
13
+ num_outputs = 512
14
+ max_chunk_overlap = 20
15
+ chunk_size_limit = 600
16
+
17
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
18
+
19
+ llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))
20
+
21
+ documents = SimpleDirectoryReader(directory_path).load_data()
22
+
23
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
24
+
25
+ index.save_to_disk('index.json')
26
+
27
+ return index
28
+
29
+
30
+ def chatbot(input_text):
31
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
32
+ response = index.query(input_text, response_mode="compact")
33
+ return response.response
34
+
35
+
36
+ iface = gr.Interface(fn=chatbot,
37
+ inputs=gr.inputs.Textbox(lines=3, label="O quê gostaria de saber?"),
38
+ outputs=gr.inputs.Textbox(lines=3, label="Veja o que encontrei"),
39
+ title="CD2 IA")
40
+
41
+ index = construct_index("docs")
42
+ iface.launch(share=True)