File size: 13,013 Bytes
f1996dd
 
 
 
d0b423f
 
 
 
 
e3bc0c6
ce67523
e3bc0c6
e851339
220b45d
7f3a813
96d9245
e3bc0c6
220b45d
 
 
 
 
 
 
 
e3bc0c6
f1996dd
220b45d
e851339
 
 
 
220b45d
58a3898
ce879d8
ce67523
58a3898
f1996dd
220b45d
 
96d9245
 
 
 
 
 
220b45d
 
 
 
 
 
468fb8d
220b45d
 
 
 
 
 
ce67523
220b45d
58a3898
 
ce67523
58a3898
 
220b45d
ce67523
220b45d
58a3898
 
ce67523
58a3898
 
220b45d
96d9245
 
e2c6744
 
 
 
 
 
7f3a813
 
96d9245
 
 
e2c6744
96d9245
 
 
 
220b45d
 
 
 
 
 
 
 
96d9245
220b45d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96d9245
220b45d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b423f
 
220b45d
 
 
 
 
 
96d9245
220b45d
 
 
 
 
 
 
 
 
7f3a813
220b45d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f3a813
96d9245
 
 
e2c6744
 
96d9245
 
 
 
220b45d
 
 
 
 
96d9245
 
 
 
220b45d
 
 
 
 
 
 
 
 
e2c6744
96d9245
220b45d
 
 
 
 
 
 
 
 
 
 
e851339
 
 
 
 
58a3898
e851339
ce67523
e851339
 
58a3898
 
 
 
e851339
58a3898
e851339
96d9245
58a3898
e851339
 
 
58a3898
e851339
 
 
220b45d
 
e851339
 
 
 
 
f1996dd
220b45d
e851339
220b45d
ad2f309
 
ce879d8
ad2f309
220b45d
ef7763d
e851339
 
58a3898
e851339
 
 
 
 
 
 
 
 
220b45d
 
ad2f309
 
220b45d
e851339
 
58a3898
e851339
 
 
 
 
 
 
 
220b45d
 
 
 
96d9245
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import base64
import gradio as gr
from mistralai import Mistral
from mistralai.models import OCRResponse
from pathlib import Path
from pydantic import BaseModel
import pycountry
import json
import logging
from tenacity import retry, stop_after_attempt, wait_fixed
import tempfile
from typing import Union, Dict, List
from contextlib import contextmanager
import requests
from enum import Enum

# Constants
DEFAULT_LANGUAGE = "English"
SUPPORTED_IMAGE_TYPES = [".jpg", ".png"]
SUPPORTED_PDF_TYPES = [".pdf"]
TEMP_FILE_EXPIRY = 7200  # 2 hours in seconds

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

