Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,19 @@ from langchain.memory import ConversationBufferMemory
|
|
10 |
from langchain_core.prompts import PromptTemplate
|
11 |
import streamlit as st
|
12 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Access the OpenAI API key from the environment
|
15 |
open_ai_key = os.getenv("OPENAI_API_KEY")
|
@@ -31,10 +44,16 @@ prompt = PromptTemplate(template=template, input_variables=["context", "question
|
|
31 |
# upload PDF
|
32 |
pdf_file = st.file_uploader("Upload your pdf",type="pdf")
|
33 |
question = st.text_input("Ask a question:")
|
|
|
|
|
|
|
|
|
34 |
if pdf_file is not None:
|
35 |
-
try:
|
|
|
|
|
36 |
# Load and process the PDF
|
37 |
-
loader = PDFPlumberLoader(pdf_file)
|
38 |
pdf_data = loader.load()
|
39 |
|
40 |
# Split the text into chunks
|
|
|
10 |
from langchain_core.prompts import PromptTemplate
|
11 |
import streamlit as st
|
12 |
import os
|
13 |
+
from io import BytesIO
|
14 |
+
|
15 |
+
class InMemoryPDFLoader(BaseLoader):
|
16 |
+
def __init__(self, file_bytes: bytes):
|
17 |
+
self.file_bytes = file_bytes
|
18 |
+
|
19 |
+
def load(self):
|
20 |
+
pdf_stream = BytesIO(self.file_bytes)
|
21 |
+
with pdfplumber.open(pdf_stream) as pdf:
|
22 |
+
text = ""
|
23 |
+
for page in pdf.pages:
|
24 |
+
text += page.extract_text()
|
25 |
+
return [Document(page_content=text)]
|
26 |
|
27 |
# Access the OpenAI API key from the environment
|
28 |
open_ai_key = os.getenv("OPENAI_API_KEY")
|
|
|
44 |
# upload PDF
|
45 |
pdf_file = st.file_uploader("Upload your pdf",type="pdf")
|
46 |
question = st.text_input("Ask a question:")
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
if pdf_file is not None:
|
52 |
+
try:
|
53 |
+
pdf_bytes = pdf_file.read()
|
54 |
+
loader = InMemoryPDFLoader(file_bytes=pdf_bytes)
|
55 |
# Load and process the PDF
|
56 |
+
# loader = PDFPlumberLoader(pdf_file)
|
57 |
pdf_data = loader.load()
|
58 |
|
59 |
# Split the text into chunks
|