Spaces:
Runtime error
Runtime error
| from glob import glob | |
| from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper | |
| from langchain.chat_models import ChatOpenAI | |
| import gradio as gr | |
| import sys | |
| import os | |
| OPENAI_API_KEY = os.getenv('token') | |
| CACHE_FILE = "contexto_cache3.txt" | |
| def obter_contexto(): | |
| if os.path.exists(CACHE_FILE): | |
| with open(CACHE_FILE, "r") as f: | |
| print("passei aqui") | |
| contexto = f.read().strip() | |
| else: | |
| # Ler e concatenar os documentos da pasta "docs" como o contexto relevante | |
| docs_directory_path = "docs" | |
| documents = "" | |
| for file_path in glob(os.path.join(docs_directory_path, "*.{txt,pdf}")): | |
| with open(file_path, "r") as f: | |
| documents += f.read() + " " | |
| contexto = documents.strip() | |
| # Armazenar o contexto em um arquivo de cache | |
| with open(CACHE_FILE, "w") as f: | |
| print("passei aqui 2") | |
| f.write(contexto) | |
| return contexto | |
| def construct_index(directory_path): | |
| max_input_size = 3072 | |
| num_outputs = 1024 | |
| max_chunk_overlap = 20 | |
| 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.3, model_name="gpt-3.5-turbo", max_tokens=num_outputs)) | |
| documents = SimpleDirectoryReader(directory_path).load_data() | |
| index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper) | |
| index.save_to_disk('index.json') | |
| print("passei aqui 3") | |
| return index | |
| def chatbot(input_text): | |
| contexto = obter_contexto() | |
| index = GPTSimpleVectorIndex.load_from_disk('index.json') | |
| # Combinar o contexto e a pergunta de entrada | |
| texto_entrada = f"Dentro do assunto agilidade me responda: {input_text}{contexto} se não for agilidade não responda" | |
| print(texto_entrada) | |
| response = index.query(texto_entrada, response_mode="compact") | |
| print(response) | |
| return response.response | |
| description = """ | |
| A IA foi treinada com materiais do Argon Fundamentals e do canal do Youtube do nosso treinador André Gomes para responder perguntas sobre agilidade. Faça sua pergunta! | |
| """ | |
| iface = gr.Interface(fn=chatbot, | |
| inputs=gr.components.Textbox(lines=7, label="Como podemos te ajudar?"), | |
| outputs="text", | |
| description=description, | |
| title="André GPT (Beta)" | |
| ) | |
| index = construct_index("docs") | |
| iface.launch(share=False) | |