Spaces:
Runtime error
Runtime error
YosrAbbassi
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import fitz
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
class PDFViewer:
|
6 |
+
def __init__(self, pdf_path):
|
7 |
+
self.doc = fitz.open(pdf_path)
|
8 |
+
self.page_num = 0
|
9 |
+
self.selected_area = None
|
10 |
+
|
11 |
+
def display_page(self, page_num):
|
12 |
+
self.page_num = page_num
|
13 |
+
page = self.doc[self.page_num]
|
14 |
+
pix = page.get_pixmap()
|
15 |
+
return Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
16 |
+
|
17 |
+
def navigate_page(self, direction):
|
18 |
+
if direction == "next":
|
19 |
+
self.page_num += 1
|
20 |
+
elif direction == "previous":
|
21 |
+
self.page_num -= 1
|
22 |
+
self.page_num = max(0, min(self.page_num, len(self.doc) - 1))
|
23 |
+
return self.display_page(self.page_num)
|
24 |
+
|
25 |
+
def select_area(self, start_x, start_y, end_x, end_y):
|
26 |
+
selected_area = fitz.Rect(start_x, start_y, end_x, end_y)
|
27 |
+
selected_pixmap = self.doc[self.page_num].get_pixmap(matrix=fitz.Matrix(1, 1), clip=selected_area)
|
28 |
+
self.selected_area = Image.frombytes("RGB", [selected_pixmap.width, selected_pixmap.height], selected_pixmap.samples)
|
29 |
+
return "Area selected"
|
30 |
+
|
31 |
+
def main(pdf_file, direction, start_x=None, start_y=None, end_x=None, end_y=None):
|
32 |
+
pdf_path = pdf_file.name
|
33 |
+
if pdf_path:
|
34 |
+
pdf_viewer = PDFViewer(pdf_path)
|
35 |
+
if start_x is not None and start_y is not None and end_x is not None and end_y is not None:
|
36 |
+
return pdf_viewer.select_area(start_x, start_y, end_x, end_y)
|
37 |
+
else:
|
38 |
+
return pdf_viewer.navigate_page(direction)
|
39 |
+
|
40 |
+
pdf_file = gr.inputs.File(label="Select a PDF file")
|
41 |
+
direction = gr.inputs.Radio(["previous", "next"], label="Navigation")
|
42 |
+
coordinates = [gr.inputs.Number(label="Start X"), gr.inputs.Number(label="Start Y"),
|
43 |
+
gr.inputs.Number(label="End X"), gr.inputs.Number(label="End Y")]
|
44 |
+
|
45 |
+
gr.Interface(fn=main, inputs=[pdf_file, direction, *coordinates], outputs=["image", "text"]).launch()
|