citation-interpreter / backend /pdf_component.py
mmrech's picture
Upload folder using huggingface_hub
9c6c358 verified
from __future__ import annotations
from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING
from gradio.components.base import Component
from gradio.data_classes import FileData
from gradio.events import Events, EventListener
if TYPE_CHECKING:
from gradio.components import Timer
class PDF(Component):
"""
PDF component for displaying PDF files and enabling citation analysis.
This component renders PDF documents with page navigation controls and
supports integration with Anthropic's Citations API. It allows users
to view PDFs, navigate through pages, and interact with citations.
"""
EVENTS = [
Events.change,
Events.upload,
EventListener("cite", doc="Triggered when a citation is selected or interacted with.")
]
data_model = FileData
def __init__(
self,
value: Optional[str | FileData] = None,
*,
height: Optional[int] = None,
label: Optional[str] = None,
info: Optional[str] = None,
show_label: Optional[bool] = None,
container: bool = True,
scale: Optional[int] = None,
min_width: Optional[int] = None,
interactive: Optional[bool] = None,
visible: bool = True,
elem_id: Optional[str] = None,
elem_classes: Optional[List[str] | str] = None,
render: bool = True,
load_fn: Optional[Callable[..., Any]] = None,
every: Optional[Timer | float] = None
):
"""
Parameters:
value: Initial path to PDF file or FileData object
height: Height of the PDF viewer in pixels
label: Component label displayed to user
info: Additional information about the component
show_label: Whether to display the label
container: Whether to display the component in a container
scale: Relative width compared to adjacent Components
min_width: Minimum width in pixels
interactive: Whether the Component is interactive
visible: Whether the Component is visible
elem_id: HTML id for the Component
elem_classes: CSS classes for the Component
render: Whether to render the Component
load_fn: Function to call when Component loads
every: How frequently to call load_fn
"""
super().__init__(
value,
label=label,
info=info,
show_label=show_label,
container=container,
scale=scale,
min_width=min_width,
interactive=interactive,
visible=visible,
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
load_fn=load_fn,
every=every
)
self.height = height
def preprocess(self, payload: Optional[FileData]) -> Optional[str]:
"""
Process the uploaded PDF file data.
Returns:
Path to the uploaded PDF file or None if no file was uploaded
"""
return payload.path if payload else None
def postprocess(self, value: Optional[str]) -> Optional[FileData]:
"""
Process the PDF file path into a FileData object.
Parameters:
value: Path to the PDF file or None
Returns:
FileData object containing the path to the PDF file or None if path is None
"""
if not value:
return None
return FileData(path=value)
def example_inputs(self) -> Any:
"""
Provides example inputs for documentation and testing.
Returns:
Example FileData for documentation
"""
return FileData(path="example.pdf")
def example_payload(self) -> str:
"""Example payload for documentation.
Returns:
URL to an example PDF file
"""
return "https://cdn.anthropic.com/claude/example.pdf"
def example_value(self) -> str:
"""Example value for documentation.
Returns:
URL to an example PDF file
"""
return "https://cdn.anthropic.com/claude/example.pdf"