Spaces:
Sleeping
Sleeping
cafepoetica
commited on
Commit
•
092632e
1
Parent(s):
8947d28
Update app.py
Browse files
app.py
CHANGED
@@ -2,17 +2,20 @@ import streamlit as st
|
|
2 |
import os
|
3 |
from PyPDF2 import PdfReader
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
from langchain.vectorstores import FAISS
|
7 |
from langchain.chat_models import ChatOpenAI
|
8 |
from langchain.chains.question_answering import load_qa_chain
|
9 |
|
10 |
-
st.set_page_config('Mi tech personal')
|
11 |
st.header("Pregunta a tu teach")
|
|
|
|
|
12 |
OPENAI_API_KEY = st.text_input('OpenAI API Key', type='password')
|
13 |
-
|
14 |
|
15 |
-
|
|
|
16 |
def create_embeddings(pdf):
|
17 |
pdf_reader = PdfReader(pdf)
|
18 |
text = ""
|
@@ -23,7 +26,7 @@ def create_embeddings(pdf):
|
|
23 |
chunk_size=800,
|
24 |
chunk_overlap=100,
|
25 |
length_function=len
|
26 |
-
|
27 |
chunks = text_splitter.split_text(text)
|
28 |
|
29 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
@@ -31,15 +34,20 @@ def create_embeddings(pdf):
|
|
31 |
|
32 |
return knowledge_base
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
user_question = st.text_input("Haz una pregunta sobre tu PDF:")
|
37 |
-
|
38 |
-
if user_question:
|
39 |
-
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
40 |
-
docs = knowledge_base.similarity_search(user_question, 3)
|
41 |
-
llm = ChatOpenAI(model_name='gpt-3.5-turbo')
|
42 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
43 |
-
respuesta = chain.run(input_documents=docs, question=user_question)
|
44 |
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
from PyPDF2 import PdfReader
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
from langchain.vectorstores import FAISS
|
7 |
from langchain.chat_models import ChatOpenAI
|
8 |
from langchain.chains.question_answering import load_qa_chain
|
9 |
|
10 |
+
st.set_page_config(page_title='Mi tech personal', page_icon=':books:')
|
11 |
st.header("Pregunta a tu teach")
|
12 |
+
|
13 |
+
# Configurar la clave de API de OpenAI
|
14 |
OPENAI_API_KEY = st.text_input('OpenAI API Key', type='password')
|
15 |
+
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
16 |
|
17 |
+
# Cargar y procesar el PDF
|
18 |
+
@st.cache_resource
|
19 |
def create_embeddings(pdf):
|
20 |
pdf_reader = PdfReader(pdf)
|
21 |
text = ""
|
|
|
26 |
chunk_size=800,
|
27 |
chunk_overlap=100,
|
28 |
length_function=len
|
29 |
+
)
|
30 |
chunks = text_splitter.split_text(text)
|
31 |
|
32 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
|
|
|
34 |
|
35 |
return knowledge_base
|
36 |
|
37 |
+
# Cargar el archivo PDF
|
38 |
+
pdf_obj = st.file_uploader("Carga tu documento", type="pdf", on_change=st.cache_resource.clear)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
if pdf_obj:
|
41 |
+
try:
|
42 |
+
knowledge_base = create_embeddings(pdf_obj)
|
43 |
+
user_question = st.text_input("Haz una pregunta sobre tu PDF:")
|
44 |
+
|
45 |
+
if user_question:
|
46 |
+
docs = knowledge_base.similarity_search(user_question, 3)
|
47 |
+
llm = ChatOpenAI(model_name='gpt-3.5-turbo')
|
48 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
49 |
+
respuesta = chain.run(input_documents=docs, question=user_question)
|
50 |
+
|
51 |
+
st.write(respuesta)
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"Se produjo un error: {e}")
|