Refled commited on
Commit
6bd1823
·
1 Parent(s): f9c5a78

Create app.py

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