Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,83 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
|
|
|
|
|
|
| 5 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from langchain import hub
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
max_new_tokens=512,
|
| 20 |
-
temperature=0.7
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
embedding_function=embedding_function
|
| 35 |
-
)
|
| 36 |
|
| 37 |
-
# RAG
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
prompt = hub.pull("rlm/rag-prompt")
|
| 41 |
-
rag_chain = prompt | llm | parser
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
context = []
|
| 44 |
for doc, score in docs:
|
| 45 |
if score < 7:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if context:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
else:
|
| 53 |
-
return "No tengo informaci贸n
|
| 54 |
-
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 6 |
+
from langchain_community.vectorstores import Chroma
|
| 7 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 8 |
+
from langchain.document_loaders import PyPDFLoader
|
| 9 |
+
import requests
|
| 10 |
+
from rerankers import Reranker
|
| 11 |
+
from langchain_community.chat_models import ChatOllama
|
| 12 |
from langchain import hub
|
| 13 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 14 |
|
| 15 |
+
# Paso 1: Instalar ollama
|
| 16 |
+
subprocess.run("curl -fsSL https://ollama.com/install.sh | sh", shell=True, check=True)
|
| 17 |
+
subprocess.run("ollama serve &", shell=True, check=True)
|
| 18 |
+
subprocess.run("ollama pull llama3.2:1b", shell=True, check=True)
|
| 19 |
|
| 20 |
+
# Paso 2: Descargar el documento PDF
|
| 21 |
+
URL = "https://gruposdetrabajo.sefh.es/gefp/images/stories/documentos/4-ATENCION-FARMACEUTICA/Nutricion/Manual_basico_N_clinica_y_Dietetica_Valencia_2012.pdf"
|
| 22 |
+
response = requests.get(URL)
|
| 23 |
+
with open("Manual_de_nutrici贸n_clinica.pdf", "wb") as f:
|
| 24 |
+
f.write(response.content)
|
| 25 |
|
| 26 |
+
# Paso 3: Inicializar el modelo y los embeddings
|
| 27 |
+
local_llm = "llama3.2:1b"
|
| 28 |
+
llm = ChatOllama(model=local_llm, temperature=0, top_k=50, top_p=0.95)
|
| 29 |
+
chain = llm | StrOutputParser()
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Cargar y procesar el PDF
|
| 32 |
+
loader = PyPDFLoader("Manual_de_nutrici贸n_clinica.pdf")
|
| 33 |
+
documents = loader.load()
|
| 34 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
|
| 35 |
+
all_splits = text_splitter.split_documents(documents)
|
| 36 |
|
| 37 |
+
# Crear embeddings y la base de datos vectorial
|
| 38 |
+
model_name = "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
| 39 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name)
|
| 40 |
+
vectordb = Chroma.from_documents(documents=all_splits, embedding=embeddings, persist_directory="chroma_db")
|
|
|
|
| 41 |
|
| 42 |
+
# Inicializar el reranker
|
| 43 |
+
ranker = Reranker("answerdotai/answerai-colbert-small-v1", model_type='colbert')
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Paso 4: Definir la funci贸n RAG con reranking
|
| 46 |
+
def format_docs(docs):
|
| 47 |
+
return "\n\n".join(doc[0].page_content for doc in docs)
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def test_rag_reranking(query, ranker):
|
| 50 |
+
docs = vectordb.similarity_search_with_score(query)
|
| 51 |
+
prompt = hub.pull("rlm/rag-prompt")
|
| 52 |
+
rag_chain = prompt | llm | StrOutputParser()
|
| 53 |
context = []
|
| 54 |
for doc, score in docs:
|
| 55 |
if score < 7:
|
| 56 |
+
doc_details = doc.to_json()['kwargs']
|
| 57 |
+
context.append(doc_details['page_content'])
|
| 58 |
+
if len(context) > 0:
|
| 59 |
+
# Aplicar reranking
|
| 60 |
+
ranking = ranker.rank(query=query, docs=context)
|
| 61 |
+
# Tomar el contexto m谩s relevante
|
| 62 |
+
useful_context = ranking[0].text
|
| 63 |
+
# Generar la respuesta
|
| 64 |
+
generation = rag_chain.invoke({"context": useful_context, "question": query})
|
| 65 |
+
return generation
|
| 66 |
else:
|
| 67 |
+
return "No tengo informaci贸n para responder a esta pregunta"
|
| 68 |
+
|
| 69 |
+
# Paso 5: Crear una interfaz con Gradio
|
| 70 |
+
def answer_query(query):
|
| 71 |
+
return test_rag_reranking(query, ranker)
|
| 72 |
+
|
| 73 |
+
interface = gr.Interface(
|
| 74 |
+
fn=answer_query,
|
| 75 |
+
inputs=gr.Textbox(label="Ingresa tu pregunta sobre nutrici贸n:"),
|
| 76 |
+
outputs=gr.Textbox(label="Respuesta:"),
|
| 77 |
+
title="Respuesta a Preguntas sobre Nutrici贸n",
|
| 78 |
+
description="Haz preguntas sobre nutrici贸n basadas en el Manual B谩sico de Nutrici贸n Cl铆nica y Diet茅tica (Valencia, 2012)."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Lanzar la interfaz
|
| 82 |
+
interface.launch()
|
| 83 |
|