Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import os
|
|
| 9 |
OPENAI_API_KEY = os.getenv('token')
|
| 10 |
|
| 11 |
|
|
|
|
| 12 |
def construct_index(directory_path):
|
| 13 |
max_input_size = 3500
|
| 14 |
num_outputs = 512
|
|
@@ -23,26 +24,13 @@ def construct_index(directory_path):
|
|
| 23 |
|
| 24 |
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
| 25 |
|
| 26 |
-
# Criar um índice invertido
|
| 27 |
-
inverted_index = {}
|
| 28 |
-
for doc_id, doc in enumerate(documents):
|
| 29 |
-
words = doc.split()
|
| 30 |
-
for word in words:
|
| 31 |
-
if word not in inverted_index:
|
| 32 |
-
inverted_index[word] = []
|
| 33 |
-
inverted_index[word].append(doc_id)
|
| 34 |
-
|
| 35 |
-
# Salvar o índice invertido em um arquivo
|
| 36 |
-
with open('inverted_index.json', 'w') as f:
|
| 37 |
-
json.dump(inverted_index, f)
|
| 38 |
-
|
| 39 |
index.save_to_disk('index.json')
|
| 40 |
|
| 41 |
-
return index
|
| 42 |
|
| 43 |
|
| 44 |
def chatbot(input_text):
|
| 45 |
-
index
|
| 46 |
|
| 47 |
# Ler e concatenar os documentos da pasta "docs" como o contexto relevante
|
| 48 |
docs_directory_path = "docs"
|
|
@@ -56,15 +44,7 @@ def chatbot(input_text):
|
|
| 56 |
texto_entrada = f"Dentro do assunto agilidade me responda: {input_text}{contexto} se não for agilidade não responda"
|
| 57 |
print(texto_entrada)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
relevant_docs = set()
|
| 61 |
-
for word in input_text.split():
|
| 62 |
-
if word in inverted_index:
|
| 63 |
-
relevant_docs.update(inverted_index[word])
|
| 64 |
-
relevant_docs = list(relevant_docs)
|
| 65 |
-
|
| 66 |
-
# Consultar o índice GPT-3.5 com os documentos relevantes
|
| 67 |
-
response = index.query(texto_entrada, relevant_docs=relevant_docs, response_mode="compact")
|
| 68 |
return response.response
|
| 69 |
|
| 70 |
|
|
@@ -76,6 +56,10 @@ iface = gr.Interface(fn=chatbot,
|
|
| 76 |
inputs=gr.components.Textbox(lines=7, label="Como podemos te ajudar?"),
|
| 77 |
outputs="text",
|
| 78 |
description=description,
|
| 79 |
-
title="MentorIA Ágil GPT (Beta)"
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
| 9 |
OPENAI_API_KEY = os.getenv('token')
|
| 10 |
|
| 11 |
|
| 12 |
+
|
| 13 |
def construct_index(directory_path):
|
| 14 |
max_input_size = 3500
|
| 15 |
num_outputs = 512
|
|
|
|
| 24 |
|
| 25 |
index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
index.save_to_disk('index.json')
|
| 28 |
|
| 29 |
+
return index
|
| 30 |
|
| 31 |
|
| 32 |
def chatbot(input_text):
|
| 33 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
| 34 |
|
| 35 |
# Ler e concatenar os documentos da pasta "docs" como o contexto relevante
|
| 36 |
docs_directory_path = "docs"
|
|
|
|
| 44 |
texto_entrada = f"Dentro do assunto agilidade me responda: {input_text}{contexto} se não for agilidade não responda"
|
| 45 |
print(texto_entrada)
|
| 46 |
|
| 47 |
+
response = index.query(texto_entrada, response_mode="compact")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return response.response
|
| 49 |
|
| 50 |
|
|
|
|
| 56 |
inputs=gr.components.Textbox(lines=7, label="Como podemos te ajudar?"),
|
| 57 |
outputs="text",
|
| 58 |
description=description,
|
| 59 |
+
title="MentorIA Ágil GPT (Beta)",
|
| 60 |
+
allow_flagging=True,
|
| 61 |
+
flagging_dir='flagged')
|
| 62 |
+
|
| 63 |
|
| 64 |
+
index = construct_index("docs")
|
| 65 |
+
iface.launch(share=False)
|