Spaces:
Runtime error
Runtime error
Commit
·
e2afb95
1
Parent(s):
5dfc323
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.vectorstores import Chroma
|
| 5 |
+
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain.chat_models import ChatOpenAI
|
| 8 |
+
|
| 9 |
+
from langchain.document_loaders import PyPDFLoader
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
import fitz
|
| 13 |
+
from PIL import Image
|
| 14 |
+
|
| 15 |
+
# Global variables
|
| 16 |
+
COUNT, N = 0, 0
|
| 17 |
+
chat_history = []
|
| 18 |
+
chain = ''
|
| 19 |
+
enable_box = gr.Textbox.update(value=None,
|
| 20 |
+
placeholder='Upload your OpenAI API key', interactive=True)
|
| 21 |
+
disable_box = gr.Textbox.update(value='OpenAI API key is Set', interactive=False)
|
| 22 |
+
|
| 23 |
+
# Function to set the OpenAI API key
|
| 24 |
+
def set_apikey(api_key):
|
| 25 |
+
os.environ['OPENAI_API_KEY'] = api_key
|
| 26 |
+
return disable_box
|
| 27 |
+
|
| 28 |
+
# Function to enable the API key input box
|
| 29 |
+
def enable_api_box():
|
| 30 |
+
return enable_box
|
| 31 |
+
|
| 32 |
+
# Function to add text to the chat history
|
| 33 |
+
def add_text(history, text):
|
| 34 |
+
if not text:
|
| 35 |
+
raise gr.Error('Enter text')
|
| 36 |
+
history = history + [(text, '')]
|
| 37 |
+
return history
|
| 38 |
+
|
| 39 |
+
# Function to process the PDF file and create a conversation chain
|
| 40 |
+
def process_file(file):
|
| 41 |
+
if 'OPENAI_API_KEY' not in os.environ:
|
| 42 |
+
raise gr.Error('Upload your OpenAI API key')
|
| 43 |
+
|
| 44 |
+
loader = PyPDFLoader(file.name)
|
| 45 |
+
documents = loader.load()
|
| 46 |
+
|
| 47 |
+
embeddings = OpenAIEmbeddings()
|
| 48 |
+
|
| 49 |
+
pdfsearch = Chroma.from_documents(documents, embeddings)
|
| 50 |
+
|
| 51 |
+
chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3),
|
| 52 |
+
retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
| 53 |
+
return_source_documents=True)
|
| 54 |
+
return chain
|
| 55 |
+
|
| 56 |
+
# Function to generate a response based on the chat history and query
|
| 57 |
+
def generate_response(history, query, btn):
|
| 58 |
+
global COUNT, N, chat_history, chain
|
| 59 |
+
|
| 60 |
+
if not btn:
|
| 61 |
+
raise gr.Error(message='Upload a PDF')
|
| 62 |
+
if COUNT == 0:
|
| 63 |
+
chain = process_file(btn)
|
| 64 |
+
COUNT += 1
|
| 65 |
+
|
| 66 |
+
result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True)
|
| 67 |
+
chat_history += [(query, result["answer"])]
|
| 68 |
+
N = list(result['source_documents'][0])[1][1]['page']
|
| 69 |
+
|
| 70 |
+
for char in result['answer']:
|
| 71 |
+
history[-1][-1] += char
|
| 72 |
+
yield history, ''
|
| 73 |
+
|
| 74 |
+
# Function to render a specific page of a PDF file as an image
|
| 75 |
+
def render_file(file):
|
| 76 |
+
global N
|
| 77 |
+
doc = fitz.open(file.name)
|
| 78 |
+
page = doc[N]
|
| 79 |
+
# Render the page as a PNG image with a resolution of 300 DPI
|
| 80 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
| 81 |
+
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
| 82 |
+
return image
|
| 83 |
+
|
| 84 |
+
# Gradio application setup
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
# Create a Gradio block
|
| 87 |
+
|
| 88 |
+
with gr.Column():
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column(scale=0.8):
|
| 91 |
+
api_key = gr.Textbox(
|
| 92 |
+
placeholder='Enter OpenAI API key',
|
| 93 |
+
show_label=False,
|
| 94 |
+
interactive=True
|
| 95 |
+
).style(container=False)
|
| 96 |
+
with gr.Column(scale=0.2):
|
| 97 |
+
change_api_key = gr.Button('Change Key')
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
chatbot = gr.Chatbot(value=[], elem_id='chatbot').style(height=650)
|
| 101 |
+
show_img = gr.Image(label='Upload PDF', tool='select').style(height=680)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
with gr.Column(scale=0.70):
|
| 105 |
+
txt = gr.Textbox(
|
| 106 |
+
show_label=False,
|
| 107 |
+
placeholder="Enter text and press enter"
|
| 108 |
+
).style(container=False)
|
| 109 |
+
|
| 110 |
+
with gr.Column(scale=0.15):
|
| 111 |
+
submit_btn = gr.Button('Submit')
|
| 112 |
+
|
| 113 |
+
with gr.Column(scale=0.15):
|
| 114 |
+
btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"]).style()
|
| 115 |
+
|
| 116 |
+
# Set up event handlers
|
| 117 |
+
|
| 118 |
+
# Event handler for submitting the OpenAI API key
|
| 119 |
+
api_key.submit(fn=set_apikey, inputs=[api_key], outputs=[api_key])
|
| 120 |
+
|
| 121 |
+
# Event handler for changing the API key
|
| 122 |
+
change_api_key.click(fn=enable_api_box, outputs=[api_key])
|
| 123 |
+
|
| 124 |
+
# Event handler for uploading a PDF
|
| 125 |
+
btn.upload(fn=render_first, inputs=[btn], outputs=[show_img])
|
| 126 |
+
|
| 127 |
+
# Event handler for submitting text and generating response
|
| 128 |
+
submit_btn.click(
|
| 129 |
+
fn=add_text,
|
| 130 |
+
inputs=[chatbot, txt],
|
| 131 |
+
outputs=[chatbot],
|
| 132 |
+
queue=False
|
| 133 |
+
).success(
|
| 134 |
+
fn=generate_response,
|
| 135 |
+
inputs=[chatbot, txt, btn],
|
| 136 |
+
outputs=[chatbot, txt]
|
| 137 |
+
).success(
|
| 138 |
+
fn=render_file,
|
| 139 |
+
inputs=[btn],
|
| 140 |
+
outputs=[show_img]
|
| 141 |
+
)
|
| 142 |
+
demo.queue()
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
demo.launch()
|