import subprocess # Install specific version of Gradio subprocess.run(["pip", "install", "gradio==1.7.7"]) import gradio as gr import fitz from PIL import Image class PDFViewer: def __init__(self, pdf_path): self.doc = fitz.open(pdf_path) self.page_num = 0 self.selected_area = None def display_page(self, page_num): self.page_num = page_num page = self.doc[self.page_num] pix = page.get_pixmap() return Image.frombytes("RGB", [pix.width, pix.height], pix.samples) def navigate_page(self, direction): if direction == "next": self.page_num += 1 elif direction == "previous": self.page_num -= 1 self.page_num = max(0, min(self.page_num, len(self.doc) - 1)) return self.display_page(self.page_num) def select_area(self, start_x, start_y, end_x, end_y): selected_area = fitz.Rect(start_x, start_y, end_x, end_y) selected_pixmap = self.doc[self.page_num].get_pixmap(matrix=fitz.Matrix(1, 1), clip=selected_area) self.selected_area = Image.frombytes("RGB", [selected_pixmap.width, selected_pixmap.height], selected_pixmap.samples) return "Area selected" def main(pdf_file, direction, start_x=None, start_y=None, end_x=None, end_y=None): pdf_path = pdf_file.name if pdf_path: pdf_viewer = PDFViewer(pdf_path) if start_x is not None and start_y is not None and end_x is not None and end_y is not None: return pdf_viewer.select_area(start_x, start_y, end_x, end_y) else: return pdf_viewer.navigate_page(direction) pdf_file = gr.inputs.File(label="Select a PDF file") direction = gr.inputs.Radio(["previous", "next"], label="Navigation") coordinates = [gr.inputs.Number(label="Start X"), gr.inputs.Number(label="Start Y"), gr.inputs.Number(label="End X"), gr.inputs.Number(label="End Y")] gr.Interface(fn=main, inputs=[pdf_file, direction, *coordinates], outputs=["image", "text"]).launch()