Update app.py
Browse filesDuplicate and change AI Engine
app.py
CHANGED
@@ -15,8 +15,32 @@ dir_path = "./docs"
|
|
15 |
# Create the directory using the os module
|
16 |
os.makedirs(dir_path, exist_ok=True)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def qa_system(pdf_file, openai_key, prompt, chain_type, k):
|
22 |
os.environ["OPENAI_API_KEY"] = openai_key
|
@@ -47,8 +71,18 @@ def qa_system(pdf_file, openai_key, prompt, chain_type, k):
|
|
47 |
result = qa({"query": prompt})
|
48 |
return result['result'], [doc.page_content for doc in result["source_documents"]]
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# define the Gradio interface
|
51 |
-
input_file = gr.inputs.File(label="PDF File")
|
52 |
openai_key = gr.inputs.Textbox(label="OpenAI API Key", type="password")
|
53 |
prompt = gr.inputs.Textbox(label="Question Prompt")
|
54 |
chain_type = gr.inputs.Radio(['stuff', 'map_reduce', "refine", "map_rerank"], label="Chain Type")
|
@@ -57,7 +91,9 @@ k = gr.inputs.Slider(minimum=1, maximum=5, default=1, label="Number of Relevant
|
|
57 |
output_text = gr.outputs.Textbox(label="Answer")
|
58 |
output_docs = gr.outputs.Textbox(label="Relevant Source Text")
|
59 |
|
60 |
-
gr.Interface(
|
|
|
61 |
title="Question Answering with PDF File and OpenAI",
|
62 |
-
description="
|
|
|
63 |
|
|
|
15 |
# Create the directory using the os module
|
16 |
os.makedirs(dir_path, exist_ok=True)
|
17 |
|
18 |
+
def construct_index(directory_path):
|
19 |
+
max_input_size = 4096
|
20 |
+
num_outputs = 512
|
21 |
+
max_chunk_overlap = 20
|
22 |
+
chunk_size_limit = 600
|
23 |
+
|
24 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
25 |
+
|
26 |
+
#llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.4, model_name="text-davinci-003", max_tokens=num_outputs))
|
27 |
+
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
|
28 |
+
|
29 |
+
|
30 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
31 |
+
|
32 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
33 |
+
|
34 |
+
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) #, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
35 |
+
|
36 |
+
index.save_to_disk('index.json')
|
37 |
+
|
38 |
+
return index
|
39 |
+
|
40 |
+
def chatbot(input_text):
|
41 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
42 |
+
response = index.query(input_text, response_mode="compact")
|
43 |
+
return response.response
|
44 |
|
45 |
def qa_system(pdf_file, openai_key, prompt, chain_type, k):
|
46 |
os.environ["OPENAI_API_KEY"] = openai_key
|
|
|
71 |
result = qa({"query": prompt})
|
72 |
return result['result'], [doc.page_content for doc in result["source_documents"]]
|
73 |
|
74 |
+
# New interface
|
75 |
+
iface = gr.Interface(fn=chatbot,
|
76 |
+
inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
|
77 |
+
outputs="text",
|
78 |
+
title="Tikehau-trained AI Chatbot")
|
79 |
+
|
80 |
+
#index = construct_index("docs")
|
81 |
+
index = construct_index(dir_path)
|
82 |
+
|
83 |
+
|
84 |
# define the Gradio interface
|
85 |
+
# input_file = gr.inputs.File(label="PDF File")
|
86 |
openai_key = gr.inputs.Textbox(label="OpenAI API Key", type="password")
|
87 |
prompt = gr.inputs.Textbox(label="Question Prompt")
|
88 |
chain_type = gr.inputs.Radio(['stuff', 'map_reduce', "refine", "map_rerank"], label="Chain Type")
|
|
|
91 |
output_text = gr.outputs.Textbox(label="Answer")
|
92 |
output_docs = gr.outputs.Textbox(label="Relevant Source Text")
|
93 |
|
94 |
+
gr.Interface(fn=chatbot,
|
95 |
+
inputs=[openai_key, prompt, chain_type, k], outputs=[output_text, output_docs],
|
96 |
title="Question Answering with PDF File and OpenAI",
|
97 |
+
description="Tikehau URDs.").launch(debug = True)
|
98 |
+
|
99 |
|