Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from typing import Any
|
2 |
import gradio as gr
|
3 |
from langchain_openai import OpenAIEmbeddings
|
4 |
from langchain_community.vectorstores import Chroma
|
@@ -16,7 +16,7 @@ openai.api_key = "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"
|
|
16 |
def add_text(history, text: str):
|
17 |
if not text:
|
18 |
raise gr.Error("Enter text")
|
19 |
-
history
|
20 |
return history
|
21 |
|
22 |
class MyApp:
|
@@ -24,18 +24,15 @@ class MyApp:
|
|
24 |
self.OPENAI_API_KEY: str = openai.api_key
|
25 |
self.chain = None
|
26 |
self.chat_history: list = []
|
27 |
-
self.N: int = 0
|
28 |
-
self.count: int = 0
|
29 |
self.documents = None
|
30 |
self.file_name = None
|
31 |
|
32 |
-
def __call__(self, file: str) ->
|
33 |
-
if self.
|
34 |
self.chain = self.build_chain(file)
|
35 |
-
self.count += 1
|
36 |
return self.chain
|
37 |
|
38 |
-
def process_file(self, file: str):
|
39 |
loader = PyMuPDFLoader(file.name)
|
40 |
self.documents = loader.load()
|
41 |
pattern = r"/([^/]+)$"
|
@@ -44,14 +41,13 @@ class MyApp:
|
|
44 |
self.file_name = match.group(1)
|
45 |
except:
|
46 |
self.file_name = os.path.basename(file)
|
47 |
-
# Render the first page for display
|
48 |
doc = fitz.open(file.name)
|
49 |
page = doc[0]
|
50 |
pix = page.get_pixmap(dpi=150)
|
51 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
52 |
return image
|
53 |
|
54 |
-
def build_chain(self, file: str):
|
55 |
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
56 |
pdfsearch = Chroma.from_documents(
|
57 |
self.documents,
|
@@ -72,23 +68,25 @@ def get_response(history, query, file):
|
|
72 |
result = chain(
|
73 |
{"question": query, "chat_history": app.chat_history}, return_only_outputs=True
|
74 |
)
|
75 |
-
app.chat_history
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
78 |
for char in result["answer"]:
|
79 |
history[-1][-1] += char
|
80 |
-
yield history, "",
|
81 |
|
82 |
-
def render_file(file):
|
83 |
doc = fitz.open(file.name)
|
84 |
-
page = doc[
|
85 |
pix = page.get_pixmap(dpi=150)
|
86 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
87 |
return image
|
88 |
|
89 |
-
def purge_chat_and_render_first(file):
|
90 |
app.chat_history = []
|
91 |
-
app.count = 0
|
92 |
doc = fitz.open(file.name)
|
93 |
page = doc[0]
|
94 |
pix = page.get_pixmap(dpi=150)
|
@@ -123,7 +121,7 @@ with gr.Blocks() as demo:
|
|
123 |
)
|
124 |
submit_btn = gr.Button("Submit", scale=1)
|
125 |
refresh_btn = gr.Button("Refresh Chat", scale=1)
|
126 |
-
|
127 |
|
128 |
btn.upload(
|
129 |
fn=purge_chat_and_render_first,
|
@@ -149,9 +147,7 @@ with gr.Blocks() as demo:
|
|
149 |
outputs=[chatbot],
|
150 |
queue=False,
|
151 |
).success(
|
152 |
-
fn=get_response, inputs=[chatbot, txt, btn], outputs=[chatbot, txt]
|
153 |
-
).success(
|
154 |
-
fn=render_file, inputs=[btn], outputs=[show_img_processed]
|
155 |
)
|
156 |
|
157 |
refresh_btn.click(
|
|
|
1 |
+
from typing import Any, Tuple
|
2 |
import gradio as gr
|
3 |
from langchain_openai import OpenAIEmbeddings
|
4 |
from langchain_community.vectorstores import Chroma
|
|
|
16 |
def add_text(history, text: str):
|
17 |
if not text:
|
18 |
raise gr.Error("Enter text")
|
19 |
+
history.append((text, ""))
|
20 |
return history
|
21 |
|
22 |
class MyApp:
|
|
|
24 |
self.OPENAI_API_KEY: str = openai.api_key
|
25 |
self.chain = None
|
26 |
self.chat_history: list = []
|
|
|
|
|
27 |
self.documents = None
|
28 |
self.file_name = None
|
29 |
|
30 |
+
def __call__(self, file: str) -> ConversationalRetrievalChain:
|
31 |
+
if self.chain is None:
|
32 |
self.chain = self.build_chain(file)
|
|
|
33 |
return self.chain
|
34 |
|
35 |
+
def process_file(self, file: str) -> Image:
|
36 |
loader = PyMuPDFLoader(file.name)
|
37 |
self.documents = loader.load()
|
38 |
pattern = r"/([^/]+)$"
|
|
|
41 |
self.file_name = match.group(1)
|
42 |
except:
|
43 |
self.file_name = os.path.basename(file)
|
|
|
44 |
doc = fitz.open(file.name)
|
45 |
page = doc[0]
|
46 |
pix = page.get_pixmap(dpi=150)
|
47 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
48 |
return image
|
49 |
|
50 |
+
def build_chain(self, file: str) -> str:
|
51 |
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
52 |
pdfsearch = Chroma.from_documents(
|
53 |
self.documents,
|
|
|
68 |
result = chain(
|
69 |
{"question": query, "chat_history": app.chat_history}, return_only_outputs=True
|
70 |
)
|
71 |
+
app.chat_history.append((query, result["answer"]))
|
72 |
+
source_docs = result["source_documents"]
|
73 |
+
source_texts = []
|
74 |
+
for doc in source_docs:
|
75 |
+
source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
76 |
+
source_texts_str = "\n\n".join(source_texts)
|
77 |
for char in result["answer"]:
|
78 |
history[-1][-1] += char
|
79 |
+
yield history, "", source_texts_str
|
80 |
|
81 |
+
def render_file(file) -> Image:
|
82 |
doc = fitz.open(file.name)
|
83 |
+
page = doc[0]
|
84 |
pix = page.get_pixmap(dpi=150)
|
85 |
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
86 |
return image
|
87 |
|
88 |
+
def purge_chat_and_render_first(file) -> Tuple[Image, list]:
|
89 |
app.chat_history = []
|
|
|
90 |
doc = fitz.open(file.name)
|
91 |
page = doc[0]
|
92 |
pix = page.get_pixmap(dpi=150)
|
|
|
121 |
)
|
122 |
submit_btn = gr.Button("Submit", scale=1)
|
123 |
refresh_btn = gr.Button("Refresh Chat", scale=1)
|
124 |
+
source_texts_output = gr.Textbox(label="Source Texts", interactive=False)
|
125 |
|
126 |
btn.upload(
|
127 |
fn=purge_chat_and_render_first,
|
|
|
147 |
outputs=[chatbot],
|
148 |
queue=False,
|
149 |
).success(
|
150 |
+
fn=get_response, inputs=[chatbot, txt, btn], outputs=[chatbot, txt, source_texts_output]
|
|
|
|
|
151 |
)
|
152 |
|
153 |
refresh_btn.click(
|