Spaces:
Runtime error
Runtime error
YosrAbbassi
commited on
Commit
•
f4de44c
1
Parent(s):
820c10f
Rename app1.py to app.py
Browse files- app1.py → app.py +150 -146
app1.py → app.py
RENAMED
@@ -1,147 +1,151 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
self.
|
16 |
-
|
17 |
-
|
18 |
-
#
|
19 |
-
self.
|
20 |
-
self.
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
self.
|
25 |
-
self.
|
26 |
-
|
27 |
-
#
|
28 |
-
self.
|
29 |
-
self.
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
self.
|
48 |
-
self.
|
49 |
-
|
50 |
-
#
|
51 |
-
self.
|
52 |
-
self.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
self.
|
57 |
-
|
58 |
-
|
59 |
-
#
|
60 |
-
self.
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
self.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
self.
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
def
|
80 |
-
#
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
self.
|
92 |
-
|
93 |
-
|
94 |
-
#
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
#
|
99 |
-
self.rect
|
100 |
-
|
101 |
-
|
102 |
-
#
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
147 |
interface.launch()
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
# Install specific version of Gradio
|
4 |
+
subprocess.run(["pip", "install", "gradio==1.7.7"])
|
5 |
+
import gradio as gr
|
6 |
+
import fitz
|
7 |
+
import tkinter as tk
|
8 |
+
from tkinter import filedialog
|
9 |
+
from PIL import Image, ImageTk
|
10 |
+
|
11 |
+
class PDFViewer:
|
12 |
+
def __init__(self, pdf_path):
|
13 |
+
self.doc = fitz.open(pdf_path)
|
14 |
+
self.page = self.doc[0] # Assuming you want to work with the first page
|
15 |
+
self.page_num=0
|
16 |
+
|
17 |
+
|
18 |
+
# Get the size of the first page
|
19 |
+
self.page_width = int(self.page.rect.width)
|
20 |
+
self.page_height = int(self.page.rect.height)
|
21 |
+
|
22 |
+
# Create a Tkinter window
|
23 |
+
self.root = tk.Tk()
|
24 |
+
self.root.title("PDF Viewer")
|
25 |
+
self.root.attributes("-topmost", True) # Put the window at the top
|
26 |
+
|
27 |
+
# Create a canvas to display the PDF page
|
28 |
+
self.canvas = tk.Canvas(self.root, width=self.page_width, height=self.page_height)
|
29 |
+
self.canvas.pack()
|
30 |
+
|
31 |
+
# Initialize scrollbar
|
32 |
+
self.scrollbar = tk.Scrollbar(self.root, orient="vertical", command=self.on_scroll)
|
33 |
+
self.scrollbar.pack(side="right", fill="y")
|
34 |
+
|
35 |
+
self.canvas.configure(yscrollcommand=self.scrollbar.set)
|
36 |
+
|
37 |
+
# Display the first page
|
38 |
+
self.display_page()
|
39 |
+
|
40 |
+
# Bind mouse wheel event for scrolling
|
41 |
+
self.canvas.bind("<MouseWheel>", self.on_mousewheel)
|
42 |
+
|
43 |
+
|
44 |
+
# Display the PDF page on the canvas
|
45 |
+
pix = self.page.get_pixmap(matrix=fitz.Matrix(1, 1))
|
46 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
47 |
+
self.photo = ImageTk.PhotoImage(image=img)
|
48 |
+
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
|
49 |
+
|
50 |
+
# Variables to store mouse click coordinates
|
51 |
+
self.start_x = None
|
52 |
+
self.start_y = None
|
53 |
+
|
54 |
+
# Bind left mouse button click and drag events
|
55 |
+
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
|
56 |
+
self.canvas.bind("<B1-Motion>", self.on_move_press)
|
57 |
+
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
|
58 |
+
|
59 |
+
# Initialize rectangle drawn on canvas
|
60 |
+
self.rect = None
|
61 |
+
|
62 |
+
def display_page(self):
|
63 |
+
# Clear canvas
|
64 |
+
self.canvas.delete("all")
|
65 |
+
|
66 |
+
# Get the size of the page
|
67 |
+
self.page = self.doc[self.page_num]
|
68 |
+
self.page_width = int(self.page.rect.width)
|
69 |
+
self.page_height = int(self.page.rect.height)
|
70 |
+
|
71 |
+
# Display the PDF page on the canvas
|
72 |
+
pix = self.page.get_pixmap()
|
73 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
74 |
+
self.photo = ImageTk.PhotoImage(image=img)
|
75 |
+
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
|
76 |
+
|
77 |
+
# Update scrollbar
|
78 |
+
self.scrollbar.config(command=self.canvas.yview)
|
79 |
+
def on_scroll(self, *args):
|
80 |
+
# Update canvas view when scrollbar is moved
|
81 |
+
self.canvas.yview(*args)
|
82 |
+
|
83 |
+
def on_mousewheel(self, event):
|
84 |
+
# Scroll up/down when mouse wheel is moved
|
85 |
+
if event.delta < 0:
|
86 |
+
self.page_num += 1
|
87 |
+
else:
|
88 |
+
self.page_num -= 1
|
89 |
+
|
90 |
+
self.page_num = max(0, min(self.page_num, len(self.doc) - 1))
|
91 |
+
self.display_page()
|
92 |
+
|
93 |
+
def on_button_press(self, event):
|
94 |
+
# Record the starting point of the selection
|
95 |
+
self.start_x = self.canvas.canvasx(event.x)
|
96 |
+
self.start_y = self.canvas.canvasy(event.y)
|
97 |
+
|
98 |
+
# Delete any previously drawn rectangle
|
99 |
+
if self.rect:
|
100 |
+
self.canvas.delete(self.rect)
|
101 |
+
|
102 |
+
# Draw a new rectangle starting from the clicked point
|
103 |
+
self.rect = self.canvas.create_rectangle(self.start_x, self.start_y, self.start_x, self.start_y, outline='red')
|
104 |
+
|
105 |
+
def on_move_press(self, event):
|
106 |
+
# Update the size of the rectangle as the mouse moves
|
107 |
+
cur_x = self.canvas.canvasx(event.x)
|
108 |
+
cur_y = self.canvas.canvasy(event.y)
|
109 |
+
|
110 |
+
self.canvas.coords(self.rect, self.start_x, self.start_y, cur_x, cur_y)
|
111 |
+
|
112 |
+
def on_button_release(self, event):
|
113 |
+
# Save the selected area as an image
|
114 |
+
x1 = min(self.start_x, self.canvas.canvasx(event.x))
|
115 |
+
y1 = min(self.start_y, self.canvas.canvasy(event.y))
|
116 |
+
x2 = max(self.start_x, self.canvas.canvasx(event.x))
|
117 |
+
y2 = max(self.start_y, self.canvas.canvasy(event.y))
|
118 |
+
|
119 |
+
selected_area = fitz.Rect(x1, y1, x2, y2)
|
120 |
+
selected_pixmap = self.page.get_pixmap(matrix=fitz.Matrix(1, 1), clip=selected_area)
|
121 |
+
|
122 |
+
# Convert Pixmap to PIL Image
|
123 |
+
img = Image.frombytes("RGB", [selected_pixmap.width, selected_pixmap.height], selected_pixmap.samples)
|
124 |
+
|
125 |
+
# Save the selected area as an image
|
126 |
+
save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
|
127 |
+
if save_path:
|
128 |
+
img.save(save_path)
|
129 |
+
|
130 |
+
# Destroy the Tkinter window
|
131 |
+
self.root.destroy()
|
132 |
+
|
133 |
+
# Define the function to be called when the PDF file path is provided
|
134 |
+
def main(pdf_file):
|
135 |
+
# Ask user to select a PDF file
|
136 |
+
pdf_path = pdf_file.name
|
137 |
+
if pdf_path:
|
138 |
+
PDFViewer(pdf_path).root.mainloop()
|
139 |
+
return "File Saved"
|
140 |
+
|
141 |
+
pdf_file = gr.inputs.File(label="Select a PDF file")
|
142 |
+
|
143 |
+
# Create the Gradio interface
|
144 |
+
interface = gr.Interface(
|
145 |
+
fn=main,
|
146 |
+
inputs=pdf_file,
|
147 |
+
outputs="text",
|
148 |
+
title="PDF Region Extraction",
|
149 |
+
description="Select a region from a PDF file to extract.",
|
150 |
+
)
|
151 |
interface.launch()
|