File size: 8,562 Bytes
099e67a
c5e1e79
099e67a
4371128
099e67a
c5e1e79
099e67a
 
 
 
 
 
 
 
8a5a9ab
099e67a
 
 
 
 
84661cc
8a5a9ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4371128
8a5a9ab
 
 
 
 
4371128
8a5a9ab
 
4371128
8a5a9ab
 
 
 
 
4371128
8a5a9ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4371128
8a5a9ab
4371128
 
8a5a9ab
4371128
8a5a9ab
4371128
 
 
 
 
c5e1e79
 
8a5a9ab
 
4371128
099e67a
 
c5e1e79
099e67a
 
c5e1e79
 
 
 
 
 
 
 
 
 
 
099e67a
 
c5e1e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
099e67a
c5e1e79
 
 
099e67a
c5e1e79
 
 
 
 
4371128
 
099e67a
 
 
 
 
8a5a9ab
099e67a
 
84661cc
8a5a9ab
 
099e67a
 
 
 
8a5a9ab
c5e1e79
099e67a
8a5a9ab
4371128
8a5a9ab
 
 
 
4371128
099e67a
8a5a9ab
099e67a
 
 
 
 
 
 
 
 
4371128
099e67a
8a5a9ab
099e67a
 
 
 
 
 
 
 
 
 
c5e1e79
 
 
099e67a
 
 
8a5a9ab
c5e1e79
099e67a
c5e1e79
099e67a
 
 
 
 
 
 
 
8a5a9ab
c5e1e79
099e67a
 
 
8a5a9ab
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
import gradio as gr
from langchain_text_splitters import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter
from langchain.schema import Document
from typing import List, Dict
import logging
import re
from pathlib import Path
import requests
import base64
import io
from PIL import Image
from datasets import Dataset
from huggingface_hub import HfApi
import os
from mistralai import Mistral

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Mistral OCR setup
api_key = os.environ.get("MISTRAL_API_KEY")
if not api_key:
    raise ValueError("MISTRAL_API_KEY environment variable not set")
client = Mistral(api_key=api_key)

# Function to encode image to base64
def encode_image(image_path):
    try:
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        return "Error: The file was not found."
    except Exception as e:
        return f"Error: {e}"

# Function to replace images in markdown with base64 strings
def replace_images_in_markdown(markdown_str: str, images_dict: Dict[str, str]) -> str:
    for img_name, base64_str in images_dict.items():
        markdown_str = markdown_str.replace(f"![{img_name}]({img_name})", f"![{img_name}]({base64_str})")
    return markdown_str

# Function to combine markdown from OCR response
def get_combined_markdown(ocr_response) -> tuple[str, str]:
    markdowns = []
    raw_markdowns = []
    image_data = {}  # Collect all image data
    for page in ocr_response.pages:
        for img in page.images:
            image_data[img.id] = img.image_base64
        markdowns.append(replace_images_in_markdown(page.markdown, image_data))
        raw_markdowns.append(page.markdown)
    return "\n\n".join(markdowns), "\n\n".join(raw_markdowns), image_data

# Perform OCR on uploaded file
def perform_ocr_file(file):
    try:
        if file.name.lower().endswith('.pdf'):
            uploaded_pdf = client.files.upload(
                file={
                    "file_name": file.name,
                    "content": open(file.name, "rb"),
                },
                purpose="ocr"
            )
            signed_url = client.files.get_signed_url(file_id=uploaded_pdf.id)
            ocr_response = client.ocr.process(
                model="mistral-ocr-latest",
                document={
                    "type": "document_url",
                    "document_url": signed_url.url,
                },
                include_image_base64=True
            )
            client.files.delete(file_id=uploaded_pdf.id)

        elif file.name.lower().endswith(('.png', '.jpg', '.jpeg')):
            base64_image = encode_image(file.name)
            ocr_response = client.ocr.process(
                model="mistral-ocr-latest",
                document={
                    "type": "image_url",
                    "image_url": f"data:image/jpeg;base64,{base64_image}"
                },
                include_image_base64=True
            )
        else:
            return "Unsupported file type. Please provide a PDF or an image (png, jpeg, jpg).", "", {}

        combined_markdown, raw_markdown, image_data = get_combined_markdown(ocr_response)
        return combined_markdown, raw_markdown, image_data
    except Exception as e:
        return f"Error during OCR: {str(e)}", "", {}

# Function to extract image names from markdown content
def extract_image_names_from_markdown(markdown_text: str) -> List[str]:
    # Regex to match markdown image syntax
    pattern = r"!\[(.*?)\]\("
    return [match.replace("![","").replace("](","") for match in re.findall(pattern, markdown_text)]

