andreinigo commited on
Commit
88e9c36
·
1 Parent(s): 8fa521c

Upload files

Browse files
Files changed (3) hide show
  1. app.py +97 -0
  2. reglamento-cdmx.csv +0 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import openai
4
+ from langchain.embeddings.openai import OpenAIEmbeddings
5
+ from langchain.text_splitter import CharacterTextSplitter
6
+ from langchain.vectorstores.faiss import FAISS
7
+ from langchain.docstore.document import Document
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain.chains.question_answering import load_qa_chain
10
+ from langchain.llms import OpenAI
11
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
12
+ import gradio as gr
13
+ import os
14
+ import PyPDF2
15
+ import zipfile
16
+
17
+ openai.api_key = "sk-"+os.environ['OPENAI_API_KEY']
18
+ embeddings = OpenAIEmbeddings()
19
+
20
+ df = pd.read_csv('reglamento-cdmx.csv')
21
+ text = df['text'].tolist()
22
+
23
+ text_splitter = RecursiveCharacterTextSplitter(
24
+ # Set a really small chunk size, just to show.
25
+ chunk_size=500,
26
+ chunk_overlap=0,
27
+ length_function=len,
28
+ )
29
+
30
+ texts = text_splitter.split_text(text)
31
+
32
+ docsearch = FAISS.from_texts(texts, embeddings)
33
+
34
+
35
+ def proper_query(query):
36
+ prompt = f"El siguiente texto es una pregunta en español: {query}\n\n¿Cómo debería ser la pregunta para que sea correcta en español?\nPregunta corregida:"
37
+ response = openai.Completion.create(
38
+ engine="text-davinci-003", prompt=prompt, max_tokens=1000, temperature=0.2)
39
+ return response.choices[0].text
40
+
41
+
42
+ def answer_question(query):
43
+ query = proper_query(query)
44
+ docs = docsearch.similarity_search(query)
45
+ refine_prompt_template = (
46
+ "The original question is as follows: {question}\n"
47
+ "We have provided an existing answer: {existing_answer}\n"
48
+ "You have the opportunity to refine the existing answer,"
49
+ "only if needed, with the context below.\n"
50
+ "------------\n"
51
+ "{context_str}\n"
52
+ "------------\n"
53
+ "If that context is not helpful to answer the question, then omit it.\n"
54
+ "Your answer should be correct, and concise.\n"
55
+ "Shorten the answer if possible.\n"
56
+ "Reply in the same language as the question.\n"
57
+ "Answer:"
58
+ )
59
+ refine_prompt = PromptTemplate(
60
+ input_variables=["question", "existing_answer", "context_str"],
61
+ template=refine_prompt_template,
62
+ )
63
+
64
+ initial_qa_template = (
65
+ "Context information is below. \n"
66
+ "---------------------\n"
67
+ "{context_str}"
68
+ "\n---------------------\n"
69
+ "Given the context information and not prior knowledge, "
70
+ "answer the question: {question}\n"
71
+ )
72
+ initial_qa_prompt = PromptTemplate(
73
+ input_variables=["context_str", "question"], template=initial_qa_template
74
+ )
75
+ chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine", return_refine_steps=False,
76
+ question_prompt=initial_qa_prompt, refine_prompt=refine_prompt)
77
+ answer = chain({"input_documents": docs, "question": query},
78
+ return_only_outputs=True)['output_text']
79
+ return answer
80
+
81
+
82
+ demo = gr.Interface(
83
+ fn=answer_question,
84
+ inputs=[
85
+ gr.Textbox(label="Hola, ¿cuál es tu pregunta?", lines=3,),
86
+ ],
87
+ outputs=[gr.Textbox(label="Respuesta: ")],
88
+ title="Asesor de Reglamento de Tránsito CDMX",
89
+ examples=[
90
+ ["me pueden detener por no llevar casco?"],
91
+ ["qué pasa si no tengo licencia de conducir?"],
92
+ ["qué pasa si no tengo placas?"],
93
+ ],
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()
reglamento-cdmx.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ openai==0.25.0
2
+ PyPDF2==3.0.1
3
+ langchain==0.0.68
4
+ zipfile36==0.1.3