Spaces:
Sleeping
Sleeping
Commit
·
377da30
1
Parent(s):
3bbcfa8
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
|
| 2 |
+
from langchain import OpenAI
|
| 3 |
+
from langchain import LLMChain, PromptTemplate
|
| 4 |
+
from langchain.memory import ConversationBufferMemory
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from IPython.display import Markdown, display
|
| 9 |
+
|
| 10 |
+
def construct_index(directory_path):
|
| 11 |
+
# set maximum input size
|
| 12 |
+
max_input_size = 4096
|
| 13 |
+
# set number of output tokens
|
| 14 |
+
num_outputs = 2000
|
| 15 |
+
# set maximum chunk overlap
|
| 16 |
+
max_chunk_overlap = 20
|
| 17 |
+
# set chunk size limit
|
| 18 |
+
chunk_size_limit = 600
|
| 19 |
+
|
| 20 |
+
# define prompt helper
|
| 21 |
+
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
| 22 |
+
|
| 23 |
+
# define LLM
|
| 24 |
+
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
|
| 25 |
+
|
| 26 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
| 27 |
+
|
| 28 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
| 29 |
+
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context)
|
| 30 |
+
|
| 31 |
+
index.save_to_disk('index.json')
|
| 32 |
+
|
| 33 |
+
return index
|
| 34 |
+
|
| 35 |
+
def ask_ai(query):
|
| 36 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
| 37 |
+
response = index.query(query)
|
| 38 |
+
return f"Response: *{response.response}*"
|
| 39 |
+
|
| 40 |
+
OPENAI_API_KEY="OPENAI_API_KEY"
|
| 41 |
+
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
| 42 |
+
|
| 43 |
+
construct_index("chatbotdata")
|
| 44 |
+
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=ask_ai,
|
| 47 |
+
inputs="text",
|
| 48 |
+
outputs=gr.outputs.Textbox(),
|
| 49 |
+
layout="vertical",
|
| 50 |
+
title="Ask AI",
|
| 51 |
+
description="Ask the AI any question.",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch()
|