Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,16 @@
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
-
from langchain.chains import
|
4 |
from langchain.llms import OpenAI
|
5 |
from langchain.document_loaders import PyPDFLoader
|
6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
from langchain.vectorstores import FAISS
|
8 |
from langchain.chat_models import ChatOpenAI
|
9 |
-
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
10 |
from PyPDF2 import PdfReader
|
11 |
-
import os
|
12 |
|
13 |
# Function to load and process the PDF document
|
14 |
def load_pdf(file):
|
15 |
-
# Load the PDF using
|
16 |
loader = PyPDFLoader(file.name)
|
17 |
documents = loader.load()
|
18 |
return documents
|
@@ -22,6 +20,7 @@ def summarize_pdf(file, openai_api_key):
|
|
22 |
# Set the API key dynamically
|
23 |
openai.api_key = openai_api_key
|
24 |
|
|
|
25 |
documents = load_pdf(file)
|
26 |
|
27 |
# Create embeddings for the documents
|
@@ -30,16 +29,16 @@ def summarize_pdf(file, openai_api_key):
|
|
30 |
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
31 |
vector_store = FAISS.from_documents(documents, embeddings)
|
32 |
|
33 |
-
# Create a
|
34 |
llm = ChatOpenAI(model="gpt-4") # Using GPT-4 as the LLM
|
35 |
-
|
36 |
-
llm=llm,
|
37 |
-
|
38 |
-
|
39 |
)
|
40 |
|
41 |
-
# Query the model for a summary
|
42 |
-
response =
|
43 |
return response
|
44 |
|
45 |
# Function to handle user queries and provide answers from the document
|
@@ -47,6 +46,7 @@ def query_pdf(file, user_query, openai_api_key):
|
|
47 |
# Set the API key dynamically
|
48 |
openai.api_key = openai_api_key
|
49 |
|
|
|
50 |
documents = load_pdf(file)
|
51 |
|
52 |
# Create embeddings for the documents
|
@@ -55,16 +55,16 @@ def query_pdf(file, user_query, openai_api_key):
|
|
55 |
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
56 |
vector_store = FAISS.from_documents(documents, embeddings)
|
57 |
|
58 |
-
# Create a
|
59 |
-
llm = ChatOpenAI(model="gpt-
|
60 |
-
|
61 |
llm=llm,
|
62 |
-
|
63 |
-
|
64 |
)
|
65 |
|
66 |
# Query the model for the user query
|
67 |
-
response =
|
68 |
return response
|
69 |
|
70 |
# Define Gradio interface for the summarization
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
from langchain.llms import OpenAI
|
5 |
from langchain.document_loaders import PyPDFLoader
|
6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
from langchain.vectorstores import FAISS
|
8 |
from langchain.chat_models import ChatOpenAI
|
|
|
9 |
from PyPDF2 import PdfReader
|
|
|
10 |
|
11 |
# Function to load and process the PDF document
|
12 |
def load_pdf(file):
|
13 |
+
# Load the PDF using LangChain's PyPDFLoader
|
14 |
loader = PyPDFLoader(file.name)
|
15 |
documents = loader.load()
|
16 |
return documents
|
|
|
20 |
# Set the API key dynamically
|
21 |
openai.api_key = openai_api_key
|
22 |
|
23 |
+
# Load and process the PDF
|
24 |
documents = load_pdf(file)
|
25 |
|
26 |
# Create embeddings for the documents
|
|
|
29 |
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
30 |
vector_store = FAISS.from_documents(documents, embeddings)
|
31 |
|
32 |
+
# Create a RetrievalQA chain for summarization
|
33 |
llm = ChatOpenAI(model="gpt-4") # Using GPT-4 as the LLM
|
34 |
+
qa_chain = RetrievalQA.from_chain_type(
|
35 |
+
llm=llm,
|
36 |
+
chain_type="stuff",
|
37 |
+
retriever=vector_store.as_retriever()
|
38 |
)
|
39 |
|
40 |
+
# Query the model for a summary of the document
|
41 |
+
response = qa_chain.run("Summarize the content of the research paper.")
|
42 |
return response
|
43 |
|
44 |
# Function to handle user queries and provide answers from the document
|
|
|
46 |
# Set the API key dynamically
|
47 |
openai.api_key = openai_api_key
|
48 |
|
49 |
+
# Load and process the PDF
|
50 |
documents = load_pdf(file)
|
51 |
|
52 |
# Create embeddings for the documents
|
|
|
55 |
# Use LangChain's FAISS Vector Store to store and search the embeddings
|
56 |
vector_store = FAISS.from_documents(documents, embeddings)
|
57 |
|
58 |
+
# Create a RetrievalQA chain for querying the document
|
59 |
+
llm = ChatOpenAI(model="gpt-4") # Using GPT-4 as the LLM
|
60 |
+
qa_chain = RetrievalQA.from_chain_type(
|
61 |
llm=llm,
|
62 |
+
chain_type="stuff",
|
63 |
+
retriever=vector_store.as_retriever()
|
64 |
)
|
65 |
|
66 |
# Query the model for the user query
|
67 |
+
response = qa_chain.run(user_query)
|
68 |
return response
|
69 |
|
70 |
# Define Gradio interface for the summarization
|