Saurabh46 commited on
Commit
90b9c26
·
1 Parent(s): 9702345

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -1,37 +1,47 @@
1
  from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext, StorageContext, load_index_from_storage
2
- from langchain.chat_models import ChatOpenAI
3
- import gradio as gr
4
- import sys
5
  import os
6
 
7
  os.environ["OPENAI_API_KEY"] = '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'
8
 
9
  def construct_index(directory_path):
10
- max_input_size = 4096
11
- num_outputs = 512
12
- max_chunk_overlap = 20
13
- chunk_size_limit = 600
14
 
15
- llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-4", max_tokens=num_outputs))
16
 
17
- documents = SimpleDirectoryReader(directory_path).load_data()
18
 
19
- index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
20
 
21
- index.save_to_disk('index.json')
 
 
 
22
 
23
  return index
24
 
25
  def chatbot(input_text):
26
- index = GPTSimpleVectorIndex.load_from_disk('index.json')
27
- response = index.query(input_text, response_mode="compact")
 
 
 
 
 
 
 
 
28
  return response.response
29
 
30
- iface = gr.Interface(fn=chatbot,
31
- inputs=gr.components.Textbox(lines=7, label="Enter your text"),
32
- outputs="text",
33
- title="My Custom-trained AI Chatbot")
 
34
 
 
35
  index = construct_index("trainingData")
36
- iface.launch()
37
 
 
 
 
1
  from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext, StorageContext, load_index_from_storage
2
+ from langchain import OpenAI
3
+ import gradio
 
4
  import os
5
 
6
  os.environ["OPENAI_API_KEY"] = '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'
7
 
8
  def construct_index(directory_path):
9
+ num_outputs = 256
 
 
 
10
 
11
+ _llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="gpt-4", max_tokens=num_outputs))
12
 
13
+ service_context = ServiceContext.from_defaults(llm_predictor=_llm_predictor)
14
 
15
+ docs = SimpleDirectoryReader(directory_path).load_data()
16
 
17
+ index = GPTVectorStoreIndex.from_documents(docs, service_context=service_context)
18
+
19
+ #Directory in which the indexes will be stored
20
+ index.storage_context.persist(persist_dir="indexes")
21
 
22
  return index
23
 
24
  def chatbot(input_text):
25
+
26
+ # rebuild storage context
27
+ storage_context = StorageContext.from_defaults(persist_dir="indexes")
28
+
29
+ #load indexes from directory using storage_context
30
+ query_engne = load_index_from_storage(storage_context).as_query_engine()
31
+
32
+ response = query_engne.query(input_text)
33
+
34
+ #returning the response
35
  return response.response
36
 
37
+ #Creating the web UIusing gradio
38
+ iface = gradio.Interface(fn=chatbot,
39
+ inputs=gradio.inputs.Textbox(lines=4, label="Enter your question here"),
40
+ outputs=gradio.outputs.Textbox(label="Generated Text"),
41
+ title="Custom-trained AI Chatbot")
42
 
43
+ #Constructing indexes based on the documents in trainingData folder
44
  index = construct_index("trainingData")
 
45
 
46
+ #launching the web UI using gradio
47
+ iface.launch()