Waflon commited on
Commit
fcc8b9e
·
verified ·
1 Parent(s): 971e748

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -60
app.py CHANGED
@@ -33,64 +33,57 @@ try:
33
  else:
34
  print("dato default")
35
  except:
36
- print("Pass")
37
-
38
- print("No hay datos previos")
39
- loader = PyPDFLoader("https://www.sii.cl/normativa_legislacion/circulares/2024/circu3.pdf")
40
- data = loader.load()
41
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
42
- #Transformado a tipo de dato especifico para esto
43
- docs = text_splitter.split_documents(data) # 'data' holds the text you want to split, split the text into documents using the text splitter.
44
-
45
- #Modelo QA sentence similarity
46
- modelPath = 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2' #español
47
- model_kwargs = {'device':'cpu'} # o cuda
48
- encode_kwargs = {'normalize_embeddings': False}
49
-
50
- #Embeddings que transforman a vectores densos multidimensionales las preguntas del SII
51
- embeddings = HuggingFaceEmbeddings(
52
- model_name=modelPath, # Ruta a modelo Pre entrenado
53
- model_kwargs=model_kwargs, # Opciones de configuracion del modelo
54
- encode_kwargs=encode_kwargs # Opciones de Encoding
55
- )
56
-
57
- #DB y retriever
58
- db = FAISS.from_documents(docs, embeddings) # Create a retriever object from the 'db' with a search configuration where it retrieves up to 4 relevant splits/documents.
59
- retriever = db.as_retriever(search_kwargs={"k": 3})
60
-
61
- template = """Responde la pregunta basado unicamente en el siguiente contexto
62
-
63
- {contexto}
64
-
65
- Pregunta: {pregunta}
66
-
67
- """
68
-
69
- #LLM
70
- prompt = ChatPromptTemplate.from_template(template)
71
- model = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)
72
-
73
- def format_docs(docs):
74
- return "\n\n".join([d.page_content for d in docs])
75
-
76
- chain = (
77
- {"contexto": retriever | format_docs, "pregunta": RunnablePassthrough()}
78
- | prompt
79
- | model
80
- | StrOutputParser()
81
- )
82
- print(chain)
83
- add_data(chain)
84
 
85
- def main():
86
- pregunta = st.text_area('Ingresa algun texto:')
87
- tmp_button = st.button("CLICK")
88
- if tmp_button: #Esperar al boton
89
- out = chain.invoke(pregunta)
90
- st.write(out)
91
- st.rerun() #Restart app
92
- else:
93
- st.stop()
94
-
95
- if __name__ == "__main__":
96
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  else:
34
  print("dato default")
35
  except:
36
+ loader = PyPDFLoader("https://www.sii.cl/normativa_legislacion/circulares/2024/circu3.pdf")
37
+ data = loader.load()
38
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
39
+ #Transformado a tipo de dato especifico para esto
40
+ docs = text_splitter.split_documents(data) # 'data' holds the text you want to split, split the text into documents using the text splitter.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ #Modelo QA sentence similarity
43
+ modelPath = 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2' #español
44
+ model_kwargs = {'device':'cpu'} # o cuda
45
+ encode_kwargs = {'normalize_embeddings': False}
46
+
47
+ #Embeddings que transforman a vectores densos multidimensionales las preguntas del SII
48
+ embeddings = HuggingFaceEmbeddings(
49
+ model_name=modelPath, # Ruta a modelo Pre entrenado
50
+ model_kwargs=model_kwargs, # Opciones de configuracion del modelo
51
+ encode_kwargs=encode_kwargs # Opciones de Encoding
52
+ )
53
+
54
+ #DB y retriever
55
+ db = FAISS.from_documents(docs, embeddings) # Create a retriever object from the 'db' with a search configuration where it retrieves up to 4 relevant splits/documents.
56
+ retriever = db.as_retriever(search_kwargs={"k": 3})
57
+
58
+ template = """Responde la pregunta basado unicamente en el siguiente contexto
59
+
60
+ {contexto}
61
+
62
+ Pregunta: {pregunta}
63
+
64
+ """
65
+
66
+ #LLM
67
+ prompt = ChatPromptTemplate.from_template(template)
68
+ model = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)
69
+
70
+ def format_docs(docs):
71
+ return "\n\n".join([d.page_content for d in docs])
72
+
73
+ chain = (
74
+ {"contexto": retriever | format_docs, "pregunta": RunnablePassthrough()}
75
+ | prompt
76
+ | model
77
+ | StrOutputParser()
78
+ )
79
+ add_data(chain)
80
+
81
+ pregunta = st.text_area('Ingresa algun texto:')
82
+ tmp_button = st.button("CLICK")
83
+ if tmp_button: #Esperar al boton
84
+ out = chain.invoke(pregunta)
85
+ st.write(out)
86
+ #st.rerun() #Restart app
87
+ else:
88
+ st.stop()
89
+ st.write(st.session_state["chain"])