Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,23 +11,67 @@ if not os.path.isdir("low_partners_chatbot_data"):
|
|
11 |
from low_partners_chatbot_data.utils import run_conversation
|
12 |
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
return
|
22 |
|
23 |
|
24 |
-
# Create the Gradio interface
|
25 |
iface = gr.Interface(
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"Note: This chatbot does not implement memory module, ask specific questions and don't expect it to remember previous conversation.\n"
|
29 |
-
)
|
30 |
)
|
31 |
|
32 |
-
# Launch the app
|
33 |
iface.launch()
|
|
|
11 |
from low_partners_chatbot_data.utils import run_conversation
|
12 |
|
13 |
|
14 |
+
import gradio as gr
|
15 |
+
import fitz # PyMuPDF
|
16 |
+
from PIL import Image
|
17 |
+
import io
|
18 |
+
import os
|
19 |
+
|
20 |
+
|
21 |
+
def extract_pages_from_multiple_pdfs(pdf_info):
|
22 |
+
images = []
|
23 |
+
for pdf_path, page_numbers in pdf_info.items():
|
24 |
+
if not os.path.exists(pdf_path):
|
25 |
+
print(f"PDF file not found: {pdf_path}")
|
26 |
+
continue
|
27 |
+
|
28 |
+
try:
|
29 |
+
doc = fitz.open(pdf_path)
|
30 |
+
for page_num in page_numbers:
|
31 |
+
if 1 <= page_num <= len(doc):
|
32 |
+
page = doc.load_page(page_num - 1)
|
33 |
+
pix = page.get_pixmap(
|
34 |
+
matrix=fitz.Matrix(2, 2)
|
35 |
+
) # Increase resolution
|
36 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
37 |
+
images.append(
|
38 |
+
(img, f"{os.path.basename(pdf_path)} - Page {page_num}")
|
39 |
+
)
|
40 |
+
else:
|
41 |
+
print(f"Page {page_num} is out of range for {pdf_path}")
|
42 |
+
doc.close()
|
43 |
+
except Exception as e:
|
44 |
+
print(f"An error occurred with {pdf_path}: {str(e)}")
|
45 |
+
|
46 |
+
return images
|
47 |
+
|
48 |
|
49 |
+
def get_answer(query):
|
50 |
+
answer, pdf_info = run_conversation(query)
|
51 |
+
print(f"Answer: {answer}")
|
52 |
+
print(f"PDF Info: {pdf_info}")
|
53 |
+
images = []
|
54 |
+
if pdf_info:
|
55 |
+
images = extract_pages_from_multiple_pdfs(pdf_info)
|
56 |
|
57 |
+
if not images:
|
58 |
+
return answer, None
|
59 |
|
60 |
+
return answer, images
|
61 |
|
62 |
|
|
|
63 |
iface = gr.Interface(
|
64 |
+
fn=get_answer,
|
65 |
+
inputs="text", # No inputs needed as the function provides the information
|
66 |
+
outputs=[
|
67 |
+
gr.Textbox(label="Answer"),
|
68 |
+
gr.Gallery(label="Related Documents", allow_preview=True, preview=True),
|
69 |
+
],
|
70 |
+
title="Low & Partners Chatbot",
|
71 |
+
description=(
|
72 |
+
"This is a chatbot for Low & Partners Law Firm. Ask me anything related to law!\n\n"
|
73 |
"Note: This chatbot does not implement memory module, ask specific questions and don't expect it to remember previous conversation.\n"
|
74 |
+
),
|
75 |
)
|
76 |
|
|
|
77 |
iface.launch()
|