Spaces:
Sleeping
Sleeping
File size: 11,312 Bytes
9acf74a 80a4eae 9acf74a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
import os
import requests
from io import BytesIO
from PyPDF2 import PdfReader
import pandas as pd
from openai.embeddings_utils import get_embedding, cosine_similarity
import openai
import streamlit as st
import numpy as np
import base64
import faiss
messages = [
{"role": "system", "content": "You are SummarizeGPT, a large language model whose expertise is reading and summarizing scientific papers."}
]
class Chatbot():
def parse_paper(self, pdf):
# This function parses the PDF and returns a list of dictionaries with the text,
# font size, and x and y coordinates of each text element in the PDF
print("Parsing paper")
number_of_pages = len(pdf.pages)
print(f"Total number of pages: {number_of_pages}")
# This is the list that will contain all the text elements in the PDF and will be returned by the function
paper_text = []
for i in range(number_of_pages):
# Iterate through each page in the PDF, and extract the text elements. pdf.pages is a list of Page objects.
page = pdf.pages[i]
# This is the list that will contain all the text elements in the current page
page_text = []
def visitor_body(text, cm, tm, fontDict, fontSize):
# tm is a 6-element tuple of floats that represent a 2x3 matrix, which is the text matrix for the text.
# The first two elements are the horizontal and vertical scaling factors, the third and fourth elements
# are the horizontal and vertical shear factors, and the fifth and sixth elements are the horizontal and vertical translation factors.
# x and y are the coordinates of the text element
x = tm[4]
y = tm[5]
# ignore header/footer, and empty text.
# The y coordinate is used to filter out the header and footer of the paper
# The length of the text is used to filter out empty text
if (y > 50 and y < 720) and (len(text.strip()) > 1):
page_text.append({
# The fontsize is used to separate paragraphs into different elements in the paper_text list
'fontsize': fontSize,
# The text is stripped of whitespace and the \x03 character
'text': text.strip().replace('\x03', ''),
# The x and y coordinates are used to separate paragraphs into different elements in the paper_text list
'x': x,
'y': y
})
# Extract the text elements from the page
_ = page.extract_text(visitor_text=visitor_body)
print(f'Page {i} text", {page_text}')
blob_font_size = None
blob_text = ''
processed_text = []
for t in page_text:
if t['fontsize'] == blob_font_size:
blob_text += f" {t['text']}"
if len(blob_text) >= 2000:
processed_text.append({
'fontsize': blob_font_size,
'text': blob_text,
'page': i
})
blob_font_size = None
blob_text = ''
else:
if blob_font_size is not None and len(blob_text) >= 1:
processed_text.append({
'fontsize': blob_font_size,
'text': blob_text,
'page': i
})
blob_font_size = t['fontsize']
blob_text = t['text']
paper_text += processed_text
print("Done parsing paper")
print(paper_text)
return paper_text
def paper_df(self, pdf):
print('Creating dataframe')
filtered_pdf= []
for row in pdf:
# This will use the get method to safely access the 'text' key in the row dictionary,
# and if the key is not present, it will use an empty string as a default value. This
# should prevent a KeyError from occurring.
if len(row.get('text', '')) < 30:
continue
filtered_pdf.append(row)
print("Filtered paper_text", filtered_pdf)
df = pd.DataFrame(filtered_pdf)
print(df.shape)
print(df.head)
# remove elements with identical df[text] and df[page] values
df = df.drop_duplicates(subset=['text', 'page'], keep='first')
df['length'] = df['text'].apply(lambda x: len(x))
print('Done creating dataframe')
return df
def calculate_embeddings(self, df):
print('Calculating embeddings')
openai.api_key = os.getenv('OPENAI_API_KEY')
embedding_model = "text-embedding-ada-002"
# Get the embeddings for each text element in the dataframe
embeddings = df.text.apply(lambda x: get_embedding(x, engine=embedding_model))
embeddings = np.vstack(embeddings, dtype=np.float32)
return embeddings
def search_embeddings(self, embeddings, df, query, n=3, pprint=True):
# Step 1. Get an embedding for the question being asked to the PDF
query_embedding = get_embedding(query, engine="text-embedding-ada-002")
query_embedding = np.array(query_embedding, dtype=np.float32)
# Step 2. Create a FAISS index and add the embeddings
d = embeddings.shape[1]
# Use the L2 distance metric
index = faiss.IndexFlatL2(d)
print("Embeddings shape:", embeddings.shape)
print("Embeddings data type:", type(embeddings))
index.add(embeddings)
# Step 3. Search the index for the embedding of the question
D, I = index.search(query_embedding.reshape(1,d), n)
# Step 4. Get the top n results from the dataframe
results = df.iloc[I[0]]
results['similarity'] = D[0]
results = results.reset_index(drop=True)
# Make a dictionary of the first n results with the page number as the key and the text as the value
global sources
sources = []
for i in range(n):
# append the page number and the text as a dict to the sources list
sources.append({'Page '+str(results.iloc[i]['page']): results.iloc[i]['text'][:150]+'...'})
print(sources)
return results.head(n)
def create_prompt(self, embeddings, df, user_input):
result = self.search_embeddings(embeddings, df, user_input, n=3)
print(result)
prompt = """
You are Research Paper Guru
The user is going to ask you a question about a research paper after uploading a PDF of the paper.
You are a large language model whose expertise is reading and and providing answers to their queries, based on what you know about the subject as well as what you know about the text given to you.
The user asks: """+ user_input + """
And the information about the paper that is relevant to the question is:
1.""" + str(result.iloc[0]['text']) + """
2.""" + str(result.iloc[1]['text']) + """
3.""" + str(result.iloc[2]['text']) + """
Knowing what you know about this answer, as well as being able to navigate this knowledge in conjuction with what is being said in the paper, provide an answer to the user. If the person asks you to summarize what is in the paper, do your best to provide a summary of the paper.
The goal here is to keep the user happy and satisfied that you have given them the best answer to the question to the best of your knowledge. If necessary, you can also point them to outside resources for more information.:"""
print('Done creating prompt')
return prompt
def gpt(self, prompt):
openai.api_key = os.getenv('OPENAI_API_KEY')
print('got API key')
messages.append({"role": "user", "content": prompt})
r = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
answer = r['choices'][0]['message']['content']
response = {'answer': answer, 'sources': sources}
return response
def reply(self, embeddings, user_input):
print(user_input)
prompt = self.create_prompt(embeddings, df, user_input)
return self.gpt(prompt)
def process_pdf(file):
print("Processing pdf")
pdf = PdfReader(BytesIO(file))
chatbot = Chatbot()
paper_text = chatbot.parse_paper(pdf)
global df
df = chatbot.paper_df(paper_text)
embeddings = chatbot.calculate_embeddings(df)
print("Done processing pdf")
return embeddings
def download_pdf(url):
chatbot = Chatbot()
r = requests.get(str(url))
print(r.headers)
pdf = PdfReader(BytesIO(r.content))
paper_text = chatbot.parse_paper(pdf)
global df
df = chatbot.paper_df(paper_text)
embeddings = chatbot.calculate_embeddings(df)
print("Done processing pdf")
return embeddings
def show_pdf(file_content):
base64_pdf = base64.b64encode(file_content).decode('utf-8')
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="800" height="800" type="application/pdf"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
def main():
st.title("DeepRnD: AI Resaerch Assistant")
st.subheader("Upload PDF or Enter URL")
embeddings = None
pdf_option = st.selectbox("Choose an option:", ["Upload PDF", "Enter URL"])
chatbot = Chatbot()
if pdf_option == "Upload PDF":
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file is not None:
file_content = uploaded_file.read()
embeddings = process_pdf(file_content)
st.success("PDF uploaded and processed successfully!")
show_pdf(file_content)
elif pdf_option == "Enter URL":
url = st.text_input("Enter the URL of the PDF:")
if url:
if st.button("Download and process PDF"):
try:
r = requests.get(str(url))
content = r.content
embeddings = download_pdf(url)
st.success("PDF downloaded and processed successfully!")
show_pdf(content)
except Exception as e:
st.error(f"An error occurred while processing the PDF: {e}")
st.subheader("Ask a question about a research paper and get an answer with sources!")
query = st.text_input("Enter your query:")
if query:
if st.button("Get answer"):
if embeddings is not None:
response = chatbot.reply(embeddings, query)
else:
st.warning("Please upload a PDF or enter a URL first.")
st.write(response['answer'])
st.write("Sources:")
for source in response['sources']:
st.write(source)
if __name__ == "__main__":
main()
|