File size: 7,580 Bytes
b26838d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import fitz
import requests
import json
import time
import logging

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


def get_pdf_pages(pdf_folder_dir, pdf_dir):
    """

    Get the number of pages in a PDF file.



    Parameters:

    pdf_folder_dir: str - The directory of the PDF folder.

    pdf_dir: str - The name of the PDF file.



    Returns:

    int - The total number of pages in the PDF file, or None if the PDF cannot be read.

    """
    # Construct the full path to the PDF file
    path = pdf_folder_dir + "/" + pdf_dir

    # Attempt to open the PDF file
    try:
        doc = fitz.open(path)
    except:
        # If the file cannot be opened, print an error message and return None
        print("can not read pdf")
        return None

    # Get and return the number of pages in the PDF file
    page_count = doc.page_count

    return page_count


def get_api_credentials():
    """Retrieve Mathpix API credentials from environment variables"""
    APP_ID = os.getenv('MATHPIX_APP_ID')
    print(APP_ID)
    APP_KEY = os.getenv('MATHPIX_APP_KEY')
    if not APP_ID or not APP_KEY:
        raise ValueError("Please set MATHPIX_APP_ID and MATHPIX_APP_KEY environment variables")
    return APP_ID, APP_KEY

def upload_pdf_to_mathpix(pdf_file_path, headers, options):
    """Upload the PDF file to Mathpix API"""
    url = 'https://api.mathpix.com/v3/pdf'
    with open(pdf_file_path, 'rb') as pdf_file:
        files = {
            'file': pdf_file,
            'options_json': (None, json.dumps(options))
        }
        response = requests.post(url, headers=headers, files=files)
    return response


def check_conversion_status(pdf_id, headers, max_retries=30, retry_interval=5):
    """Check the conversion status with a maximum number of retries and interval"""
    status_url = f'https://api.mathpix.com/v3/pdf/{pdf_id}'
    retries = 0

    while retries < max_retries:
        status_response = requests.get(status_url, headers=headers)
        status_data = status_response.json()
        conversion_status = status_data.get('status', 'unknown')
        logging.info(f"conversion_status: {conversion_status}")

        # Log the full response data for debugging purposes
        logging.debug(f"Full conversion status response: {status_data}")

        if conversion_status == 'completed':
            break
        elif conversion_status in ['loaded', 'split', 'processing']:
            logging.info(f"Conversion is {conversion_status}, waiting for processing to complete.")
            time.sleep(retry_interval)
            retries += 1
            continue
        else:
            raise ValueError(f"Conversion failed, status: {conversion_status}")

        logging.info('Processing... Please wait.')
        time.sleep(retry_interval)
        retries += 1

    if retries >= max_retries:
        raise TimeoutError("Conversion did not complete within the allowed time.")


def download_md_file(pdf_id, headers, output_dir, output_filename):
    """Download and save the Markdown file"""
    md_url = f'https://api.mathpix.com/v3/pdf/{pdf_id}.md'
    md_response = requests.get(md_url, headers=headers)
    if md_response.status_code == 200:
        os.makedirs(output_dir, exist_ok=True)
        output_path = os.path.join(output_dir, output_filename)
        with open(output_path, "w", encoding="utf-8") as fout:
            fout.write(md_response.text)
        logging.info(f"OCR result saved to: {output_path}")
        return md_response.text
    else:
        logging.error('Failed to download Markdown file.')
        return None


def extract_pdf_mathpix(pdf_folder_dir, pdf_dir, md_folder_dir):
    """

    Extract content from a PDF file and convert it to Markdown format

    """
    try:
        # Retrieve API credentials
        APP_ID, APP_KEY = get_api_credentials()

        # Build the PDF file path
        pdf_file_path = os.path.join(pdf_folder_dir, pdf_dir)
        logging.info(f"pdf_file_path: {pdf_file_path}")

        # Check if the file exists
        if not os.path.exists(pdf_file_path):
            raise FileNotFoundError(f"File {pdf_file_path} does not exist")

        # Set request headers and options
        headers = {
            'app_id': APP_ID,
            'app_key': APP_KEY,
        }
        options = {
            "conversion_formats": {
                "md": True
            },
            "math_inline_delimiters": ["$", "$"],
            "rm_spaces": True
        }

        # Upload the PDF file
        response = upload_pdf_to_mathpix(pdf_file_path, headers, options)
        if response.status_code != 200:
            logging.error(f'Failed to upload PDF. Status code: {response.status_code}')
            return None

        # Get the PDF ID
        pdf_id = response.json().get('pdf_id')
        logging.info(f"pdf_id: {pdf_id}")

        # Check the conversion status
        check_conversion_status(pdf_id, headers)

        # Download and save the Markdown file
        output_filename = os.path.splitext(pdf_dir)[0] + ".md"
        return download_md_file(pdf_id, headers, md_folder_dir, output_filename)

    except Exception as e:
        logging.error(f"An error occurred: {e}")
        return None


def get_done_papers(md_folder_dir):
    done_paper = []
    if os.path.exists(md_folder_dir):
        try:
            done_paper = [i.replace(".md", ".pdf") for i in os.listdir(md_folder_dir)]
        except (FileNotFoundError, PermissionError) as e:
            print(f"Error reading md folder: {e}")
    return done_paper


def process_pdfs(pdf_folder_dir, done_paper, md_folder_dir):
    no_response_paper = []
    pages_more_50 = []

    try:
        pdf_files = [i for i in os.listdir(pdf_folder_dir) if i.endswith("pdf")]
    except (FileNotFoundError, PermissionError) as e:
        print(f"Error reading pdf folder: {e}")
        return no_response_paper, pages_more_50, done_paper

    for pdf_file in pdf_files:
        if pdf_file not in done_paper + no_response_paper + pages_more_50:
            try:
                pages = get_pdf_pages(pdf_folder_dir, pdf_file)
                print(f"\nstart: {pdf_file} have pages: {pages}")

                if pages <= 50:
                    print(f"start convert pdf 2 md: {pdf_file}")
                    content = extract_pdf_mathpix(pdf_folder_dir, pdf_file, md_folder_dir)
                    if content:
                        done_paper.append(pdf_file)
                    else:
                        no_response_paper.append(pdf_file)
                else:
                    pages_more_50.append(pdf_file)
                    print(f"pages_more_50: {pages_more_50}")
            except Exception as e:
                print(f"Error processing {pdf_file}: {e}")

    return no_response_paper, pages_more_50, done_paper


if __name__ == '__main__':
    data_folder_dir = "../data/"
    pdf_folder_dir = os.path.join(data_folder_dir, "pdf")
    md_folder_dir = os.path.join(data_folder_dir, "md")

    done_paper = get_done_papers(md_folder_dir)
    print("done_paper:", done_paper)

    no_response_paper, pages_more_50, done_paper = process_pdfs(pdf_folder_dir, done_paper, md_folder_dir)
    print("done_paper:", done_paper)
    print("no_response_paper:", no_response_paper)
    print("pages_more_50:", pages_more_50)