Files
2026-10-19 10:59:31 +05:30

87 lines
3.4 KiB
Python

"""
Configuration module for the Handwritten Marathi Extraction Application.
Loads settings from environment variables and provides defaults.
"""
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class Config:
"""Application configuration loaded from environment variables."""
# Flask
SECRET_KEY = os.getenv("FLASK_SECRET_KEY", "dev-secret-key-change-in-production")
DEBUG = os.getenv("FLASK_DEBUG", "True").lower() in ("true", "1", "yes")
PORT = int(os.getenv("FLASK_PORT", 5000))
# Upload
MAX_CONTENT_LENGTH = int(os.getenv("MAX_CONTENT_LENGTH_MB", 16)) * 1024 * 1024
UPLOAD_FOLDER = os.getenv("UPLOAD_FOLDER", "uploads")
ALLOWED_EXTENSIONS = {"jpg", "jpeg", "png", "pdf", "bmp", "tiff", "tif"}
# Sarvam AI API
SARVAM_API_KEY = os.getenv("SARVAM_API_KEY", "")
SARVAM_API_BASE_URL = os.getenv("SARVAM_API_BASE_URL", "https://api.sarvam.ai")
SARVAM_JOB_POLL_INTERVAL = int(os.getenv("SARVAM_JOB_POLL_INTERVAL", 3))
SARVAM_JOB_MAX_WAIT = int(os.getenv("SARVAM_JOB_MAX_WAIT", 120))
# Sarvam Doc-AI Endpoints
SARVAM_DIGITISE_URL = f"{SARVAM_API_BASE_URL}/doc-ai/v1/job/digitise"
SARVAM_EXTRACT_URL = f"{SARVAM_API_BASE_URL}/doc-ai/v1/job/extract"
SARVAM_JOB_STATUS_URL = f"{SARVAM_API_BASE_URL}/doc-ai/v1/job/{{job_id}}/status"
SARVAM_JOB_RESULTS_URL = f"{SARVAM_API_BASE_URL}/doc-ai/v1/job/{{job_id}}/results"
# Google Gemini Vision Configuration
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY", "")
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-3.8-flash")
GEMINI_API_BASE_URL = os.getenv("GEMINI_API_BASE_URL", "https://generativelanguage.googleapis.com/v1beta")
# Language settings
DOCUMENT_LANGUAGE = "mr-IN" # Marathi (BCP-47)
CONTENT_TYPE = "handwritten" # handwritten, printed, or mixed
@classmethod
def get_sarvam_api_key(cls):
"""Get the current Sarvam API key, reloading from .env if updated."""
load_dotenv(override=True)
return os.getenv("SARVAM_API_KEY", "")
@classmethod
def get_gemini_api_key(cls):
"""Get the current Google Gemini API key, reloading from .env if updated."""
load_dotenv(override=True)
return os.getenv("GEMINI_API_KEY", "")
@classmethod
def get_gemini_model(cls):
"""Get the configured Gemini vision model name (e.g. gemini-3.8-flash, gemini-3.6-flash)."""
load_dotenv(override=True)
return os.getenv("GEMINI_MODEL", "gemini-3.8-flash")
@classmethod
def get_configured_models(cls):
"""Return status of which vision models are configured and available."""
sarvam_key = cls.get_sarvam_api_key()
gemini_key = cls.get_gemini_api_key()
return {
"sarvam": bool(sarvam_key and sarvam_key != "your_sarvam_api_key_here"),
"gemini": bool(gemini_key and gemini_key != "your_gemini_api_key_here"),
"gemini_model": cls.get_gemini_model(),
}
@classmethod
def validate(cls):
"""Validate that at least one vision API key is configured."""
configured = cls.get_configured_models()
errors = []
if not configured["sarvam"] and not configured["gemini"]:
errors.append(
"Neither Sarvam Vision API key nor Google Gemini API key is configured. "
"Please configure at least one in your .env file."
)
return errors