Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,17 @@
|
|
1 |
-
|
2 |
from typing import Any
|
3 |
import gradio as gr
|
4 |
from langchain_openai import OpenAIEmbeddings
|
5 |
from langchain_community.vectorstores import Chroma
|
6 |
-
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
from langchain_openai import ChatOpenAI
|
9 |
-
|
10 |
from langchain_community.document_loaders import PyMuPDFLoader
|
11 |
-
|
12 |
import fitz
|
13 |
from PIL import Image
|
14 |
import os
|
15 |
import re
|
16 |
import openai
|
17 |
|
18 |
-
openai.api_key = "sk-
|
19 |
-
|
20 |
|
21 |
def add_text(history, text: str):
|
22 |
if not text:
|
@@ -24,7 +19,6 @@ def add_text(history, text: str):
|
|
24 |
history = history + [(text, "")]
|
25 |
return history
|
26 |
|
27 |
-
|
28 |
class MyApp:
|
29 |
def __init__(self) -> None:
|
30 |
self.OPENAI_API_KEY: str = openai.api_key
|
@@ -48,12 +42,10 @@ class MyApp:
|
|
48 |
file_name = match.group(1)
|
49 |
except:
|
50 |
file_name = os.path.basename(file)
|
51 |
-
|
52 |
return documents, file_name
|
53 |
|
54 |
def build_chain(self, file: str):
|
55 |
documents, file_name = self.process_file(file)
|
56 |
-
# Load embeddings model
|
57 |
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
58 |
pdfsearch = Chroma.from_documents(
|
59 |
documents,
|
@@ -67,7 +59,6 @@ class MyApp:
|
|
67 |
)
|
68 |
return chain
|
69 |
|
70 |
-
|
71 |
def get_response(history, query, file):
|
72 |
if not file:
|
73 |
raise gr.Error(message="Upload a PDF")
|
@@ -77,34 +68,30 @@ def get_response(history, query, file):
|
|
77 |
)
|
78 |
app.chat_history += [(query, result["answer"])]
|
79 |
app.N = list(result["source_documents"][0])[1][1]["page"]
|
|
|
80 |
for char in result["answer"]:
|
81 |
history[-1][-1] += char
|
82 |
-
yield history, ""
|
83 |
-
|
84 |
|
85 |
def render_file(file):
|
86 |
doc = fitz.open(file.name)
|
87 |
page = doc[app.N]
|
88 |
-
# Render the page as a PNG image with a resolution of 150 DPI
|
89 |
pix = page.get_pixmap(dpi=150)
|
90 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
91 |
return image
|
92 |
|
93 |
-
|
94 |
def purge_chat_and_render_first(file):
|
95 |
-
print("purge_chat_and_render_first")
|
96 |
-
# Purges the previous chat session so that the bot has no concept of previous documents
|
97 |
app.chat_history = []
|
98 |
app.count = 0
|
99 |
-
|
100 |
-
# Use PyMuPDF to render the first page of the uploaded document
|
101 |
doc = fitz.open(file.name)
|
102 |
page = doc[0]
|
103 |
-
# Render the page as a PNG image with a resolution of 150 DPI
|
104 |
pix = page.get_pixmap(dpi=150)
|
105 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
106 |
return image, []
|
107 |
|
|
|
|
|
|
|
108 |
|
109 |
app = MyApp()
|
110 |
|
@@ -112,21 +99,18 @@ with gr.Blocks() as demo:
|
|
112 |
with gr.Column():
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=2):
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
submit_btn = gr.Button("Submit", scale=1)
|
124 |
|
125 |
with gr.Column(scale=1):
|
126 |
-
|
127 |
-
|
128 |
-
with gr.Row():
|
129 |
-
btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
|
130 |
|
131 |
btn.upload(
|
132 |
fn=purge_chat_and_render_first,
|
@@ -147,7 +131,11 @@ with gr.Blocks() as demo:
|
|
147 |
fn=render_file, inputs=[btn], outputs=[show_img]
|
148 |
)
|
149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
demo.queue()
|
151 |
demo.launch()
|
152 |
-
|
153 |
-
|
|
|
|
|
1 |
from typing import Any
|
2 |
import gradio as gr
|
3 |
from langchain_openai import OpenAIEmbeddings
|
4 |
from langchain_community.vectorstores import Chroma
|
|
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
from langchain_openai import ChatOpenAI
|
|
|
7 |
from langchain_community.document_loaders import PyMuPDFLoader
|
|
|
8 |
import fitz
|
9 |
from PIL import Image
|
10 |
import os
|
11 |
import re
|
12 |
import openai
|
13 |
|
14 |
+
openai.api_key = "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"
|
|
|
15 |
|
16 |
def add_text(history, text: str):
|
17 |
if not text:
|
|
|
19 |
history = history + [(text, "")]
|
20 |
return history
|
21 |
|
|
|
22 |
class MyApp:
|
23 |
def __init__(self) -> None:
|
24 |
self.OPENAI_API_KEY: str = openai.api_key
|
|
|
42 |
file_name = match.group(1)
|
43 |
except:
|
44 |
file_name = os.path.basename(file)
|
|
|
45 |
return documents, file_name
|
46 |
|
47 |
def build_chain(self, file: str):
|
48 |
documents, file_name = self.process_file(file)
|
|
|
49 |
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
50 |
pdfsearch = Chroma.from_documents(
|
51 |
documents,
|
|
|
59 |
)
|
60 |
return chain
|
61 |
|
|
|
62 |
def get_response(history, query, file):
|
63 |
if not file:
|
64 |
raise gr.Error(message="Upload a PDF")
|
|
|
68 |
)
|
69 |
app.chat_history += [(query, result["answer"])]
|
70 |
app.N = list(result["source_documents"][0])[1][1]["page"]
|
71 |
+
highlighted_line = result["answer"]
|
72 |
for char in result["answer"]:
|
73 |
history[-1][-1] += char
|
74 |
+
yield history, "", f"Page: {app.N + 1}, Highlight: {highlighted_line}"
|
|
|
75 |
|
76 |
def render_file(file):
|
77 |
doc = fitz.open(file.name)
|
78 |
page = doc[app.N]
|
|
|
79 |
pix = page.get_pixmap(dpi=150)
|
80 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
81 |
return image
|
82 |
|
|
|
83 |
def purge_chat_and_render_first(file):
|
|
|
|
|
84 |
app.chat_history = []
|
85 |
app.count = 0
|
|
|
|
|
86 |
doc = fitz.open(file.name)
|
87 |
page = doc[0]
|
|
|
88 |
pix = page.get_pixmap(dpi=150)
|
89 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
90 |
return image, []
|
91 |
|
92 |
+
def refresh_chat():
|
93 |
+
app.chat_history = []
|
94 |
+
return []
|
95 |
|
96 |
app = MyApp()
|
97 |
|
|
|
99 |
with gr.Column():
|
100 |
with gr.Row():
|
101 |
with gr.Column(scale=2):
|
102 |
+
chatbot = gr.Chatbot(value=[], elem_id="chatbot")
|
103 |
+
txt = gr.Textbox(
|
104 |
+
show_label=False,
|
105 |
+
placeholder="Enter text and press submit",
|
106 |
+
scale=2
|
107 |
+
)
|
108 |
+
submit_btn = gr.Button("Submit", scale=1)
|
109 |
+
refresh_btn = gr.Button("Refresh Chat", scale=1)
|
|
|
110 |
|
111 |
with gr.Column(scale=1):
|
112 |
+
show_img = gr.Image(label="Upload PDF")
|
113 |
+
btn = gr.UploadButton("📁 Upload a PDF", file_types=[".pdf"])
|
|
|
|
|
114 |
|
115 |
btn.upload(
|
116 |
fn=purge_chat_and_render_first,
|
|
|
131 |
fn=render_file, inputs=[btn], outputs=[show_img]
|
132 |
)
|
133 |
|
134 |
+
refresh_btn.click(
|
135 |
+
fn=refresh_chat,
|
136 |
+
inputs=[],
|
137 |
+
outputs=[chatbot],
|
138 |
+
)
|
139 |
+
|
140 |
demo.queue()
|
141 |
demo.launch()
|
|
|
|