class OCRProcessor:
    def __init__(self, api_key: str):
        if not api_key:
            raise ValueError("API key must be provided")
        self.api_key = api_key
        self.client = Mistral(api_key=self.api_key)
        try:
            self.client.models.list()  # Validate API key
        except Exception as e:
            raise ValueError(f"Invalid API key: {str(e)}")

    @staticmethod
    def _encode_image(image_path: str) -> str:
        try:
            with open(image_path, "rb") as image_file:
                return base64.b64encode(image_file.read()).decode('utf-8')
        except Exception as e:
            logger.error(f"Error encoding image {image_path}: {str(e)}")
            raise

    @staticmethod
    @contextmanager
    def _temp_file(content: bytes, suffix: str) -> str:
        temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
        try:
            temp_file.write(content)
            temp_file.close()
            yield temp_file.name
        finally:
            if os.path.exists(temp_file.name):
                os.unlink(temp_file.name)

    @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
    def _call_ocr_api(self, document: Dict) -> OCRResponse:
        try:
            return self.client.ocr.process(model="mistral-ocr-latest", document=document)
        except Exception as e:
            logger.error(f"OCR API call failed: {str(e)}")
            raise

    @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
    def _call_chat_complete(self, model: str, messages: List[Dict], **kwargs) -> Dict:
        try:
            return self.client.chat.complete(model=model, messages=messages, **kwargs)
        except Exception as e:
            logger.error(f"Chat complete API call failed: {str(e)}")
            raise

    def _get_file_content(self, file_input: Union[str, object]) -> bytes:
        try:
            if isinstance(file_input, str) and file_input.startswith(("http://", "https://")):
                # Handle URLs
                response = requests.get(file_input, timeout=10)
                response.raise_for_status()
                return response.content
            elif isinstance(file_input, str):  # File path
                with open(file_input, "rb") as f:
                    return f.read()
            elif hasattr(file_input, 'read'):  # File-like object
                return file_input.read()
            else:
                raise ValueError("Invalid file input: must be a URL, path, or file-like object")
        except Exception as e:
            logger.error(f"Error getting file content: {str(e)}")
            raise

    def ocr_pdf_url(self, pdf_url: str) -> str:
        logger.info(f"Processing PDF URL: {pdf_url}")
        try:
            response = self._call_ocr_api({"type": "document_url", "document_url": pdf_url})
            return self._extract_markdown(response)
        except Exception as e:
            return self._handle_error("PDF URL processing", e)

    def ocr_uploaded_pdf(self, pdf_file: Union[str, object]) -> str:
        file_name = getattr(pdf_file, 'name', 'unknown')
        logger.info(f"Processing uploaded PDF: {file_name}")
        try:
            content = self._get_file_content(pdf_file)
            with self._temp_file(content, ".pdf") as temp_path:
                uploaded_file = self.client.files.upload(
                    file={"file_name": temp_path, "content": open(temp_path, "rb")},
                    purpose="ocr"
                )
                signed_url = self.client.files.get_signed_url(file_id=uploaded_file.id, expiry=TEMP_FILE_EXPIRY)
                response = self._call_ocr_api({"type": "document_url", "document_url": signed_url.url})
                return self._extract_markdown(response)
        except Exception as e:
            return self._handle_error("uploaded PDF processing", e)

    def ocr_image_url(self, image_url: str) -> str:
        logger.info(f"Processing image URL: {image_url}")
        try:
            response = self._call_ocr_api({"type": "image_url", "image_url": image_url})
            return self._extract_markdown(response)
        except Exception as e:
            return self._handle_error("image URL processing", e)

    def ocr_uploaded_image(self, image_file: Union[str, object]) -> str:
        file_name = getattr(image_file, 'name', 'unknown')
        logger.info(f"Processing uploaded image: {file_name}")
        try:
            content = self._get_file_content(image_file)
            with self._temp_file(content, ".jpg") as temp_path:
                encoded_image = self._encode_image(temp_path)
                base64_url = f"data:image/jpeg;base64,{encoded_image}"
                response = self._call_ocr_api({"type": "image_url", "image_url": base64_url})
                return self._extract_markdown(response)
        except Exception as e:
            return self._handle_error("uploaded image processing", e)

    def document_understanding(self, doc_url: str, question: str) -> str:
        logger.info(f"Document understanding - URL: {doc_url}, Question: {question}")
        try:
            messages = [{"role": "user", "content": [
                {"type": "text", "text": question},
                {"type": "document_url", "document_url": doc_url}
            ]}]
            response = self._call_chat_complete(model="mistral-small-latest", messages=messages)
            return response.choices[0].message.content if response.choices else "No response received"
        except Exception as e:
            return self._handle_error("document understanding", e)

    def structured_ocr(self, image_file: Union[str, object]) -> str:
        file_name = getattr(image_file, 'name', 'unknown')
        logger.info(f"Processing structured OCR for: {file_name}")
        try:
            content = self._get_file_content(image_file)
            with self._temp_file(content, ".jpg") as temp_path:
                encoded_image = self._encode_image(temp_path)
                base64_url = f"data:image/jpeg;base64,{encoded_image}"
                ocr_response = self._call_ocr_api({"type": "image_url", "image_url": base64_url})
                markdown = self._extract_markdown(ocr_response)

                chat_response = self._call_chat_complete(
                    model="pixtral-12b-latest",
                    messages=[{
                        "role": "user",
                        "content": [
                            {"type": "image_url", "image_url": base64_url},
                            {"type": "text", "text": (
                                f"OCR result:\n<BEGIN_IMAGE_OCR>\n{markdown}\n<END_IMAGE_OCR>\n"
                                "Convert to structured JSON with file_name, topics, languages, and ocr_contents"
                            )}
                        ]
                    }],
                    response_format={"type": "json_object"},
                    temperature=0
                )

                content = chat_response.choices[0].message.content if chat_response.choices else "{}"
                try:
                    response_dict = json.loads(content)
                    if isinstance(response_dict, list):  # Handle unexpected list response
                        response_dict = response_dict[0] if response_dict else {}
                except json.JSONDecodeError:
                    logger.error("Invalid JSON response from chat API")
                    response_dict = {}
                return self._format_structured_response(temp_path, response_dict)
        except Exception as e:
            return self._handle_error("structured OCR", e)

    @staticmethod
    def _extract_markdown(response: OCRResponse) -> str:
        try:
            return response.pages[0].markdown if response.pages else "No text extracted"
        except AttributeError:
            return "Invalid OCR response format"

    @staticmethod
    def _handle_error(context: str, error: Exception) -> str:
        logger.error(f"Error in {context}: {str(error)}")
        return f"**Error:** {str(error)}"

    @staticmethod
    def _format_structured_response(file_path: str, content: Dict) -> str:
        languages = {lang.alpha_2: lang.name for lang in pycountry.languages if hasattr(lang, 'alpha_2')}
        valid_langs = [l for l in (content.get("languages") or [DEFAULT_LANGUAGE]) if l in languages.values()]
        
        response = {
            "file_name": Path(file_path).name,
            "topics": content.get("topics", []),
            "languages": valid_langs or [DEFAULT_LANGUAGE],
            "ocr_contents": content.get("ocr_contents", {})
        }
        return f"```json\n{json.dumps(response, indent=4)}\n```"

