Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,48 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import sys
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def construct_index(directory_path):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
| 16 |
def chatbot(input_text):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
-
return response.
|
| 23 |
|
|
|
|
| 24 |
iface = gr.Interface(fn=chatbot,
|
| 25 |
-
inputs=gr.
|
| 26 |
outputs="text",
|
| 27 |
title="Custom-trained AI Chatbot")
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|