# Function to chunk markdown text with image handling
def chunk_markdown(
    markdown_text: str,
    image_data: Dict[str, str],
    chunk_size: int = 1000,
    chunk_overlap: int = 200,
    strip_headers: bool = True
) -> List[Document]:
    try:
        # Define headers to split on
        headers_to_split_on = [
            ("#", "Header 1"),
            ("##", "Header 2"),
            ("###", "Header 3"),
        ]

        # Initialize MarkdownHeaderTextSplitter
        markdown_splitter = MarkdownHeaderTextSplitter(
            headers_to_split_on=headers_to_split_on,
            strip_headers=strip_headers
        )
        
        # Split markdown by headers
        logger.info("Splitting markdown by headers")
        chunks = markdown_splitter.split_text(markdown_text)

        # If chunk_size is specified, further split large chunks
        if chunk_size > 0:
            text_splitter = RecursiveCharacterTextSplitter(
                chunk_size=chunk_size,
                chunk_overlap=chunk_overlap,
                length_function=len,
                separators=["\n\n", "\n", ".", " ", ""],
                keep_separator=True,
                add_start_index=True
            )
            logger.info(f"Applying character-level splitting with chunk_size={chunk_size}")
            final_chunks = []
            for chunk in chunks:
                if len(chunk.page_content) > chunk_size:
                    sub_chunks = text_splitter.split_documents([chunk])
                    final_chunks.extend(sub_chunks)
                else:
                    final_chunks.append(chunk)
            chunks = final_chunks

        # Add images to metadata
        for chunk in chunks:
            image_names = extract_image_names_from_markdown(chunk.page_content)
            chunk.metadata["images"] = {name: image_data.get(name, None) for name in image_names}

        logger.info(f"Created {len(chunks)} chunks")
        return chunks
        
    except Exception as e:
        logger.error(f"Error processing markdown: {str(e)}")
        raise

# Placeholder image generation
def text_to_base64_dummy(text: str, chunk_index: int):
    img = Image.new('RGB', (200, 200), color='white')
    buffer = io.BytesIO()
    img.save(buffer, format="PNG")
    return base64.b64encode(buffer.getvalue()).decode("utf-8")

# Process file: OCR -> Chunk -> Save
def process_file_and_save(file, chunk_size, chunk_overlap, strip_headers, hf_token, repo_name):
    try:
        # Step 1: Perform OCR
        combined_markdown, raw_markdown, image_data = perform_ocr_file(file)
        if "Error" in combined_markdown:
            return combined_markdown
        
        # Step 2: Chunk the markdown
        chunks = chunk_markdown(combined_markdown, image_data, chunk_size, chunk_overlap, strip_headers)
        
        # Step 3: Prepare dataset
        data = {
            "chunk_id": [],
            "content": [],
            "metadata": [],
        }
        
        for i, chunk in enumerate(chunks):
            data["chunk_id"].append(i)
            data["content"].append(chunk.page_content)
            data["metadata"].append(chunk.metadata)

        # Step 4: Create and push dataset to Hugging Face
        dataset = Dataset.from_dict(data)
        api = HfApi()
        api.create_repo(repo_id=repo_name, token=hf_token, repo_type="dataset", exist_ok=True)
        dataset.push_to_hub(repo_name, token=hf_token)
        
        return f"Dataset created with {len(chunks)} chunks and saved to Hugging Face at {repo_name}"
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Interface
with gr.Blocks(title="PDF/Image OCR, Markdown Chunking, and Dataset Creator") as demo:
    gr.Markdown("# PDF/Image OCR, Markdown Chunking, and Dataset Creator")
    gr.Markdown("Upload a PDF or image, extract text/images with Mistral OCR, chunk the markdown by headers, and save to Hugging Face.")
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(label="Upload PDF or Image")
            chunk_size = gr.Slider(0, 2000, value=1000, step=100, label="Max Chunk Size (0 to disable)")
            chunk_overlap = gr.Slider(0, 500, value=200, step=50, label="Chunk Overlap")
            strip_headers = gr.Checkbox(label="Strip Headers from Content", value=True)
            hf_token = gr.Textbox(label="Hugging Face Token", type="password")
            repo_name = gr.Textbox(label="Hugging Face Repository Name (e.g., username/dataset-name)")
            submit_btn = gr.Button("Process and Save")
        
        with gr.Column():
            output = gr.Textbox(label="Result")

    submit_btn.click(
        fn=process_file_and_save,
        inputs=[file_input, chunk_size, chunk_overlap, strip_headers, hf_token, repo_name],
        outputs=output
    )

demo.launch(share=True)