Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,46 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
| 3 |
-
from langchain_community.chat_models import ChatHuggingFace
|
| 4 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
|
|
|
| 5 |
from langchain import hub
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
embedding_function = HuggingFaceEmbeddings(
|
| 10 |
model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
| 11 |
model_kwargs={"device": "cpu"}
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# Cargar la base de vectores persistida
|
| 15 |
vectordb = Chroma(
|
| 16 |
persist_directory="chroma_db",
|
| 17 |
embedding_function=embedding_function
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
task="text-generation",
|
| 24 |
-
model_kwargs={"temperature": 0.7, "max_new_tokens": 512}
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# Crear la cadena de procesamiento
|
| 28 |
-
parser = StrOutputParser()
|
| 29 |
-
|
| 30 |
def responder_pregunta(query):
|
| 31 |
docs = vectordb.similarity_search_with_score(query, k=5)
|
| 32 |
prompt = hub.pull("rlm/rag-prompt")
|
|
@@ -44,11 +58,13 @@ def responder_pregunta(query):
|
|
| 44 |
else:
|
| 45 |
return "No tengo informaci贸n suficiente para responder a esta pregunta."
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
| 48 |
gr.Interface(
|
| 49 |
fn=responder_pregunta,
|
| 50 |
inputs=gr.Textbox(label="Pregunta sobre nutrici贸n"),
|
| 51 |
outputs="text",
|
| 52 |
title="Sistema RAG sobre Nutrici贸n Cl铆nica",
|
| 53 |
-
description="Haz preguntas sobre el manual cl铆nico procesado con
|
| 54 |
).launch()
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 2 |
+
from langchain_community.llms import HuggingFacePipeline
|
|
|
|
| 3 |
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from langchain_chroma import Chroma
|
| 5 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 6 |
from langchain import hub
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
# ------------------------------
|
| 10 |
+
# MODELO
|
| 11 |
+
# ------------------------------
|
| 12 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 16 |
+
|
| 17 |
+
pipe = pipeline(
|
| 18 |
+
"text-generation",
|
| 19 |
+
model=model,
|
| 20 |
+
tokenizer=tokenizer,
|
| 21 |
+
max_new_tokens=512,
|
| 22 |
+
temperature=0.7
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 26 |
+
parser = StrOutputParser()
|
| 27 |
+
|
| 28 |
+
# ------------------------------
|
| 29 |
+
# EMBEDDINGS + CHROMA
|
| 30 |
+
# ------------------------------
|
| 31 |
embedding_function = HuggingFaceEmbeddings(
|
| 32 |
model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
| 33 |
model_kwargs={"device": "cpu"}
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
vectordb = Chroma(
|
| 37 |
persist_directory="chroma_db",
|
| 38 |
embedding_function=embedding_function
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# ------------------------------
|
| 42 |
+
# FUNCI脫N RAG
|
| 43 |
+
# ------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def responder_pregunta(query):
|
| 45 |
docs = vectordb.similarity_search_with_score(query, k=5)
|
| 46 |
prompt = hub.pull("rlm/rag-prompt")
|
|
|
|
| 58 |
else:
|
| 59 |
return "No tengo informaci贸n suficiente para responder a esta pregunta."
|
| 60 |
|
| 61 |
+
# ------------------------------
|
| 62 |
+
# INTERFAZ GRADIO
|
| 63 |
+
# ------------------------------
|
| 64 |
gr.Interface(
|
| 65 |
fn=responder_pregunta,
|
| 66 |
inputs=gr.Textbox(label="Pregunta sobre nutrici贸n"),
|
| 67 |
outputs="text",
|
| 68 |
title="Sistema RAG sobre Nutrici贸n Cl铆nica",
|
| 69 |
+
description="Haz preguntas sobre el manual cl铆nico procesado con embeddings + Mistral 7B."
|
| 70 |
).launch()
|