Spaces:
Runtime error
Runtime error
Commit
Β·
8a53096
1
Parent(s):
f6bf7e0
updated app.py to use classes
Browse files
app.py
CHANGED
@@ -12,18 +12,16 @@ import os
|
|
12 |
import fitz
|
13 |
from PIL import Image
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def enable_api_box():
|
26 |
-
return enable_box
|
27 |
|
28 |
def add_text(history, text):
|
29 |
if not text:
|
@@ -31,59 +29,75 @@ def add_text(history, text):
|
|
31 |
history = history + [(text,'')]
|
32 |
return history
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
global COUNT, N, chat_history, chain
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
history[-1][-1] += char
|
65 |
-
yield history,''
|
66 |
-
|
67 |
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
with gr.Blocks() as demo:
|
89 |
|
@@ -97,27 +111,30 @@ with gr.Blocks() as demo:
|
|
97 |
chatbot = gr.Chatbot(value=[], elem_id='chatbot').style(height=650)
|
98 |
show_img = gr.Image(label='Upload PDF', tool='select' ).style(height=680)
|
99 |
with gr.Row():
|
100 |
-
with gr.Column(scale=0.
|
101 |
txt = gr.Textbox(
|
102 |
show_label=False,
|
103 |
placeholder="Enter text and press enter",
|
104 |
).style(container=False)
|
105 |
-
with gr.Column(scale=0.
|
106 |
submit_btn = gr.Button('submit')
|
107 |
-
with gr.Column(scale=0.
|
108 |
btn = gr.UploadButton("π upload a PDF", file_types=[".pdf"]).style()
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
|
|
|
|
116 |
|
117 |
-
txt.submit(fn=add_text, inputs=[chatbot,txt], outputs=[chatbot, ], queue=False).success(fn=generate_response,inputs = [chatbot, txt, btn],
|
118 |
-
outputs = [chatbot,txt]).success(fn=render_file,inputs = [btn], outputs=[show_img])
|
119 |
|
120 |
|
121 |
-
demo.queue()
|
122 |
-
if __name__ == "__main__":
|
123 |
-
demo.launch()
|
|
|
12 |
import fitz
|
13 |
from PIL import Image
|
14 |
|
15 |
+
from functools import lru_cache
|
16 |
+
import json
|
17 |
+
import uuid
|
18 |
+
import re
|
19 |
+
import chromadb
|
20 |
+
from chromadb.config import Settings
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
|
|
|
|
25 |
|
26 |
def add_text(history, text):
|
27 |
if not text:
|
|
|
29 |
history = history + [(text,'')]
|
30 |
return history
|
31 |
|
32 |
+
class my_app:
|
33 |
+
def __init__(self, OPENAI_API_KEY: None ) -> None:
|
34 |
+
self.OPENAI_API_KEY = OPENAI_API_KEY
|
35 |
+
self.chain = None
|
36 |
+
self.chat_history = []
|
37 |
+
self.N = 0
|
38 |
+
self.enable_box = gr.Textbox.update(value=None,placeholder= 'Upload your OpenAI API key',interactive=True)
|
39 |
+
self.disable_box = gr.Textbox.update(value = 'OpenAI API key is Set',interactive=False)
|
40 |
+
|
41 |
+
def set_apikey(self,api_key):
|
42 |
+
self.OPENAI_API_KEY = api_key
|
43 |
+
return self.disable_box
|
44 |
|
45 |
+
def enable_api_box(self):
|
46 |
+
return self.enable_box
|
47 |
+
|
48 |
+
def chroma_client(self):
|
49 |
+
#create a chroma client
|
50 |
+
client = chromadb.Client()
|
51 |
+
#create a collecyion
|
52 |
+
collection = client.get_or_create_collection(name="my-collection")
|
53 |
|
54 |
+
return client
|
|
|
55 |
|
56 |
+
def process_file(self,file):
|
57 |
+
|
58 |
+
loader = PyPDFLoader(file.name)
|
59 |
+
documents = loader.load()
|
60 |
+
pattern = r"/([^/]+)$"
|
61 |
+
match = re.search(pattern, file.name)
|
62 |
+
file_name = match.group(1)
|
63 |
+
return documents, file_name
|
64 |
|
65 |
+
def build_chain(self, file):
|
66 |
+
documents, file_name = self.process_file(file)
|
67 |
+
#Load embeddings model
|
68 |
+
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
69 |
+
pdfsearch = Chroma.from_documents(documents, embeddings, collection_name= file_name,)
|
|
|
|
|
|
|
70 |
|
71 |
+
self.chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.0, openai_api_key=self.OPENAI_API_KEY),
|
72 |
+
retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
73 |
+
return_source_documents=True,)
|
74 |
|
75 |
+
def get_response(self, history, query, file):
|
76 |
+
|
77 |
+
if not file:
|
78 |
+
raise gr.Error(message='Upload a PDF')
|
79 |
+
|
80 |
+
if not self.chain:
|
81 |
+
self.build_chain(file)
|
82 |
+
|
83 |
+
result = self.chain({"question": query, 'chat_history':self.chat_history},return_only_outputs=True)
|
84 |
+
self.chat_history += [(query, result["answer"])]
|
85 |
+
self.N = list(result['source_documents'][0])[1][1]['page']
|
86 |
+
|
87 |
+
for char in result['answer']:
|
88 |
+
history[-1][-1] += char
|
89 |
+
yield history,''
|
90 |
+
|
91 |
+
def render_file(self,file):
|
92 |
+
|
93 |
+
doc = fitz.open(file.name)
|
94 |
+
page = doc[self.N]
|
95 |
+
#Render the page as a PNG image with a resolution of 300 DPI
|
96 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
97 |
+
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
98 |
+
return image
|
99 |
+
|
100 |
+
app = my_app()
|
101 |
|
102 |
with gr.Blocks() as demo:
|
103 |
|
|
|
111 |
chatbot = gr.Chatbot(value=[], elem_id='chatbot').style(height=650)
|
112 |
show_img = gr.Image(label='Upload PDF', tool='select' ).style(height=680)
|
113 |
with gr.Row():
|
114 |
+
with gr.Column(scale=0.60):
|
115 |
txt = gr.Textbox(
|
116 |
show_label=False,
|
117 |
placeholder="Enter text and press enter",
|
118 |
).style(container=False)
|
119 |
+
with gr.Column(scale=0.20):
|
120 |
submit_btn = gr.Button('submit')
|
121 |
+
with gr.Column(scale=0.20):
|
122 |
btn = gr.UploadButton("π upload a PDF", file_types=[".pdf"]).style()
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
api_key.submit(fn=app.set_apikey, inputs=[api_key], outputs=[api_key])
|
127 |
+
change_api_key.click(fn= app.enable_api_box,outputs=[api_key])
|
128 |
+
btn.upload(fn=app.render_file, inputs=[btn], outputs=[show_img],)
|
129 |
|
130 |
+
submit_btn.click(fn=add_text, inputs=[chatbot,txt], outputs=[chatbot, ], queue=False).success(fn=app.get_response,inputs = [chatbot, txt, btn],
|
131 |
+
outputs = [chatbot,txt]).success(fn=app.render_file,inputs = [btn], outputs=[show_img])
|
132 |
+
|
133 |
|
134 |
+
demo.queue()
|
135 |
+
demo.launch()
|
136 |
+
|
137 |
+
|
138 |
|
|
|
|
|
139 |
|
140 |
|
|
|
|
|
|