stephenmccartney1234 commited on
Commit
a9d652e
·
verified ·
1 Parent(s): 0c0581c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -18
app.py CHANGED
@@ -1,30 +1,48 @@
1
- import openai
 
2
  import gradio as gr
3
- import sys
4
  import os
5
 
6
- os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
 
7
 
8
  def construct_index(directory_path):
9
- # Your existing code for index construction remains unchanged
10
- def construct_index(directory_path):
11
- max_input_size = 4096
12
- num_outputs = 512
13
- max_chunk_overlap = 20
14
- chunk_size_limit = 600
 
 
 
 
 
 
 
 
 
 
 
15
 
 
 
16
  def chatbot(input_text):
17
- response = openai.Completion.create(
18
- model="text-davinci-003", # Specify the GPT-3 model you want to use
19
- prompt=input_text,
20
- max_tokens=150 # Adjust the max tokens based on your requirements
21
- )
22
- return response.choices[0].text.strip()
23
 
 
24
  iface = gr.Interface(fn=chatbot,
25
- inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
26
  outputs="text",
27
  title="Custom-trained AI Chatbot")
28
 
29
- # index = construct_index("docs") # You may not need this line anymore
30
- iface.launch(share=True)
 
 
 
 
1
+ from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
2
+ from langchain.chat_models import ChatOpenAI
3
  import gradio as gr
 
4
  import os
5
 
6
+ # Set your OpenAI API key here
7
+ os.environ["OPENAI_API_KEY"] = 'Your Secret API Key'
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
+ # Initialize the prompt helper
16
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
17
+
18
+ # Initialize the LLM predictor with an updated model name if available
19
+ llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-4", max_tokens=num_outputs)) # Updated to GPT-4 or latest available model
20
+
21
+ # Load documents from the directory
22
+ documents = SimpleDirectoryReader(directory_path).load_data()
23
+
24
+ # Create and populate the index
25
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
26
+ index.save_to_disk('index.json')
27
 
28
+ return index
29
+
30
  def chatbot(input_text):
31
+ # Load the index from disk
32
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
33
+
34
+ # Query the index and return the response
35
+ response = index.query(input_text, response_mode="compact")
36
+ return response.response
37
 
38
+ # Set up the Gradio interface
39
  iface = gr.Interface(fn=chatbot,
40
+ inputs=gr.components.Textbox(lines=7, label="Enter your text"),
41
  outputs="text",
42
  title="Custom-trained AI Chatbot")
43
 
44
+ # Construct the index with your documents directory
45
+ index = construct_index("docs")
46
+
47
+ # Launch the Gradio interface
48
+ iface.launch(share=True)