Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,12 +10,12 @@ from langchain.prompts import PromptTemplate
|
|
10 |
from dotenv import load_dotenv
|
11 |
import re
|
12 |
|
13 |
-
#
|
14 |
load_dotenv()
|
15 |
os.getenv("GROQ_API_KEY")
|
16 |
|
17 |
def get_pdf_text(pdf_docs):
|
18 |
-
|
19 |
text = ""
|
20 |
for pdf in pdf_docs:
|
21 |
pdf_reader = PdfReader(pdf)
|
@@ -24,19 +24,19 @@ def get_pdf_text(pdf_docs):
|
|
24 |
return text
|
25 |
|
26 |
def get_text_chunks(text):
|
27 |
-
|
28 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
|
29 |
chunks = text_splitter.split_text(text)
|
30 |
return chunks
|
31 |
|
32 |
def get_vector_store(text_chunks):
|
33 |
-
|
34 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
35 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
36 |
vector_store.save_local("faiss_index")
|
37 |
|
38 |
def get_conversational_chain():
|
39 |
-
|
40 |
prompt_template = """
|
41 |
Responde la pregunta en espa帽ol de la manera m谩s detallada posible a partir del contexto proporcionado. Si la respuesta no est谩 en
|
42 |
el contexto proporcionado, simplemente di, "la respuesta no est谩 disponible en el contexto." No proporciones respuestas incorrectas.
|
@@ -46,7 +46,7 @@ def get_conversational_chain():
|
|
46 |
{question}
|
47 |
Respuesta:
|
48 |
"""
|
49 |
-
|
50 |
model = ChatGroq(
|
51 |
temperature=0.3,
|
52 |
model_name="deepseek-r1-distill-llama-70b",
|
@@ -56,6 +56,7 @@ def get_conversational_chain():
|
|
56 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
57 |
return chain
|
58 |
|
|
|
59 |
def eliminar_texto_entre_tags(texto):
|
60 |
patron = r'<think>.*?</think>'
|
61 |
texto_limpio = re.sub(patron, '', texto, flags=re.DOTALL)
|
@@ -89,18 +90,18 @@ def user_input(user_question):
|
|
89 |
# Eliminar el proceso de pensamiento de la respuesta principal
|
90 |
clean_response = eliminar_texto_entre_tags(original_response)
|
91 |
|
92 |
-
#
|
93 |
print("Cleaned Response:", clean_response)
|
94 |
|
95 |
# Mostrar el proceso de pensamiento del modelo en el expander
|
96 |
-
with st.expander("
|
97 |
st.write(thought_process)
|
98 |
|
99 |
st.markdown(f"### Respuesta:\n{clean_response}")
|
100 |
|
101 |
def main():
|
102 |
"""Funci贸n principal para ejecutar la aplicaci贸n Streamlit."""
|
103 |
-
st.set_page_config(page_title="
|
104 |
|
105 |
# Configuraci贸n de la apariencia de la aplicaci贸n
|
106 |
st.markdown(
|
@@ -142,15 +143,15 @@ def main():
|
|
142 |
unsafe_allow_html=True
|
143 |
)
|
144 |
|
145 |
-
st.title("PDF Consultor")
|
146 |
|
147 |
with st.sidebar:
|
148 |
pdf_docs = st.file_uploader(
|
149 |
-
"Subir archivo PDF",
|
150 |
accept_multiple_files=True,
|
151 |
type=["pdf"]
|
152 |
)
|
153 |
-
if st.button("Procesar"):
|
154 |
with st.spinner("Procesando el archivo..."):
|
155 |
raw_text = get_pdf_text(pdf_docs)
|
156 |
text_chunks = get_text_chunks(raw_text)
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
import re
|
12 |
|
13 |
+
# Cargamos las variables de entorno Groq
|
14 |
load_dotenv()
|
15 |
os.getenv("GROQ_API_KEY")
|
16 |
|
17 |
def get_pdf_text(pdf_docs):
|
18 |
+
# Extraemos texto de los archivos cargados
|
19 |
text = ""
|
20 |
for pdf in pdf_docs:
|
21 |
pdf_reader = PdfReader(pdf)
|
|
|
24 |
return text
|
25 |
|
26 |
def get_text_chunks(text):
|
27 |
+
# Divisi贸n del texto en fragmentos
|
28 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=5000, chunk_overlap=500)
|
29 |
chunks = text_splitter.split_text(text)
|
30 |
return chunks
|
31 |
|
32 |
def get_vector_store(text_chunks):
|
33 |
+
# Creaci贸n de almac茅n de vectores FAISS a partir de los fragmentos
|
34 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
35 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
36 |
vector_store.save_local("faiss_index")
|
37 |
|
38 |
def get_conversational_chain():
|
39 |
+
# Especificamos un prompt inicial al modelo
|
40 |
prompt_template = """
|
41 |
Responde la pregunta en espa帽ol de la manera m谩s detallada posible a partir del contexto proporcionado. Si la respuesta no est谩 en
|
42 |
el contexto proporcionado, simplemente di, "la respuesta no est谩 disponible en el contexto." No proporciones respuestas incorrectas.
|
|
|
46 |
{question}
|
47 |
Respuesta:
|
48 |
"""
|
49 |
+
# Implementamos el modelo
|
50 |
model = ChatGroq(
|
51 |
temperature=0.3,
|
52 |
model_name="deepseek-r1-distill-llama-70b",
|
|
|
56 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
57 |
return chain
|
58 |
|
59 |
+
# Tratamiento para recoger el pensamiento del modelo
|
60 |
def eliminar_texto_entre_tags(texto):
|
61 |
patron = r'<think>.*?</think>'
|
62 |
texto_limpio = re.sub(patron, '', texto, flags=re.DOTALL)
|
|
|
90 |
# Eliminar el proceso de pensamiento de la respuesta principal
|
91 |
clean_response = eliminar_texto_entre_tags(original_response)
|
92 |
|
93 |
+
# Imprimir la respuesta limpia, sin las marcas <think> </think>
|
94 |
print("Cleaned Response:", clean_response)
|
95 |
|
96 |
# Mostrar el proceso de pensamiento del modelo en el expander
|
97 |
+
with st.expander("馃挱 Pensamiento del Modelo"):
|
98 |
st.write(thought_process)
|
99 |
|
100 |
st.markdown(f"### Respuesta:\n{clean_response}")
|
101 |
|
102 |
def main():
|
103 |
"""Funci贸n principal para ejecutar la aplicaci贸n Streamlit."""
|
104 |
+
st.set_page_config(page_title="PDF Consultor 馃攳", page_icon="馃攳", layout="wide")
|
105 |
|
106 |
# Configuraci贸n de la apariencia de la aplicaci贸n
|
107 |
st.markdown(
|
|
|
143 |
unsafe_allow_html=True
|
144 |
)
|
145 |
|
146 |
+
st.title("PDF Consultor 馃攳")
|
147 |
|
148 |
with st.sidebar:
|
149 |
pdf_docs = st.file_uploader(
|
150 |
+
"[1] Subir archivo PDF",
|
151 |
accept_multiple_files=True,
|
152 |
type=["pdf"]
|
153 |
)
|
154 |
+
if st.button("[2] Procesar"):
|
155 |
with st.spinner("Procesando el archivo..."):
|
156 |
raw_text = get_pdf_text(pdf_docs)
|
157 |
text_chunks = get_text_chunks(raw_text)
|