demogal / app.py
dtrejopizzo's picture
Update app.py
721ab63
raw
history blame
1.47 kB
from llama_index import SimpleDirectoryReader, LLMPredictor, PromptHelper, StorageContext, ServiceContext, GPTVectorStoreIndex, load_index_from_storage
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os
#from langchain.chat_models import ChatOpenAI
os.environ["OPENAI_API_KEY"]
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 0.2
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=1.0, model_name="gpt-4", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTVectorStoreIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
#index.save_to_disk('index.json')
index.storage_context.persist(persist_dir="index.json")
return index
def chatbot(input_text):
#query_engine = index.as_query_engine()
index = GPTVectorStoreIndex.load_from_disk('index.json')
response = index.query(input_text, response_mode="compact")
return response.response
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Ingrese su pregunta"),
outputs="text",
title="Demo Galicia")
index = construct_index("docs")
iface.launch(share=True, debug=True)