File size: 10,256 Bytes
9634b36
 
c7a5739
9634b36
b3bb65b
ad0b1f7
 
7998543
9323459
14cda2c
 
 
 
 
 
 
 
 
 
 
 
b3bb65b
9bea774
c7a5739
b3bb65b
 
 
 
 
 
 
 
 
 
 
7998543
06449e7
 
 
b3bb65b
 
 
9bea774
b3bb65b
 
 
c7a5739
06449e7
 
 
 
 
7998543
 
c7a5739
 
 
 
b3bb65b
c7a5739
 
 
b3bb65b
 
7998543
c7a5739
 
b3bb65b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9433534
b3bb65b
 
 
9433534
b3bb65b
 
 
 
 
 
 
c8cd30b
9433534
b3bb65b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7a5739
06449e7
b3bb65b
 
14cda2c
b3bb65b
 
c7a5739
9bea774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14cda2c
 
 
 
 
 
 
 
 
 
 
 
9bea774
 
 
 
 
 
 
 
 
 
 
 
43a7a2a
9bea774
43a7a2a
9bea774
 
43a7a2a
9bea774
 
 
 
 
 
 
69c287e
 
9bea774
 
 
 
 
 
 
 
 
 
 
69c287e
9bea774
 
69c287e
9bea774
 
43a7a2a
9bea774
 
 
 
 
 
 
43a7a2a
69c287e
9bea774
 
 
69c287e
9bea774
 
 
69c287e
9bea774
 
 
 
 
 
 
 
 
 
 
69c287e
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import gradio as gr
import pandas as pd
import fitz  # PyMuPDF
import os
import re
from huggingface_hub import HfApi
from huggingface_hub.utils import HfHubHTTPError
import time

def sanitize_title(title, max_length=100):
    """
    Sanitize the paper title to be safe for use as a filename.
    Removes non-alphanumeric characters (except underscores and hyphens)
    and truncates to max_length characters.
    """
    sanitized = re.sub(r'[^\w\s-]', '', title).strip()  # Remove unwanted characters
    sanitized = re.sub(r'[-\s]+', '_', sanitized)  # Replace spaces and hyphens with underscores
    if len(sanitized) > max_length:
        sanitized = sanitized[:max_length]
    return sanitized

