Add comprehensive documentation and code comments
This commit adds extensive documentation to the Mistral OCR CLI project: - Add API.md with detailed API response format documentation - Add CHANGELOG.md to track version changes - Add CONTRIBUTING.md with guidelines for contributors - Enhance README.md with more detailed usage examples and troubleshooting - Add proper docstrings to all Python modules and functions - Update requirements.txt with development dependencies - Improve setup.py with better metadata These changes make the project more accessible to users and contributors.
This commit is contained in:
+65
-3
@@ -5,10 +5,33 @@ import requests
|
||||
from typing import Optional, Dict, Any, Tuple
|
||||
|
||||
class MistralClient:
|
||||
"""
|
||||
Client for interacting with the Mistral AI OCR API.
|
||||
|
||||
This client handles authentication, file uploads, and OCR processing
|
||||
requests to the Mistral AI API.
|
||||
|
||||
Attributes:
|
||||
BASE_URL (str): Base URL for the Mistral AI API
|
||||
MAX_FILE_SIZE (int): Maximum allowed file size in bytes (52 MB)
|
||||
api_key (str): Mistral AI API key for authentication
|
||||
session (requests.Session): Session object for making HTTP requests
|
||||
"""
|
||||
|
||||
BASE_URL = "https://api.mistral.ai/v1"
|
||||
MAX_FILE_SIZE = 52 * 1024 * 1024 # 52 MB
|
||||
|
||||
def __init__(self, api_key: Optional[str] = None):
|
||||
"""
|
||||
Initialize the Mistral AI client.
|
||||
|
||||
Args:
|
||||
api_key (Optional[str]): Mistral AI API key. If not provided,
|
||||
will look for MISTRAL_API_KEY environment variable.
|
||||
|
||||
Raises:
|
||||
ValueError: If no API key is provided or found in environment variables.
|
||||
"""
|
||||
self.api_key = api_key or os.environ.get("MISTRAL_API_KEY")
|
||||
if not self.api_key:
|
||||
raise ValueError("API key must be provided or set as MISTRAL_API_KEY environment variable")
|
||||
@@ -20,7 +43,19 @@ class MistralClient:
|
||||
})
|
||||
|
||||
def upload_file(self, file_path: str) -> str:
|
||||
"""Upload a file to Mistral API for OCR processing."""
|
||||
"""
|
||||
Upload a file to Mistral API for OCR processing.
|
||||
|
||||
Args:
|
||||
file_path (str): Path to the local file to upload
|
||||
|
||||
Returns:
|
||||
str: File ID returned by the API
|
||||
|
||||
Raises:
|
||||
ValueError: If the file is too large or if the upload fails
|
||||
requests.RequestException: If there's an error communicating with the API
|
||||
"""
|
||||
# Check file size
|
||||
file_size = os.path.getsize(file_path)
|
||||
if file_size > self.MAX_FILE_SIZE:
|
||||
@@ -72,7 +107,19 @@ class MistralClient:
|
||||
raise last_error or ValueError(f"Failed to upload file after {max_retries} attempts")
|
||||
|
||||
def get_file_url(self, file_id: str) -> str:
|
||||
"""Get a signed URL for an uploaded file."""
|
||||
"""
|
||||
Get a signed URL for an uploaded file.
|
||||
|
||||
Args:
|
||||
file_id (str): ID of the file previously uploaded to the API
|
||||
|
||||
Returns:
|
||||
str: Signed URL that can be used for OCR processing
|
||||
|
||||
Raises:
|
||||
ValueError: If the API response does not contain a URL
|
||||
requests.RequestException: If there's an error communicating with the API
|
||||
"""
|
||||
response = self.session.get(f"{self.BASE_URL}/files/{file_id}/url?expiry=24")
|
||||
response.raise_for_status()
|
||||
|
||||
@@ -85,7 +132,22 @@ class MistralClient:
|
||||
return url
|
||||
|
||||
def process_ocr(self, doc_type: str, doc_source: str, include_image_base64: bool = False) -> bytes:
|
||||
"""Process a document with OCR."""
|
||||
"""
|
||||
Process a document with OCR.
|
||||
|
||||
Args:
|
||||
doc_type (str): Type of document, either "document_url" or "image_url"
|
||||
doc_source (str): URL of the document to process
|
||||
include_image_base64 (bool, optional): Whether to include base64-encoded
|
||||
images in the response. Defaults to False.
|
||||
|
||||
Returns:
|
||||
bytes: JSON response from the API containing OCR results
|
||||
|
||||
Raises:
|
||||
ValueError: If the document type is unsupported or if processing fails
|
||||
requests.RequestException: If there's an error communicating with the API
|
||||
"""
|
||||
if doc_type not in ["document_url", "image_url"]:
|
||||
raise ValueError(f"Unsupported document type: {doc_type}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user