Spaces:
Runtime error
Runtime error
Commit
Β·
dc483b5
1
Parent(s):
8fcee46
added state in app.py
Browse files
app.py
CHANGED
@@ -13,13 +13,19 @@ from PIL import Image
|
|
13 |
|
14 |
import chromadb
|
15 |
import re
|
|
|
16 |
|
17 |
enable_box = gr.Textbox.update(value=None,placeholder= 'Upload your OpenAI API key',interactive=True)
|
18 |
disable_box = gr.Textbox.update(value = 'OpenAI API key is Set',interactive=False)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
return disable_box
|
24 |
|
25 |
def enable_api_box():
|
@@ -76,12 +82,21 @@ class my_app:
|
|
76 |
return_source_documents=True,)
|
77 |
return self.chain
|
78 |
|
79 |
-
|
80 |
-
|
|
|
81 |
if not file:
|
82 |
raise gr.Error(message='Upload a PDF')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
chain = app(file)
|
|
|
85 |
result = chain({"question": query, 'chat_history':app.chat_history},return_only_outputs=True)
|
86 |
app.chat_history += [(query, result["answer"])]
|
87 |
app.N = list(result['source_documents'][0])[1][1]['page']
|
@@ -90,18 +105,30 @@ def get_response(history, query, file):
|
|
90 |
history[-1][-1] += char
|
91 |
yield history,''
|
92 |
|
93 |
-
def render_file(file):
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
doc = fitz.open(file.name)
|
96 |
page = doc[app.N]
|
97 |
#Render the page as a PNG image with a resolution of 300 DPI
|
98 |
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
99 |
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
100 |
return image
|
101 |
-
|
102 |
-
app = my_app()
|
103 |
-
with gr.Blocks() as demo:
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
with gr.Column():
|
106 |
with gr.Row():
|
107 |
with gr.Column(scale=0.8):
|
@@ -123,12 +150,12 @@ with gr.Blocks() as demo:
|
|
123 |
btn = gr.UploadButton("π upload a PDF", file_types=[".pdf"]).style()
|
124 |
|
125 |
|
126 |
-
api_key.submit(fn=set_apikey, inputs=[api_key], outputs=[api_key])
|
127 |
change_api_key.click(fn= enable_api_box,outputs=[api_key])
|
128 |
-
btn.upload(fn=
|
129 |
|
130 |
-
submit_btn.click(fn=add_text, inputs=[chatbot,txt], outputs=[chatbot, ], queue=False).success(fn=get_response,inputs = [chatbot, txt, btn],
|
131 |
-
outputs = [chatbot,txt]).success(fn=render_file,inputs = [btn], outputs=[show_img])
|
132 |
|
133 |
|
134 |
demo.queue()
|
|
|
13 |
|
14 |
import chromadb
|
15 |
import re
|
16 |
+
import uuid
|
17 |
|
18 |
enable_box = gr.Textbox.update(value=None,placeholder= 'Upload your OpenAI API key',interactive=True)
|
19 |
disable_box = gr.Textbox.update(value = 'OpenAI API key is Set',interactive=False)
|
20 |
|
21 |
+
user_app = {}
|
22 |
+
user_api = {}
|
23 |
+
|
24 |
+
def set_apikey(state, api_key):
|
25 |
+
|
26 |
+
if state not in user_app:
|
27 |
+
user_api[state] = api_key
|
28 |
+
|
29 |
return disable_box
|
30 |
|
31 |
def enable_api_box():
|
|
|
82 |
return_source_documents=True,)
|
83 |
return self.chain
|
84 |
|
85 |
+
|
86 |
+
def get_response(state, history, query, file):
|
87 |
+
|
88 |
if not file:
|
89 |
raise gr.Error(message='Upload a PDF')
|
90 |
+
if state not in user_app:
|
91 |
+
app = my_app()
|
92 |
+
user_app[state] = app
|
93 |
+
else:
|
94 |
+
app = user_app[state]
|
95 |
+
|
96 |
+
app.OPENAI_API_KEY = user_api[state]
|
97 |
|
98 |
chain = app(file)
|
99 |
+
|
100 |
result = chain({"question": query, 'chat_history':app.chat_history},return_only_outputs=True)
|
101 |
app.chat_history += [(query, result["answer"])]
|
102 |
app.N = list(result['source_documents'][0])[1][1]['page']
|
|
|
105 |
history[-1][-1] += char
|
106 |
yield history,''
|
107 |
|
108 |
+
def render_file(state,file):
|
109 |
+
if state not in user_app:
|
110 |
+
app = my_app()
|
111 |
+
user_app[state] = app
|
112 |
+
else:
|
113 |
+
app = user_app[state]
|
114 |
doc = fitz.open(file.name)
|
115 |
page = doc[app.N]
|
116 |
#Render the page as a PNG image with a resolution of 300 DPI
|
117 |
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
118 |
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
119 |
return image
|
|
|
|
|
|
|
120 |
|
121 |
+
def render_first(file):
|
122 |
+
doc = fitz.open(file.name)
|
123 |
+
page = doc[0]
|
124 |
+
#Render the page as a PNG image with a resolution of 300 DPI
|
125 |
+
pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
126 |
+
image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
127 |
+
return image,[]
|
128 |
+
|
129 |
+
|
130 |
+
with gr.Blocks() as demo:
|
131 |
+
state = gr.State(uuid.uuid4().hex)
|
132 |
with gr.Column():
|
133 |
with gr.Row():
|
134 |
with gr.Column(scale=0.8):
|
|
|
150 |
btn = gr.UploadButton("π upload a PDF", file_types=[".pdf"]).style()
|
151 |
|
152 |
|
153 |
+
api_key.submit(fn=set_apikey, inputs=[state, api_key], outputs=[api_key,])
|
154 |
change_api_key.click(fn= enable_api_box,outputs=[api_key])
|
155 |
+
btn.upload(fn=render_first, inputs=[btn], outputs=[show_img,chatbot],)
|
156 |
|
157 |
+
submit_btn.click(fn=add_text, inputs=[chatbot,txt], outputs=[chatbot, ], queue=False).success(fn=get_response,inputs = [state,chatbot, txt, btn],
|
158 |
+
outputs = [chatbot,txt]).success(fn=render_file,inputs = [state,btn], outputs=[show_img])
|
159 |
|
160 |
|
161 |
demo.queue()
|