Spaces:
Sleeping
Sleeping
231217: first version of app
Browse files- app.py +59 -4
- requirements.txt +7 -0
app.py
CHANGED
|
@@ -1,8 +1,63 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 7 |
+
from langchain.llms import OpenAI
|
| 8 |
+
from langchain.callbacks import get_openai_callback
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
def process_pdf_and_answer_question(pdf, user_question, openai_key):
|
| 12 |
+
if pdf is None or user_question == "":
|
| 13 |
+
return "Please upload a PDF and enter a question."
|
| 14 |
|
| 15 |
+
if not pdf.name.lower().endswith('.pdf'):
|
| 16 |
+
return "Please upload a PDF file."
|
| 17 |
|
| 18 |
+
pdf_reader = PdfReader(pdf)
|
| 19 |
+
text = ""
|
| 20 |
+
for page in pdf_reader.pages:
|
| 21 |
+
text += page.extract_text()
|
| 22 |
+
|
| 23 |
+
text_splitter = CharacterTextSplitter(
|
| 24 |
+
separator="\n",
|
| 25 |
+
chunk_size=1000,
|
| 26 |
+
chunk_overlap=200,
|
| 27 |
+
length_function=len
|
| 28 |
+
)
|
| 29 |
+
chunks = text_splitter.split_text(text)
|
| 30 |
+
|
| 31 |
+
# Set OpenAI API key from the provided input
|
| 32 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 33 |
+
|
| 34 |
+
embeddings = OpenAIEmbeddings()
|
| 35 |
+
knowledge_base = FAISS.from_texts(chunks, embeddings)
|
| 36 |
+
|
| 37 |
+
docs = knowledge_base.similarity_search(user_question)
|
| 38 |
+
llm = OpenAI()
|
| 39 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 40 |
+
with get_openai_callback() as cb:
|
| 41 |
+
response = chain.run(input_documents=docs, question=user_question)
|
| 42 |
+
print(cb)
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
with gr.Accordion("Enter OpenAI API Key for the app to work"):
|
| 48 |
+
openai_key = gr.Textbox(label="API Key", placeholder="Enter your OpenAI API Key here")
|
| 49 |
+
|
| 50 |
+
gr.Markdown("Q&A on your PDF")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
pdf = gr.File(label="Upload a PDF")
|
| 53 |
+
user_question = gr.Textbox(label="Ask a question about this PDF:")
|
| 54 |
+
answer_button = gr.Button("Answer")
|
| 55 |
+
response = gr.Textbox(label="Response", interactive=False)
|
| 56 |
+
|
| 57 |
+
answer_button.click(
|
| 58 |
+
fn=process_pdf_and_answer_question,
|
| 59 |
+
inputs=[pdf, user_question, openai_key],
|
| 60 |
+
outputs=response
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.10.0
|
| 2 |
+
langchain
|
| 3 |
+
PyPDF2==3.0.1
|
| 4 |
+
python-dotenv==1.0.0
|
| 5 |
+
openai==0.27.6
|
| 6 |
+
faiss-cpu==1.7.4
|
| 7 |
+
tiktoken==0.4.0
|