Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,27 +16,41 @@ os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
|
| 16 |
os.environ["LANGCHAIN_API_KEY"] = st.secrets['OPENAI']
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
template = """Responde la pregunta basado unicamente en el siguiente contexto
|
| 42 |
|
|
|
|
| 16 |
os.environ["LANGCHAIN_API_KEY"] = st.secrets['OPENAI']
|
| 17 |
|
| 18 |
|
| 19 |
+
def get_data():
|
| 20 |
+
return st.session_state["BD"].get(None)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def add_data(value: FAISS):
|
| 24 |
+
st.session_state["BD"]= value
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
db = get_data()
|
| 29 |
+
except:
|
| 30 |
+
print("No hay datos previos")
|
| 31 |
+
loader = PyPDFLoader("https://www.sii.cl/normativa_legislacion/circulares/2024/circu3.pdf")
|
| 32 |
+
data = loader.load()
|
| 33 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
|
| 34 |
+
#Transformado a tipo de dato especifico para esto
|
| 35 |
+
docs = text_splitter.split_documents(data) # 'data' holds the text you want to split, split the text into documents using the text splitter.
|
| 36 |
+
|
| 37 |
+
#Modelo QA sentence similarity
|
| 38 |
+
modelPath = 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2' #español
|
| 39 |
+
model_kwargs = {'device':'cpu'} # o cuda
|
| 40 |
+
encode_kwargs = {'normalize_embeddings': False}
|
| 41 |
+
|
| 42 |
+
#Embeddings que transforman a vectores densos multidimensionales las preguntas del SII
|
| 43 |
+
embeddings = HuggingFaceEmbeddings(
|
| 44 |
+
model_name=modelPath, # Ruta a modelo Pre entrenado
|
| 45 |
+
model_kwargs=model_kwargs, # Opciones de configuracion del modelo
|
| 46 |
+
encode_kwargs=encode_kwargs # Opciones de Encoding
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
#DB y retriever
|
| 50 |
+
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.
|
| 51 |
+
add_data(db)
|
| 52 |
+
|
| 53 |
+
retriever = db.as_retriever(search_kwargs={"k": 3})
|
| 54 |
|
| 55 |
template = """Responde la pregunta basado unicamente en el siguiente contexto
|
| 56 |
|