Spaces:
Sleeping
Sleeping
File size: 1,786 Bytes
14ecdf1 e217897 14ecdf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
from llama_index import SimpleDirectoryReader, GPTListIndex, GPTVectorStoreIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
from llama_index import StorageContext, load_index_from_storage
os.environ["OPENAI_API_KEY"] = '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'
def construct_index(base_directory):
full_path = os.path.join(base_directory, "docs")
max_input_size = 40960
num_outputs = 5120
max_chunk_overlap = 0.4
chunk_size_limit = 600
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.8, model_name="gpt-3.5-turbo-0613", max_tokens=num_outputs))
documents = SimpleDirectoryReader(full_path).load_data()
index = GPTVectorStoreIndex.from_documents(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.storage_context.persist()
#print(index)
#index.save_to_disk('index.json')
return index
def chatbot(input_text):
#index = GPTVectorStoreIndex.load_from_disk('index.json')
# rebuild storage context
# load index
storage_context = StorageContext.from_defaults(persist_dir='./storage')##########
index = load_index_from_storage(storage_context)######################
query_engine = index.as_query_engine()
response = query_engine.query(input_text)
return response.response
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="MCQ AI Chatbot")
#index = construct_index("docs")
index = construct_index("")
iface.launch(share=True) |