NechuBM commited on
Commit
b48b23f
1 Parent(s): 2c605b3

Upload streamlit app to ask ChatGPT about a PDF

Browse files
Files changed (2) hide show
  1. app.py +46 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+
4
+ from PyPDF2 import PdfReader
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.embeddings import HuggingFaceEmbeddings
7
+ from langchain.vectorstores import FAISS
8
+ from langchain.chat_models import ChatOpenAI
9
+ from langchain.chains.question_answering import load_qa_chain
10
+
11
+ st.set_page_config('preguntaDOC')
12
+ st.header("Pregunta a tu PDF")
13
+ OPENAI_API_KEY = st.text_input('OpenAI API Key', type='password')
14
+ pdf_obj = st.file_uploader("Carga tu documento", type="pdf", on_change=st.cache_resource.clear)
15
+
16
+ @st.cache_resource
17
+ def create_embeddings(pdf):
18
+ pdf_reader = PdfReader(pdf)
19
+ text = ""
20
+ for page in pdf_reader.pages:
21
+ text += page.extract_text()
22
+
23
+ text_splitter = RecursiveCharacterTextSplitter(
24
+ chunk_size=800,
25
+ chunk_overlap=100,
26
+ length_function=len
27
+ )
28
+ chunks = text_splitter.split_text(text)
29
+
30
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
31
+ knowledge_base = FAISS.from_texts(chunks, embeddings)
32
+
33
+ return knowledge_base
34
+
35
+ if pdf_obj:
36
+ knowledge_base = create_embeddings(pdf_obj)
37
+ user_question = st.text_input("Haz una pregunta sobre tu PDF:")
38
+
39
+ if user_question:
40
+ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
41
+ docs = knowledge_base.similarity_search(user_question, 3)
42
+ llm = ChatOpenAI(model_name='gpt-3.5-turbo')
43
+ chain = load_qa_chain(llm, chain_type="stuff")
44
+ respuesta = chain.run(input_documents=docs, question=user_question)
45
+
46
+ st.write(respuesta)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai
2
+ langchain
3
+ faiss-cpu
4
+ streamlit
5
+ PyPDF2