Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,32 +2,26 @@ 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):
|
@@ -38,61 +32,54 @@ def add_text(history, text):
|
|
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 |
-
|
52 |
-
|
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 |
-
|
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 |
-
|
92 |
-
|
93 |
-
|
94 |
-
interactive=True
|
95 |
-
).style(container=False)
|
96 |
with gr.Column(scale=0.2):
|
97 |
change_api_key = gr.Button('Change Key')
|
98 |
|
|
|
2 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
3 |
from langchain.text_splitter import CharacterTextSplitter
|
4 |
from langchain.vectorstores import Chroma
|
|
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
from langchain.chat_models import ChatOpenAI
|
|
|
7 |
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):
|
|
|
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 |
with gr.Row():
|
79 |
with gr.Column(scale=0.8):
|
80 |
+
enable_box = gr.Textbox(placeholder='Enter OpenAI API key',
|
81 |
+
show_label=False, interactive=True).style(container=False)
|
82 |
+
disable_box = gr.Textbox(value='OpenAI API key is Set', interactive=False)
|
|
|
|
|
83 |
with gr.Column(scale=0.2):
|
84 |
change_api_key = gr.Button('Change Key')
|
85 |
|