Spaces:
Running
Running
File size: 12,251 Bytes
f1996dd d0b423f e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 468fb8d f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 fd84b98 f1996dd 468fb8d e3bc0c6 f1996dd 468fb8d e3bc0c6 468fb8d e3bc0c6 f1996dd e3bc0c6 f1996dd 468fb8d e3bc0c6 fd84b98 f1996dd e3bc0c6 fd84b98 e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 fd84b98 f1996dd e3bc0c6 fd84b98 f1996dd 468fb8d e3bc0c6 f1996dd 468fb8d e3bc0c6 468fb8d e3bc0c6 fd84b98 f1996dd e3bc0c6 fd84b98 e3bc0c6 f1996dd e3bc0c6 f1996dd d0b423f f1996dd e3bc0c6 f1996dd e3bc0c6 fd84b98 f1996dd d0b423f 468fb8d e3bc0c6 d0b423f 468fb8d e3bc0c6 468fb8d e3bc0c6 d0b423f e3bc0c6 d0b423f e3bc0c6 d0b423f e3bc0c6 d0b423f e3bc0c6 fd84b98 d0b423f e3bc0c6 fd84b98 e3bc0c6 d0b423f f1996dd d0b423f e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd e3bc0c6 f1996dd d0b423f e3bc0c6 d0b423f |
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 |
import os
import base64
import gradio as gr
from mistralai import Mistral
from mistralai.models import OCRResponse
from pathlib import Path
from enum import Enum
from pydantic import BaseModel
import pycountry
import json
import logging
from tenacity import retry, stop_after_attempt, wait_fixed
import tempfile
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize Mistral client with API key
api_key = os.environ.get("MISTRAL_API_KEY")
if not api_key:
raise ValueError("MISTRAL_API_KEY environment variable is not set. Please configure it.")
client = Mistral(api_key=api_key)
# Helper 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 Exception as e:
logger.error(f"Error encoding image {image_path}: {str(e)}")
return f"Error encoding image: {str(e)}"
# Retry-enabled API call helpers
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def call_ocr_api(document):
return client.ocr.process(model="mistral-ocr-latest", document=document)
@retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
def call_chat_complete(model, messages, **kwargs):
return client.chat.complete(model=model, messages=messages, **kwargs)
# Helper function to get file content (handles both string paths and file-like objects)
def get_file_content(file_input):
if isinstance(file_input, str): # Gradio 3.x: file path
with open(file_input, "rb") as f:
return f.read()
else: # Gradio 4.x or file-like object
return file_input.read()
# OCR with PDF URL
def ocr_pdf_url(pdf_url):
logger.info(f"Processing PDF URL: {pdf_url}")
try:
ocr_response = call_ocr_api({"type": "document_url", "document_url": pdf_url})
try:
markdown = ocr_response.pages[0].markdown
except (IndexError, AttributeError):
markdown = "No text extracted or response invalid."
logger.info("Successfully processed PDF URL")
return markdown
except Exception as e:
logger.error(f"Error processing PDF URL: {str(e)}")
return f"**Error:** {str(e)}"
# OCR with Uploaded PDF
def ocr_uploaded_pdf(pdf_file):
logger.info(f"Processing uploaded PDF: {getattr(pdf_file, 'name', 'unknown')}")
temp_path = None
try:
# Get file content (handles both string and file-like objects)
content = get_file_content(pdf_file)
# Use tempfile to handle uploaded file securely
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
temp_file.write(content)
temp_path = temp_file.name
uploaded_pdf = client.files.upload(
file={"file_name": temp_path, "content": open(temp_path, "rb")},
purpose="ocr"
)
signed_url = client.files.get_signed_url(file_id=uploaded_pdf.id, expiry=7200) # 2 hours
ocr_response = call_ocr_api({"type": "document_url", "document_url": signed_url.url})
try:
markdown = ocr_response.pages[0].markdown
except (IndexError, AttributeError):
markdown = "No text extracted or response invalid."
logger.info("Successfully processed uploaded PDF")
return markdown
except Exception as e:
logger.error(f"Error processing uploaded PDF: {str(e)}")
return f"**Error:** {str(e)}"
finally:
if temp_path and os.path.exists(temp_path):
os.remove(temp_path)
# OCR with Image URL
def ocr_image_url(image_url):
logger.info(f"Processing image URL: {image_url}")
try:
ocr_response = call_ocr_api({"type": "image_url", "image_url": image_url})
try:
markdown = ocr_response.pages[0].markdown
except (IndexError, AttributeError):
markdown = "No text extracted or response invalid."
logger.info("Successfully processed image URL")
return markdown
except Exception as e:
logger.error(f"Error processing image URL: {str(e)}")
return f"**Error:** {str(e)}"
# OCR with Uploaded Image
def ocr_uploaded_image(image_file):
logger.info(f"Processing uploaded image: {getattr(image_file, 'name', 'unknown')}")
temp_path = None
try:
# Get file content (handles both string and file-like objects)
content = get_file_content(image_file)
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
temp_file.write(content)
temp_path = temp_file.name
encoded_image = encode_image(temp_path)
if "Error" in encoded_image:
raise ValueError(encoded_image)
base64_data_url = f"data:image/jpeg;base64,{encoded_image}"
ocr_response = call_ocr_api({"type": "image_url", "image_url": base64_data_url})
try:
markdown = ocr_response.pages[0].markdown
except (IndexError, AttributeError):
markdown = "No text extracted or response invalid."
logger.info("Successfully processed uploaded image")
return markdown
except Exception as e:
logger.error(f"Error processing uploaded image: {str(e)}")
return f"**Error:** {str(e)}"
finally:
if temp_path and os.path.exists(temp_path):
os.remove(temp_path)
# Document Understanding
def document_understanding(doc_url, question):
logger.info(f"Processing document understanding - URL: {doc_url}, Question: {question}")
try:
messages = [
{"role": "user", "content": [
{"type": "text", "text": question},
{"type": "document_url", "document_url": doc_url}
]}
]
chat_response = call_chat_complete(model="mistral-small-latest", messages=messages)
try:
content = chat_response.choices[0].message.content
except (IndexError, AttributeError):
content = "No response received from the API."
logger.info("Successfully processed document understanding")
return content
except Exception as e:
logger.error(f"Error in document understanding: {str(e)}")
return f"**Error:** {str(e)}"
# Structured OCR Setup
languages = {lang.alpha_2: lang.name for lang in pycountry.languages if hasattr(lang, 'alpha_2')}
class LanguageMeta(Enum.__class__):
def __new__(metacls, cls, bases, classdict):
for code, name in languages.items():
classdict[name.upper().replace(' ', '_')] = name
return super().__new__(metacls, cls, bases, classdict)
class Language(Enum, metaclass=LanguageMeta):
pass
class StructuredOCR(BaseModel):
file_name: str
topics: list[str]
languages: list[Language]
ocr_contents: dict
def structured_ocr(image_file):
logger.info(f"Processing structured OCR for image: {getattr(image_file, 'name', 'unknown')}")
temp_path = None
try:
# Get file content (handles both string and file-like objects)
content = get_file_content(image_file)
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
temp_file.write(content)
temp_path = temp_file.name
image_path = Path(temp_path)
encoded_image = encode_image(temp_path)
if "Error" in encoded_image:
raise ValueError(encoded_image)
base64_data_url = f"data:image/jpeg;base64,{encoded_image}"
image_response = call_ocr_api({"type": "image_url", "image_url": base64_data_url})
try:
image_ocr_markdown = image_response.pages[0].markdown
except (IndexError, AttributeError):
image_ocr_markdown = "No text extracted."
chat_response = call_chat_complete(
model="pixtral-12b-latest",
messages=[{
"role": "user",
"content": [
{"type": "image_url", "image_url": base64_data_url},
{"type": "text", "text": (
f"This is the image's OCR in markdown:\n<BEGIN_IMAGE_OCR>\n{image_ocr_markdown}\n<END_IMAGE_OCR>.\n"
"Convert this into a structured JSON response with the OCR contents in a sensible dictionary."
)}
],
}],
response_format={"type": "json_object"},
temperature=0
)
try:
content = chat_response.choices[0].message.content
response_dict = json.loads(content)
except (json.JSONDecodeError, IndexError, AttributeError):
logger.error("Failed to parse structured response")
return "Failed to parse structured response. Please try again."
language_members = {member.value: member for member in Language}
valid_languages = [l for l in response_dict.get("languages", ["English"]) if l in language_members]
languages = [language_members[l] for l in valid_languages] if valid_languages else [Language.ENGLISH]
structured_response = StructuredOCR(
file_name=image_path.name,
topics=response_dict.get("topics", []),
languages=languages,
ocr_contents=response_dict.get("ocr_contents", {})
)
logger.info("Successfully processed structured OCR")
return f"```json\n{json.dumps(structured_response.dict(), indent=4)}\n```"
except Exception as e:
logger.error(f"Error processing structured OCR: {str(e)}")
return f"**Error:** {str(e)}"
finally:
if temp_path and os.path.exists(temp_path):
os.remove(temp_path)
# Gradio Interface
with gr.Blocks(title="Mistral OCR & Structured Output App") as demo:
gr.Markdown("# Mistral OCR & Structured Output App")
gr.Markdown("Extract text from PDFs and images, ask questions about documents, or get structured JSON output!")
with gr.Tab("OCR with PDF URL"):
pdf_url_input = gr.Textbox(label="PDF URL", placeholder="e.g., https://arxiv.org/pdf/2201.04234")
pdf_url_output = gr.Textbox(label="OCR Result (Markdown)")
pdf_url_button = gr.Button("Process PDF")
pdf_url_button.click(ocr_pdf_url, inputs=pdf_url_input, outputs=pdf_url_output)
with gr.Tab("OCR with Uploaded PDF"):
pdf_file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
pdf_file_output = gr.Textbox(label="OCR Result (Markdown)")
pdf_file_button = gr.Button("Process Uploaded PDF")
pdf_file_button.click(ocr_uploaded_pdf, inputs=pdf_file_input, outputs=pdf_file_output)
with gr.Tab("OCR with Image URL"):
image_url_input = gr.Textbox(label="Image URL", placeholder="e.g., https://example.com/image.jpg")
image_url_output = gr.Textbox(label="OCR Result (Markdown)")
image_url_button = gr.Button("Process Image")
image_url_button.click(ocr_image_url, inputs=image_url_input, outputs=image_url_output)
with gr.Tab("OCR with Uploaded Image"):
image_file_input = gr.File(label="Upload Image", file_types=[".jpg", ".png"])
image_file_output = gr.Textbox(label="OCR Result (Markdown)")
image_file_button = gr.Button("Process Uploaded Image")
image_file_button.click(ocr_uploaded_image, inputs=image_file_input, outputs=image_file_output)
with gr.Tab("Document Understanding"):
doc_url_input = gr.Textbox(label="Document URL", placeholder="e.g., https://arxiv.org/pdf/1805.04770")
question_input = gr.Textbox(label="Question", placeholder="e.g., What is the last sentence?")
doc_output = gr.Textbox(label="Answer")
doc_button = gr.Button("Ask Question")
doc_button.click(document_understanding, inputs=[doc_url_input, question_input], outputs=doc_output)
with gr.Tab("Structured OCR"):
struct_image_input = gr.File(label="Upload Image", file_types=[".jpg", ".png"])
struct_output = gr.Textbox(label="Structured JSON Output")
struct_button = gr.Button("Get Structured Output")
struct_button.click(structured_ocr, inputs=struct_image_input, outputs=struct_output)
demo.launch(share=True, debug=True) |