Spaces:
Runtime error
Runtime error
delete copy app
Browse files- NewMistral.py +0 -141
NewMistral.py
DELETED
@@ -1,141 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from streamlit_chat import message
|
3 |
-
from langchain.chains import ConversationalRetrievalChain
|
4 |
-
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
5 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
-
from langchain.llms import CTransformers
|
7 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
-
from langchain.vectorstores import FAISS
|
9 |
-
from langchain.memory import ConversationBufferMemory
|
10 |
-
import streamlit.components.v1 as components
|
11 |
-
from templatesStreamlit import *
|
12 |
-
import tempfile
|
13 |
-
import os
|
14 |
-
|
15 |
-
# Funcion para leer los documentos
|
16 |
-
def load_documents(uploaded_files):
|
17 |
-
docs = []
|
18 |
-
temp_dir = tempfile.TemporaryDirectory()
|
19 |
-
for file in uploaded_files:
|
20 |
-
temp_filepath = os.path.join(temp_dir.name, file.name)
|
21 |
-
with open(temp_filepath, "wb") as f:
|
22 |
-
f.write(file.getvalue())
|
23 |
-
loader = PyPDFLoader(temp_filepath)
|
24 |
-
docs.extend(loader.load())
|
25 |
-
|
26 |
-
# loader = DirectoryLoader('data/', glob="*.pdf", loader_cls=PyPDFLoader)
|
27 |
-
# documents = loader.load()
|
28 |
-
return docs
|
29 |
-
|
30 |
-
# Funcion para convertir el texto en chunks
|
31 |
-
def split_text_into_chunks(documents):
|
32 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
33 |
-
text_chunks = text_splitter.split_documents(documents)
|
34 |
-
return text_chunks
|
35 |
-
|
36 |
-
def get_vectorstore(text_chunks):
|
37 |
-
embbedings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': "cpu"})
|
38 |
-
vector_store = FAISS.from_documents(text_chunks, embbedings)
|
39 |
-
return vector_store
|
40 |
-
|
41 |
-
# def create_llms_model():
|
42 |
-
# llm = CTransformers(model="mistral-7b-instruct-v0.1.Q4_K_M.gguf", config={'max_new_tokens': 512, 'temperature': 0.01})
|
43 |
-
# return llm
|
44 |
-
|
45 |
-
def get_conversation_chain(vector_store):
|
46 |
-
|
47 |
-
llm = CTransformers(model="mistral-7b-instruct-v0.1.Q4_K_M.gguf", config={'max_new_tokens': 512, 'temperature': 0.01})
|
48 |
-
|
49 |
-
#Creamos la memoria
|
50 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
51 |
-
|
52 |
-
# Create chain (lANGCHAIN)
|
53 |
-
conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
|
54 |
-
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
55 |
-
memory=memory)
|
56 |
-
return conversation_chain
|
57 |
-
|
58 |
-
def handle_userinput(user_question):
|
59 |
-
response = st.session_state.conversation({'question': user_question})
|
60 |
-
st.session_state.chat_history = response['chat_history']
|
61 |
-
|
62 |
-
for i, message in enumerate(st.session_state.chat_history):
|
63 |
-
if i % 2 == 0:
|
64 |
-
st.write(user_template2.replace(
|
65 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
66 |
-
else:
|
67 |
-
st.write(bot_template2.replace(
|
68 |
-
"{{MSG}}", message.content), unsafe_allow_html=True)
|
69 |
-
|
70 |
-
|
71 |
-
def main():
|
72 |
-
st.set_page_config(page_title="LLM-RAG",
|
73 |
-
page_icon=":books:")
|
74 |
-
st.write(css, unsafe_allow_html=True)
|
75 |
-
|
76 |
-
titulo = f"""
|
77 |
-
<div class="btn-neon">
|
78 |
-
<span class="icon"><img src=static/Mistral.png></span>
|
79 |
-
Mistral7b + Streamlit
|
80 |
-
<span class="icon"><img src=static/streamlit.png></span>
|
81 |
-
</div>
|
82 |
-
"""
|
83 |
-
st.markdown(titulo, unsafe_allow_html=True)
|
84 |
-
|
85 |
-
presentacion = f"""
|
86 |
-
<div class="skill">
|
87 |
-
<div class="skill-content">
|
88 |
-
<div class="skill-img-box">
|
89 |
-
<a href="https://www.linkedin.com/in/manueloteromarquez/" target="_blank">
|
90 |
-
<img src="https://media.licdn.com/dms/image/C4D03AQEsabRcMGkMmQ/profile-displayphoto-shrink_800_800/0/1663585925916?e=1708560000&v=beta&t=1Ofx1PsbTSlMcNIVCxznEjtIA_aIlTVaJm52toMKddU" alt="Tu descripción">
|
91 |
-
</a>
|
92 |
-
</div>
|
93 |
-
<div class="skill-detail">
|
94 |
-
<h2 class="skill-title">Manuel Otero</h2>
|
95 |
-
<p>211 Days</p>
|
96 |
-
<div class="skill-progress">
|
97 |
-
<div class="progress progress-1"></div>
|
98 |
-
</div>
|
99 |
-
</div>
|
100 |
-
</div>
|
101 |
-
<h2 class="percent">60%</h2>
|
102 |
-
</div>
|
103 |
-
"""
|
104 |
-
st.markdown(presentacion, unsafe_allow_html=True)
|
105 |
-
|
106 |
-
if "conversation" not in st.session_state:
|
107 |
-
st.session_state.conversation = None
|
108 |
-
if "chat_history" not in st.session_state:
|
109 |
-
st.session_state.chat_history = None
|
110 |
-
|
111 |
-
st.header("Hazle preguntas a tus documentos PDFs :books:")
|
112 |
-
|
113 |
-
|
114 |
-
with st.sidebar:
|
115 |
-
st.subheader("Tus Documentos")
|
116 |
-
pdf_docs = st.file_uploader(
|
117 |
-
"Sube tus PDFs aquí y pulsa 'Procesar PDF'", accept_multiple_files=True)
|
118 |
-
if not pdf_docs:
|
119 |
-
st.info("Sube tus pdfs para continuar.")
|
120 |
-
st.stop()
|
121 |
-
|
122 |
-
if st.button("Procesar PDF"):
|
123 |
-
with st.spinner("Procesando"):
|
124 |
-
# get pdf text
|
125 |
-
documents = load_documents(pdf_docs)
|
126 |
-
print(documents)
|
127 |
-
text_chunks = split_text_into_chunks(documents)
|
128 |
-
# create vector store
|
129 |
-
vectorstore = get_vectorstore(text_chunks)
|
130 |
-
# create conversation chain
|
131 |
-
st.session_state.conversation = get_conversation_chain(vectorstore)
|
132 |
-
|
133 |
-
user_question = st.text_input("Adelante pregunta")
|
134 |
-
if user_question:
|
135 |
-
handle_userinput(user_question)
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
if __name__ == '__main__':
|
141 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|