Spaces:
Paused
Paused
Commit
·
939dab1
1
Parent(s):
a94d7ef
Update app.py
Browse files
app.py
CHANGED
@@ -13,89 +13,77 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
13 |
import openai
|
14 |
|
15 |
def proper_query(query):
|
16 |
-
prompt = f"
|
17 |
-
response = openai.Completion.create(
|
|
|
18 |
return response.choices[0].text
|
19 |
-
|
20 |
-
def extract_text_from_pdf(file_path, splitter = "\n\n"):
|
21 |
-
with open(file_path, 'rb') as file:
|
22 |
-
pdf = PyPDF2.PdfReader(file)
|
23 |
-
text = ''
|
24 |
-
for page in pdf.pages:
|
25 |
-
text += page.extract_text()
|
26 |
-
chunks = text.split(splitter)
|
27 |
-
chunks = [splitter + chunk for chunk in chunks[1:]]
|
28 |
-
#create a csv file with the chunks in one column
|
29 |
-
#df = pd.DataFrame(chunks, columns=['text'])
|
30 |
-
#write to csv
|
31 |
-
#df.to_csv(file_path[:-4]+'.csv', index=False)
|
32 |
-
return chunks
|
33 |
|
34 |
embeddings = OpenAIEmbeddings()
|
35 |
-
|
|
|
|
|
36 |
|
37 |
text_splitter = RecursiveCharacterTextSplitter(
|
38 |
# Set a really small chunk size, just to show.
|
39 |
-
chunk_size =
|
40 |
chunk_overlap = 0,
|
41 |
length_function = len,
|
42 |
)
|
43 |
-
|
44 |
texts = text_splitter.split_text(text)
|
45 |
|
46 |
docsearch = FAISS.from_texts(texts, embeddings)
|
47 |
|
48 |
-
def
|
49 |
query = proper_query(query)
|
50 |
docs = docsearch.similarity_search(query)
|
51 |
refine_prompt_template = (
|
52 |
"The original question is as follows: {question}\n"
|
53 |
-
"We have provided an
|
54 |
-
"You have the opportunity to refine
|
55 |
-
"only if needed,
|
56 |
"------------\n"
|
57 |
"{context_str}\n"
|
58 |
"------------\n"
|
59 |
-
"
|
60 |
-
"
|
|
|
61 |
"Reply in the same language as the question.\n"
|
62 |
-
"If the context is not helpful to answer the question or if it is not a question, then you will refuse to answer.\n"
|
63 |
"Answer:"
|
64 |
)
|
65 |
refine_prompt = PromptTemplate(
|
66 |
input_variables=["question", "existing_answer", "context_str"],
|
67 |
template=refine_prompt_template,
|
68 |
)
|
69 |
-
|
70 |
-
|
71 |
initial_qa_template = (
|
72 |
"Context information is below. \n"
|
73 |
"---------------------\n"
|
74 |
"{context_str}"
|
75 |
"\n---------------------\n"
|
76 |
"Given the context information and not prior knowledge, "
|
77 |
-
"answer the question: {question}\n"
|
78 |
-
"If the context is not helpful to answer the question
|
79 |
)
|
80 |
initial_qa_prompt = PromptTemplate(
|
81 |
input_variables=["context_str", "question"], template=initial_qa_template
|
82 |
)
|
83 |
-
chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine", return_refine_steps=False,
|
84 |
-
|
85 |
ans = chain({"input_documents": docs, "question": query}, return_only_outputs=True)['output_text']
|
86 |
return ans
|
87 |
|
88 |
demo = gr.Interface(
|
89 |
fn=asesor_transito,
|
90 |
inputs=[
|
91 |
-
gr.Textbox(label="
|
92 |
],
|
93 |
-
outputs=[gr.Textbox(label="Respuesta: \
|
94 |
-
title="Asesor de Reglamento de
|
|
|
95 |
examples=[
|
96 |
-
["
|
97 |
-
["
|
98 |
-
["What would happen if I drove under the influence of alcohol?"]
|
99 |
],
|
100 |
)
|
101 |
|
|
|
13 |
import openai
|
14 |
|
15 |
def proper_query(query):
|
16 |
+
prompt = f"The following text is a user's question: {query}\n\nHow should that question be modified so that it uses correct language?\nReturn the question in the same language.\nCorrected Question:"
|
17 |
+
response = openai.Completion.create(
|
18 |
+
engine="text-davinci-003", prompt=prompt, max_tokens=1000, temperature=0.3)
|
19 |
return response.choices[0].text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
embeddings = OpenAIEmbeddings()
|
22 |
+
#transform a column of a csv into a list
|
23 |
+
df = pd.read_csv('reglamento-avianca.csv')
|
24 |
+
text = df['text'].tolist()
|
25 |
|
26 |
text_splitter = RecursiveCharacterTextSplitter(
|
27 |
# Set a really small chunk size, just to show.
|
28 |
+
chunk_size = 1000,
|
29 |
chunk_overlap = 0,
|
30 |
length_function = len,
|
31 |
)
|
|
|
32 |
texts = text_splitter.split_text(text)
|
33 |
|
34 |
docsearch = FAISS.from_texts(texts, embeddings)
|
35 |
|
36 |
+
def asesor_avianca(query):
|
37 |
query = proper_query(query)
|
38 |
docs = docsearch.similarity_search(query)
|
39 |
refine_prompt_template = (
|
40 |
"The original question is as follows: {question}\n"
|
41 |
+
"We have provided an answer: {existing_answer}\n"
|
42 |
+
"You have the opportunity to refine that answer,"
|
43 |
+
"only if needed, with the context below.\n"
|
44 |
"------------\n"
|
45 |
"{context_str}\n"
|
46 |
"------------\n"
|
47 |
+
"Using no prior knowledge, change the answer only if the given context is related to the question.\n"
|
48 |
+
"Translate the answer to easy-to-understand language.\n"
|
49 |
+
"Shorten the answer as much as possible.\n"
|
50 |
"Reply in the same language as the question.\n"
|
|
|
51 |
"Answer:"
|
52 |
)
|
53 |
refine_prompt = PromptTemplate(
|
54 |
input_variables=["question", "existing_answer", "context_str"],
|
55 |
template=refine_prompt_template,
|
56 |
)
|
57 |
+
|
58 |
+
|
59 |
initial_qa_template = (
|
60 |
"Context information is below. \n"
|
61 |
"---------------------\n"
|
62 |
"{context_str}"
|
63 |
"\n---------------------\n"
|
64 |
"Given the context information and not prior knowledge, "
|
65 |
+
"answer the question to the user: {question}\n"
|
66 |
+
"If the context is not helpful to answer the question then refuse to answer the question in the same language."
|
67 |
)
|
68 |
initial_qa_prompt = PromptTemplate(
|
69 |
input_variables=["context_str", "question"], template=initial_qa_template
|
70 |
)
|
71 |
+
chain = load_qa_chain(OpenAI(temperature=0.2), chain_type="refine", return_refine_steps=False,
|
72 |
+
question_prompt=initial_qa_prompt, refine_prompt=refine_prompt)
|
73 |
ans = chain({"input_documents": docs, "question": query}, return_only_outputs=True)['output_text']
|
74 |
return ans
|
75 |
|
76 |
demo = gr.Interface(
|
77 |
fn=asesor_transito,
|
78 |
inputs=[
|
79 |
+
gr.Textbox(label="Pregunta: / Question: ", lines=3,),
|
80 |
],
|
81 |
+
outputs=[gr.Textbox(label="Respuesta: \ Answer: ")],
|
82 |
+
title="Asesor de Reglamento de Avianca",
|
83 |
+
description = "Hola soy tu asesor personal de Avianca, Hexagonito. Pregúntame lo que necesites saber sobre las reglas de tu vuelo. \nHi, I am Hexagonito, your Avianca rules personal assistant, ask me anything about Avianca rules in any language.",
|
84 |
examples=[
|
85 |
+
["qué documentos necesito para viajar?"],
|
86 |
+
["no llegó mi equipaje, qué puedo hacer?"]
|
|
|
87 |
],
|
88 |
)
|
89 |
|