def extract_full_paper_with_labels(pdf_path, progress=None):
    print(f"πŸ“„ Starting PDF Processing: {os.path.basename(pdf_path)}")
    doc = fitz.open(pdf_path)
    content = ""

    # Initialize metadata
    title = ""
    authors = ""
    year = ""
    doi = ""
    abstract = ""
    footnotes = ""
    references = ""
    sources = ""
    total_pages = len(doc)
    max_iterations = total_pages * 2  # To prevent infinite loops
    iteration_count = 0

    # Regex patterns for detection
    doi_pattern = r"\b10\.\d{4,9}/[-._;()/:A-Z0-9]+\b"
    year_pattern = r'\b(19|20)\d{2}\b'
    code_pattern = r"(def\s+\w+\s*\(|class\s+\w+|import\s+\w+|for\s+\w+\s+in|if\s+\w+|while\s+\w+|try:|except|{|\}|;)"
    reference_keywords = ['reference', 'bibliography', 'sources']
    financial_keywords = ['p/e', 'volatility', 'market cap', 'roi', 'sharpe', 'drawdown']

    for page_num, page in enumerate(doc):
        iteration_count += 1
        if iteration_count > max_iterations:
            raise Exception("⚠️ PDF processing exceeded iteration limit. Possible malformed PDF.")

        if progress is not None:
            progress((page_num + 1) / total_pages, desc=f"Processing Page {page_num + 1}/{total_pages}")

        blocks = page.get_text("dict")["blocks"]
        for block in blocks:
            if "lines" in block:
                text = ""
                max_font_size = 0
                for line in block["lines"]:
                    for span in line["spans"]:
                        text += span["text"] + " "
                        if span["size"] > max_font_size:
                            max_font_size = span["size"]

                text = text.strip()

                # Title (First Page, Largest Font)
                if page_num == 0 and max_font_size > 15 and not title:
                    title = text
                    content += f"<TITLE>{title}</TITLE>\n"

                # Authors
                elif re.search(r'author|by', text, re.IGNORECASE) and not authors:
                    authors = text
                    content += f"<AUTHORS>{authors}</AUTHORS>\n"

                # Year
                elif re.search(year_pattern, text) and not year:
                    year = re.search(year_pattern, text).group(0)
                    content += f"<YEAR>{year}</YEAR>\n"

                # DOI
                elif re.search(doi_pattern, text) and not doi:
                    doi = re.search(doi_pattern, text).group(0)
                    content += f"<DOI>{doi}</DOI>\n"

                # Abstract
                elif "abstract" in text.lower() and not abstract:
                    abstract = text
                    content += f"<ABSTRACT>{abstract}</ABSTRACT>\n"

                # Footnotes (small fonts)
                elif max_font_size < 10:
                    footnotes += text + " "

                # References
                elif any(keyword in text.lower() for keyword in reference_keywords):
                    references += text + " "

                # Tables
                elif re.search(r"table\s*\d+", text, re.IGNORECASE):
                    content += f"<TABLE>{text}</TABLE>\n"

                # Figures
                elif re.search(r"figure\s*\d+", text, re.IGNORECASE):
                    content += f"<FIGURE>{text}</FIGURE>\n"

                # Equations (look for math symbols)
                elif re.search(r"=|βˆ‘|√|Β±|Γ—|Ο€|ΞΌ|Οƒ", text):
                    content += f"<EQUATION>{text}</EQUATION>\n"

                # βœ… Improved Code Block Detection
                elif re.search(code_pattern, text) and len(text.split()) <= 50:
                    content += f"<CODE>{text}</CODE>\n"

                # Financial Metrics
                elif any(fin_kw in text.lower() for fin_kw in financial_keywords):
                    content += f"<FINANCIAL_METRIC>{text}</FINANCIAL_METRIC>\n"

                # Regular Paragraph
                else:
                    content += f"<PARAGRAPH>{text}</PARAGRAPH>\n"

    # Append Footnotes and References
    if footnotes:
        content += f"<FOOTNOTE>{footnotes.strip()}</FOOTNOTE>\n"
    if references:
        content += f"<REFERENCE>{references.strip()}</REFERENCE>\n"

    print(f"βœ… Finished Processing PDF: {os.path.basename(pdf_path)}")
    return {
        "filename": os.path.basename(pdf_path),
        "title": title,  # Include the title in the return data
        "content": content
    }

def upload_with_progress(file_path, repo_id, token, progress):
    """
    Upload file to Hugging Face Dataset using upload_file() API method.
    """
    print(f"πŸ“€ Starting upload of Parquet: {file_path}")
    file_size = os.path.getsize(file_path)

    api = HfApi()

    try:
        # Use upload_file() method from huggingface_hub
        api.upload_file(
            path_or_fileobj=file_path,
            path_in_repo=os.path.basename(file_path),
            repo_id=repo_id,
            repo_type="dataset",
            token=token
        )

        if progress is not None:
            progress(1, desc="βœ… Upload Complete")

        print(f"βœ… Successfully uploaded to {repo_id}")
        return f"βœ… Successfully uploaded to {repo_id}"

    except HfHubHTTPError as e:
        print(f"❌ Upload failed: {e}")
        return f"❌ Upload failed: {str(e)}"
    except Exception as e:
        print(f"❌ Unexpected error: {e}")
        return f"❌ Unexpected error: {str(e)}"