def create_interface():
    with gr.Blocks(title="Mistral OCR & Structured Output App") as demo:
        gr.Markdown("# Mistral OCR & Structured Output App")
        gr.Markdown("Enter your Mistral API key below to use the app. Extract text from PDFs and images or get structured JSON output.")

        api_key_input = gr.Textbox(
            label="Mistral API Key",
            placeholder="Enter your Mistral API key here",
            type="password"
        )

        def initialize_processor(api_key):
            try:
                processor = OCRProcessor(api_key)
                return processor, "**Success:** API key set and validated!"
            except ValueError as e:
                return None, f"**Error:** {str(e)}"
            except Exception as e:
                return None, f"**Error:** Unexpected error: {str(e)}"

        processor_state = gr.State(value=None)
        api_status = gr.Markdown("API key not set. Please enter and set your key.")

        set_api_button = gr.Button("Set API Key")
        set_api_button.click(
            fn=initialize_processor,
            inputs=api_key_input,
            outputs=[processor_state, api_status]
        )

        tabs = [
            ("OCR with PDF URL", gr.Textbox, "ocr_pdf_url", "PDF URL", None),
            ("OCR with Uploaded PDF", gr.File, "ocr_uploaded_pdf", "Upload PDF", SUPPORTED_PDF_TYPES),
            ("OCR with Image URL", gr.Textbox, "ocr_image_url", "Image URL", None),
            ("OCR with Uploaded Image", gr.File, "ocr_uploaded_image", "Upload Image", SUPPORTED_IMAGE_TYPES),
            ("Structured OCR", gr.File, "structured_ocr", "Upload Image", SUPPORTED_IMAGE_TYPES),
        ]

        for name, input_type, fn_name, label, file_types in tabs:
            with gr.Tab(name):
                if input_type == gr.Textbox:
                    inputs = input_type(label=label, placeholder=f"e.g., https://example.com/{label.lower().replace(' ', '')}")
                else:
                    inputs = input_type(label=label, file_types=file_types)
                output = gr.Markdown(label="Result")
                button_label = name.replace("OCR with ", "").replace("Structured ", "Get Structured ")

                def process_with_api(processor, input_data):
                    if not processor:
                        return "**Error:** Please set a valid API key first."
                    fn = getattr(processor, fn_name)
                    return fn(input_data)

                gr.Button(f"Process {button_label}").click(
                    fn=process_with_api,
                    inputs=[processor_state, inputs],
                    outputs=output
                )

        with gr.Tab("Document Understanding"):
            doc_url = gr.Textbox(label="Document URL", placeholder="e.g., https://arxiv.org/pdf/1805.04770")
            question = gr.Textbox(label="Question", placeholder="e.g., What is the last sentence?")
            output = gr.Markdown(label="Answer")

            def doc_understanding_with_api(processor, url, q):
                if not processor:
                    return "**Error:** Please set a valid API key first."
                return processor.document_understanding(url, q)

            gr.Button("Ask Question").click(
                fn=doc_understanding_with_api,
                inputs=[processor_state, doc_url, question],
                outputs=output
            )

    return demo

if __name__ == "__main__":
    create_interface().launch(share=True, debug=True)