-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
124 lines (105 loc) · 4.61 KB
/
config.py
File metadata and controls
124 lines (105 loc) · 4.61 KB
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
import os
import logging
from typing import Dict, List, Tuple, Optional
from dotenv import load_dotenv
# Logging for configuration issues
logger = logging.getLogger(__name__)
# Safely loaded environment variables
try:
load_dotenv()
logger.info("Environment variables loaded successfully")
except Exception as e:
logger.warning(f"Failed to load .env file: {e}")
class Config:
"""Configuration for Scribbly - AI Study Helper"""
# Service Credentials
AZURE_DOC_INTELLIGENCE_ENDPOINT = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT")
AZURE_DOC_INTELLIGENCE_KEY = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_KEY")
AZURE_LANGUAGE_ENDPOINT = os.getenv("AZURE_LANGUAGE_ENDPOINT")
AZURE_LANGUAGE_KEY = os.getenv("AZURE_LANGUAGE_KEY")
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# Application Settings
PAGE_TITLE = "🧠 Scribbly - AI Study Helper"
PAGE_ICON = "🧠"
PROGRESS_STEPS = ["📁 Upload", "🎯 Choose", "🔍 Process", "📚 Study"]
# File Processing Settings
SUPPORTED_FILE_TYPES = ['pdf', 'jpg', 'jpeg', 'png', 'txt', 'docx']
# Content Generation Settings
DEFAULT_FLASHCARD_COUNT = 5
MAX_FLASHCARD_COUNT = 20
MAX_KEY_PHRASES = 15
# Processing Configuration
class ProcessingLimits:
"""Centralized processing limits with business justification"""
CHUNK_SIZE_DEFAULT = 4000 # (Azure 5KB limit)
SUMMARY_SENTENCES_MAX = 5 # Optimal summary length for study
KEY_PHRASES_MAX = 15 # Good balance for flashcard generation
FLASHCARD_INPUT_MAX_WORDS = 1000 # Prevents API token limit issues
@classmethod
def _safe_int_from_env(cls, key: str, default: int) -> int:
"""Safely parse integer from environment variable"""
try:
value = int(os.getenv(key, str(default)))
logger.debug(f"Loaded {key}={value}")
return value
except (ValueError, TypeError):
logger.warning(f"Invalid {key} in environment, using default: {default}")
return default
# Initializes calculated values safely
MAX_FILE_SIZE_MB = _safe_int_from_env.__func__(None, 'MAX_FILE_SIZE_MB', 10)
MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024
# Service availability checks
@classmethod
def has_azure_document(cls) -> bool:
"""Check if Azure Document Intelligence is configured"""
return bool(cls.AZURE_DOC_INTELLIGENCE_ENDPOINT and cls.AZURE_DOC_INTELLIGENCE_KEY)
@classmethod
def has_azure_language(cls) -> bool:
"""Check if Azure Language Services is configured"""
return bool(cls.AZURE_LANGUAGE_ENDPOINT and cls.AZURE_LANGUAGE_KEY)
@classmethod
def has_gemini(cls) -> bool:
"""Check if Gemini AI is configured"""
return bool(cls.GEMINI_API_KEY)
@classmethod
def get_available_services(cls) -> Dict[str, bool]:
"""Get status of all AI services"""
return {
"azure_document_intelligence": cls.has_azure_document(),
"azure_language_services": cls.has_azure_language(),
"gemini_ai": cls.has_gemini()
}
@classmethod
def validate_file_type(cls, file_extension: str) -> bool:
"""Check if file type is supported"""
return file_extension.lower() in cls.SUPPORTED_FILE_TYPES
@classmethod
def validate_file_size(cls, file_size_bytes: int) -> Dict[str, any]:
"""Validate file size with detailed error message"""
is_valid = file_size_bytes <= cls.MAX_FILE_SIZE_BYTES
if is_valid:
return {'valid': True}
size_mb = file_size_bytes / (1024 * 1024)
return {
'valid': False,
'error': f"File too large ({size_mb:.1f}MB). Maximum allowed: {cls.MAX_FILE_SIZE_MB}MB"
}
@classmethod
def is_ready(cls) -> Tuple[bool, str]:
"""Check if minimum services are available for basic functionality"""
if not cls.has_azure_document():
return False, "Missing Azure Document Intelligence credentials"
if not cls.has_gemini():
return False, "Missing Gemini API key"
return True, "Ready"
@classmethod
def get_missing_services(cls) -> List[str]:
"""Get list of missing service configurations for debugging"""
missing = []
if not cls.has_azure_document():
missing.append("Azure Document Intelligence")
if not cls.has_azure_language():
missing.append("Azure Language Services")
if not cls.has_gemini():
missing.append("Gemini AI")
return missing