def pdf_to_parquet_and_upload(pdf_files, hf_token, dataset_repo_id, action_choice, progress=gr.Progress()):
    all_data = []

    total_files = len(pdf_files)
    print("πŸš€ Starting PDF to Parquet Conversion Process")

    for idx, pdf_file in enumerate(pdf_files):
        if progress is not None:
            progress(idx / total_files, desc=f"Processing File {idx + 1}/{total_files}")

        # βœ… Step 1: Process PDF with Full Labels
        extracted_data = extract_full_paper_with_labels(pdf_file.name, progress=progress)
        all_data.append(extracted_data)

    print("🟑 Converting Processed Data to Parquet")
    # βœ… Step 2: Convert to Parquet
    df = pd.DataFrame(all_data)
    
    # Generate the parquet file name
    if len(all_data) == 1:
        paper_title = all_data[0].get("title", "").strip()
        if paper_title:
            safe_title = sanitize_title(paper_title)
            parquet_file = f"{safe_title}.parquet"
        else:
            parquet_file = 'fully_labeled_papers.parquet'
    else:
        # For multiple PDFs, include a timestamp to avoid overwrites
        parquet_file = f"fully_labeled_papers_{time.strftime('%Y%m%d_%H%M%S')}.parquet"

    try:
        df.to_parquet(parquet_file, engine='pyarrow', index=False)
        print("βœ… Parquet Conversion Completed")
    except Exception as e:
        print(f"❌ Parquet Conversion Failed: {str(e)}")
        return None, f"❌ Parquet Conversion Failed: {str(e)}"

    upload_message = "Skipped Upload"

    # βœ… Step 3: Upload Parquet (if selected)
    if action_choice in ["Upload to Hugging Face", "Both"]:
        try:
            upload_message = upload_with_progress(parquet_file, dataset_repo_id, hf_token, progress)
        except Exception as e:
            print(f"❌ Upload Failed: {str(e)}")
            upload_message = f"❌ Upload failed: {str(e)}"

    print("🏁 Process Completed")
    return parquet_file, upload_message

# Define a function for our custom "Reset Files Only" button.
def reset_files_fn():
    # Return None for both the file input and the output file, clearing them.
    return None, None

with gr.Blocks() as demo:
    gr.Markdown(
        """
        # PDF to Parquet Converter with Full Labeling
        
        **Clear All Inputs:** The button below (labeled "Clear All Inputs") will reset every field, including your API key and dataset repo ID.  
        **Reset Files Only:** Use this button if you want to clear the PDF file uploads and the generated Parquet file, while keeping your credentials intact.
        """
    )
    
    with gr.Row():
        pdf_input = gr.File(file_types=[".pdf"], file_count="multiple", label="Upload PDFs (Drag & Drop or Search)")
    with gr.Row():
        hf_token = gr.Textbox(label="Hugging Face API Token", type="password", placeholder="Enter your Hugging Face API token")
        dataset_repo = gr.Textbox(label="Your Dataset Repo ID (e.g., username/research-dataset)", placeholder="username/research-dataset")
    with gr.Row():
        action_radio = gr.Radio(["Download Locally", "Upload to Hugging Face", "Both"], label="Action", value="Download Locally")
    
    with gr.Row():
        convert_button = gr.Button("Convert PDF to Parquet")
        reset_files_button = gr.Button("Reset Files Only")
        clear_all_button = gr.Button("Clear All Inputs")
    
    with gr.Row():
        output_file = gr.File(label="Download Parquet File")
        status_text = gr.Textbox(label="Status")
    
    convert_button.click(
        fn=pdf_to_parquet_and_upload,
        inputs=[pdf_input, hf_token, dataset_repo, action_radio],
        outputs=[output_file, status_text]
    )
    
    reset_files_button.click(
        fn=reset_files_fn,
        inputs=None,
        outputs=[pdf_input, output_file]
    )
    
    # The Clear All button resets every input field.
    def clear_all_fn():
        return None, None, None, "Download Locally"
    
    clear_all_button.click(
        fn=clear_all_fn,
        inputs=None,
        outputs=[pdf_input, hf_token, dataset_repo, action_radio]
    )

demo.launch()