Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,47 @@
|
|
| 1 |
-
from
|
| 2 |
-
import openai
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Set your API key
|
| 6 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
context = "Python is a popular programming language used for various applications."
|
| 18 |
|
| 19 |
-
|
| 20 |
-
question = "What is Python used for?"
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
max_tokens=150
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 1 |
+
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
|
|
|
| 2 |
import os
|
| 3 |
+
from langchain.chat_models import ChatOpenAI
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
| 6 |
+
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
llm = ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo-instruct", engine="gpt-3.5-turbo-instruct", max_tokens=num_outputs)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
|
|
|
|
| 13 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 14 |
|
| 15 |
|
| 16 |
+
def construct_index(directory_path):
|
| 17 |
+
max_input_size = 4096
|
| 18 |
+
num_outputs = 512
|
| 19 |
+
max_chunk_overlap = 20
|
| 20 |
+
chunk_size_limit = 600
|
| 21 |
+
|
| 22 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
| 23 |
+
|
| 24 |
+
llm_predictor = LLMPredictor(llm=llm)
|
| 25 |
|
| 26 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
|
|
|
| 27 |
|
| 28 |
+
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
|
|
|
| 29 |
|
| 30 |
+
index.save_to_disk('index.json')
|
|
|
|
| 31 |
|
| 32 |
+
return index
|
| 33 |
+
|
| 34 |
+
def chatbot(input_text):
|
| 35 |
+
predetermined_text = "- please only answer using information found in the directory 'docs' and nothing else"
|
| 36 |
+
input_text = input_text + predetermined_text
|
| 37 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
| 38 |
+
response = index.query(input_text, response_mode="compact")
|
| 39 |
+
return response.response
|
| 40 |
|
| 41 |
+
iface = gr.Interface(fn=chatbot,
|
| 42 |
+
inputs=gr.components.Textbox(lines=7, label="What do you want to know about the project?"),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Project Knowledge AI")
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
index = construct_index("docs")
|
| 47 |
+
iface.launch()
|