File size: 2,167 Bytes
7b19e66
 
7888f8c
7b19e66
7888f8c
 
7b19e66
e40d876
 
 
 
 
 
 
 
 
 
 
7b19e66
7888f8c
7b19e66
 
 
e40d876
 
 
 
 
 
 
 
 
 
 
 
7888f8c
 
7b19e66
7888f8c
7b19e66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7888f8c
 
7b19e66
 
 
8643c37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gradio as gr
import os
from pdfitdown.pdfconversion import Converter

converter = Converter(reader="docling")

def convert_readme_to_pdf(markdown_file) -> str:
    """
    Converts a markdown file to PDF format.

    Args:
        markdown_file: A file object representing the markdown file to be converted.
                      Expected to have a 'name' attribute containing the file path.

    Returns:
        str: Path to the generated PDF file. The output filename is created by 
             replacing the '.md' extension of the input file with '.pdf'.
    """
    output_path = markdown_file.name.replace('.md', '.pdf')
    converter.convert(markdown_file.name, output_path)
    return output_path

def convert_image_to_pdf(image_file) -> str:
    """
    Convert an image file to PDF format.

    Args:
        image_file: A file object containing the image to be converted.
                    The file must be in a format supported by the converter
                    (e.g., PNG, JPG, JPEG).

    Returns:
        str: The file path of the generated PDF file. The output filename will be
             the same as the input filename but with a .pdf extension.
    """
    output_path = image_file.name.replace(os.path.splitext(image_file.name)[1], '.pdf')
    converter.convert(image_file.name, output_path)
    return output_path
    


# Create individual interfaces
readme_to_pdf = gr.Interface(
    fn=convert_readme_to_pdf,
    inputs=gr.File(label="Upload README/Markdown file", file_types=[".md"]),
    outputs=gr.File(label="Converted PDF"),
    title="README to PDF Converter",
    description="Convert your markdown files to PDF format"
)

image_to_pdf = gr.Interface(
    fn=convert_image_to_pdf,
    inputs=gr.File(label="Upload Image", file_types=["image"]),
    outputs=gr.File(label="Converted PDF"),
    title="Image to PDF Converter",
    description="Convert your images to PDF format"
)

# Create tabbed interface
demo = gr.TabbedInterface(
    [readme_to_pdf, image_to_pdf],
    ["README to PDF", "Image to PDF"],
)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, debug=True, mcp_server=True)