Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,71 +8,103 @@ from langchain.document_loaders import PyPDFLoader
|
|
| 8 |
import os
|
| 9 |
import fitz
|
| 10 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Global variables
|
| 13 |
-
COUNT, N = 0, 0
|
| 14 |
-
chat_history = []
|
| 15 |
-
chain = None # Initialize chain as None
|
| 16 |
-
|
| 17 |
-
# Function to set the OpenAI API key
|
| 18 |
-
def set_apikey(api_key):
|
| 19 |
-
os.environ['OPENAI_API_KEY'] = api_key
|
| 20 |
-
return disable_box # Update the disable_box
|
| 21 |
-
|
| 22 |
-
# Function to enable the API key input box
|
| 23 |
-
def enable_api_box():
|
| 24 |
-
return enable_box # Update the enable_box
|
| 25 |
-
|
| 26 |
-
# Function to add text to the chat history
|
| 27 |
-
def add_text(history, text):
|
| 28 |
-
if not text:
|
| 29 |
-
raise gr.Error('Enter text')
|
| 30 |
-
history = history + [(text, '')]
|
| 31 |
-
return history
|
| 32 |
-
|
| 33 |
-
# Function to process the PDF file and create a conversation chain
|
| 34 |
-
def process_file(file):
|
| 35 |
-
global chain # Access the global 'chain' variable
|
| 36 |
-
if 'OPENAI_API_KEY' not in os.environ:
|
| 37 |
-
raise gr.Error('Upload your OpenAI API key')
|
| 38 |
-
|
| 39 |
-
loader = PyPDFLoader(file.name)
|
| 40 |
-
documents = loader.load()
|
| 41 |
-
embeddings = OpenAIEmbeddings()
|
| 42 |
-
pdfsearch = Chroma.from_documents(documents, embeddings)
|
| 43 |
-
chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3),
|
| 44 |
-
retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
| 45 |
-
return_source_documents=True)
|
| 46 |
-
return chain
|
| 47 |
-
|
| 48 |
-
# Function to generate a response based on the chat history and query
|
| 49 |
-
def generate_response(history, query, btn):
|
| 50 |
-
global COUNT, N, chat_history, chain
|
| 51 |
-
if not btn:
|
| 52 |
-
raise gr.Error(message='Upload a PDF')
|
| 53 |
-
|
| 54 |
-
if COUNT == 0:
|
| 55 |
-
chain = process_file(btn)
|
| 56 |
-
COUNT += 1
|
| 57 |
-
|
| 58 |
-
result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True)
|
| 59 |
-
chat_history += [(query, result["answer"])]
|
| 60 |
-
N = list(result['source_documents'][0])[1][1]['page']
|
| 61 |
-
|
| 62 |
-
for char in result['answer']:
|
| 63 |
-
history[-1][-1] += char # Update the last response
|
| 64 |
-
yield history, ''
|
| 65 |
-
|
| 66 |
-
# Function to render a specific page of a PDF file as an image
|
| 67 |
-
def render_file(file):
|
| 68 |
-
global N
|
| 69 |
-
doc = fitz.open(file.name)
|
| 70 |
-
page = doc[N]
|
| 71 |
-
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
| 72 |
-
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
| 73 |
-
return image
|
| 74 |
-
|
| 75 |
-
# Gradio application setup
|
| 76 |
# with gr.Blocks() as demo:
|
| 77 |
# with gr.Column():
|
| 78 |
# gr.Markdown("""
|
|
@@ -81,67 +113,67 @@ def render_file(file):
|
|
| 81 |
# </style>
|
| 82 |
# """)
|
| 83 |
# with gr.Row():
|
| 84 |
-
# enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
| 85 |
# show_label=False, interactive=True)
|
| 86 |
# disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
| 87 |
# change_api_key = gr.Button('Change Key')
|
| 88 |
# with gr.Row():
|
| 89 |
# chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
| 90 |
-
# show_img = gr.Image(label='Upload PDF')
|
| 91 |
-
|
| 92 |
-
# # Set up event handlers
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
#
|
| 98 |
-
# change_api_key.click(fn=enable_api_box, outputs=[enable_box])
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
def render_first(pdf_file):
|
| 102 |
-
# ... Logic to process the PDF
|
| 103 |
-
# ... Generate the first image
|
| 104 |
-
return image
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
with gr.Blocks() as demo:
|
| 108 |
-
with gr.Column():
|
| 109 |
-
gr.Markdown("""
|
| 110 |
-
<style>
|
| 111 |
-
.image-container { height: 680px; }
|
| 112 |
-
</style>
|
| 113 |
-
""")
|
| 114 |
-
with gr.Row():
|
| 115 |
-
enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
| 116 |
-
show_label=False, interactive=True)
|
| 117 |
-
disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
| 118 |
-
change_api_key = gr.Button('Change Key')
|
| 119 |
-
with gr.Row():
|
| 120 |
-
chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
| 121 |
-
show_img = gr.Image(label='Upload PDF')
|
| 122 |
-
pdf_upload = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"]) # Added
|
| 123 |
-
|
| 124 |
-
# Event handlers
|
| 125 |
-
enable_box.submit(fn=set_apikey, inputs=[enable_box], outputs=[disable_box])
|
| 126 |
-
change_api_key.click(fn=enable_api_box, outputs=[enable_box])
|
| 127 |
-
pdf_upload.upload(fn=render_first, inputs=[pdf_upload], outputs=[show_img]) # Corrected
|
| 128 |
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
demo.launch(server_port=7861)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
import os
|
| 9 |
import fitz
|
| 10 |
from PIL import Image
|
| 11 |
+
import streamlit as st
|
| 12 |
+
|
| 13 |
+
# # Global variables
|
| 14 |
+
# COUNT, N = 0, 0
|
| 15 |
+
# chat_history = []
|
| 16 |
+
# chain = None # Initialize chain as None
|
| 17 |
+
|
| 18 |
+
# # Function to set the OpenAI API key
|
| 19 |
+
# def set_apikey(api_key):
|
| 20 |
+
# os.environ['OPENAI_API_KEY'] = api_key
|
| 21 |
+
# return disable_box # Update the disable_box
|
| 22 |
+
|
| 23 |
+
# # Function to enable the API key input box
|
| 24 |
+
# def enable_api_box():
|
| 25 |
+
# return enable_box # Update the enable_box
|
| 26 |
+
|
| 27 |
+
# # Function to add text to the chat history
|
| 28 |
+
# def add_text(history, text):
|
| 29 |
+
# if not text:
|
| 30 |
+
# raise gr.Error('Enter text')
|
| 31 |
+
# history = history + [(text, '')]
|
| 32 |
+
# return history
|
| 33 |
+
|
| 34 |
+
# # Function to process the PDF file and create a conversation chain
|
| 35 |
+
# def process_file(file):
|
| 36 |
+
# global chain # Access the global 'chain' variable
|
| 37 |
+
# if 'OPENAI_API_KEY' not in os.environ:
|
| 38 |
+
# raise gr.Error('Upload your OpenAI API key')
|
| 39 |
+
|
| 40 |
+
# loader = PyPDFLoader(file.name)
|
| 41 |
+
# documents = loader.load()
|
| 42 |
+
# embeddings = OpenAIEmbeddings()
|
| 43 |
+
# pdfsearch = Chroma.from_documents(documents, embeddings)
|
| 44 |
+
# chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3),
|
| 45 |
+
# retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
| 46 |
+
# return_source_documents=True)
|
| 47 |
+
# return chain
|
| 48 |
+
|
| 49 |
+
# # Function to generate a response based on the chat history and query
|
| 50 |
+
# def generate_response(history, query, btn):
|
| 51 |
+
# global COUNT, N, chat_history, chain
|
| 52 |
+
# if not btn:
|
| 53 |
+
# raise gr.Error(message='Upload a PDF')
|
| 54 |
+
|
| 55 |
+
# if COUNT == 0:
|
| 56 |
+
# chain = process_file(btn)
|
| 57 |
+
# COUNT += 1
|
| 58 |
+
|
| 59 |
+
# result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True)
|
| 60 |
+
# chat_history += [(query, result["answer"])]
|
| 61 |
+
# N = list(result['source_documents'][0])[1][1]['page']
|
| 62 |
+
|
| 63 |
+
# for char in result['answer']:
|
| 64 |
+
# history[-1][-1] += char # Update the last response
|
| 65 |
+
# yield history, ''
|
| 66 |
+
|
| 67 |
+
# # Function to render a specific page of a PDF file as an image
|
| 68 |
+
# def render_file(file):
|
| 69 |
+
# global N
|
| 70 |
+
# doc = fitz.open(file.name)
|
| 71 |
+
# page = doc[N]
|
| 72 |
+
# pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
| 73 |
+
# image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
| 74 |
+
# return image
|
| 75 |
+
|
| 76 |
+
# # Gradio application setup
|
| 77 |
+
# # with gr.Blocks() as demo:
|
| 78 |
+
# # with gr.Column():
|
| 79 |
+
# # gr.Markdown("""
|
| 80 |
+
# # <style>
|
| 81 |
+
# # .image-container { height: 680px; }
|
| 82 |
+
# # </style>
|
| 83 |
+
# # """)
|
| 84 |
+
# # with gr.Row():
|
| 85 |
+
# # enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
| 86 |
+
# # show_label=False, interactive=True)
|
| 87 |
+
# # disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
| 88 |
+
# # change_api_key = gr.Button('Change Key')
|
| 89 |
+
# # with gr.Row():
|
| 90 |
+
# # chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
| 91 |
+
# # show_img = gr.Image(label='Upload PDF')
|
| 92 |
+
|
| 93 |
+
# # # Set up event handlers
|
| 94 |
+
|
| 95 |
+
# # # Event handler for submitting the OpenAI API key
|
| 96 |
+
# # enable_box.submit(fn=set_apikey, inputs=[enable_box], outputs=[disable_box])
|
| 97 |
+
|
| 98 |
+
# # # Event handler for changing the API key
|
| 99 |
+
# # change_api_key.click(fn=enable_api_box, outputs=[enable_box])
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# def render_first(pdf_file):
|
| 103 |
+
# # ... Logic to process the PDF
|
| 104 |
+
# # ... Generate the first image
|
| 105 |
+
# return image
|
| 106 |
+
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
# with gr.Blocks() as demo:
|
| 109 |
# with gr.Column():
|
| 110 |
# gr.Markdown("""
|
|
|
|
| 113 |
# </style>
|
| 114 |
# """)
|
| 115 |
# with gr.Row():
|
| 116 |
+
# enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
| 117 |
# show_label=False, interactive=True)
|
| 118 |
# disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
| 119 |
# change_api_key = gr.Button('Change Key')
|
| 120 |
# with gr.Row():
|
| 121 |
# chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
| 122 |
+
# show_img = gr.Image(label='Upload PDF')
|
| 123 |
+
# pdf_upload = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"]) # Added
|
|
|
|
| 124 |
|
| 125 |
+
# # Event handlers
|
| 126 |
+
# enable_box.submit(fn=set_apikey, inputs=[enable_box], outputs=[disable_box])
|
| 127 |
+
# change_api_key.click(fn=enable_api_box, outputs=[enable_box])
|
| 128 |
+
# pdf_upload.upload(fn=render_first, inputs=[pdf_upload], outputs=[show_img]) # Corrected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
+
# txt = gr.Textbox(label="Enter your query", placeholder="Ask a question...") # Add Textbox
|
| 132 |
+
# submit_btn = gr.Button('Submit') # Added the Submit button
|
| 133 |
+
# submit_btn.click(
|
| 134 |
+
# fn=add_text,
|
| 135 |
+
# inputs=[chatbot, txt], # Assuming 'txt' is your textbox for query input
|
| 136 |
+
# outputs=[chatbot],
|
| 137 |
+
# queue=False
|
| 138 |
+
# ).success(
|
| 139 |
+
# fn=generate_response,
|
| 140 |
+
# inputs=[chatbot, txt, pdf_upload], # Changed from 'btn'
|
| 141 |
+
# outputs=[chatbot, txt]
|
| 142 |
+
# ).success(
|
| 143 |
+
# fn=render_file,
|
| 144 |
+
# inputs=[pdf_upload], # Changed from 'btn'
|
| 145 |
+
# outputs=[show_img]
|
| 146 |
+
# )
|
| 147 |
+
|
| 148 |
+
# demo.launch(server_port=7861)
|
| 149 |
+
|
| 150 |
+
st.title("PDF-Powered Chatbot") # Add a title
|
| 151 |
+
|
| 152 |
+
# Gradio interface with Streamlit containers
|
| 153 |
+
with st.container():
|
| 154 |
+
gr.Markdown("""
|
| 155 |
+
<style>
|
| 156 |
+
.image-container { height: 680px; }
|
| 157 |
+
</style>
|
| 158 |
+
""")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
| 161 |
+
show_label=False, interactive=True)
|
| 162 |
+
disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
| 163 |
+
change_api_key = gr.Button('Change Key')
|
| 164 |
+
with gr.Row():
|
| 165 |
+
chatbot = gr.Chatbot(value=[], elem_id='chatbot')
|
| 166 |
+
show_img = gr.Image(label='Upload PDF')
|
| 167 |
+
pdf_upload = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
|
| 168 |
+
|
| 169 |
+
# Event handlers (same as before)
|
| 170 |
+
# ... your event handlers ...
|
| 171 |
+
|
| 172 |
+
# If you only want a Gradio interface, launch Gradio
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
gr.Interface(
|
| 175 |
+
[render_first, add_text, generate_response, render_file],
|
| 176 |
+
[pdf_upload, chatbot, txt, pdf_upload, pdf_upload],
|
| 177 |
+
[show_img, chatbot, txt, show_img],
|
| 178 |
+
title="PDF-Powered Chatbot",
|
| 179 |
+
).launch()
|