Karan156 commited on
Commit
7956b8f
·
1 Parent(s): 527c4e0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
2
+ from langchain.chat_models import ChatOpenAI
3
+ import gradio as gr
4
+ import sys
5
+ import os
6
+ from gradio_client import Client
7
+
8
+
9
+
10
+ os.environ["OPENAI_API_KEY"] = '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'
11
+
12
+ def construct_index(directory_path):
13
+ max_input_size = 4096
14
+ num_outputs = 512
15
+ max_chunk_overlap = 20
16
+ chunk_size_limit = 600
17
+
18
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
19
+
20
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
21
+
22
+ documents = SimpleDirectoryReader(directory_path).load_data()
23
+
24
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
25
+
26
+ index.save_to_disk('index.json')
27
+
28
+ return index
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
+ iface = gr.Interface(fn=chatbot,
36
+ inputs=gr.components.Textbox(lines=7, label="Enter your text"),
37
+ outputs="text",
38
+ title="Custom-trained AI Chatbot")
39
+
40
+ index = construct_index("docs")
41
+ iface.launch(share=True)