-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (62 loc) · 2.41 KB
/
app.py
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
import asyncio
import logging
from pathlib import Path
from typing import Optional, Dict, Any
from .config import config
from .exceptions import MemoraithError
from .profiler import profile_model, set_output_path
class Memoraith:
"""Main application class for Memoraith profiler."""
def __init__(self, config_file: Optional[str] = None):
self.logger = logging.getLogger(__name__)
if config_file:
config.load_from_file(config_file)
async def setup(self) -> None:
"""Initialize the profiler setup."""
try:
# Ensure output directory exists
config.output_path.mkdir(parents=True, exist_ok=True)
# Validate configuration
if not config.validate():
raise MemoraithError("Invalid configuration")
self.logger.info("Memoraith setup completed successfully")
except Exception as e:
self.logger.error(f"Setup failed: {str(e)}")
raise
@profile_model()
async def profile(self, model: Any, *args: Any, **kwargs: Any) -> Dict[str, Any]:
"""Profile a model with the current configuration."""
try:
return await self._run_profiling(model, *args, **kwargs)
except Exception as e:
self.logger.error(f"Profiling failed: {str(e)}")
raise
async def _run_profiling(self, model: Any, *args: Any, **kwargs: Any) -> Dict[str, Any]:
"""Internal method to run the profiling process."""
try:
# Model profiling is handled by the @profile_model decorator
result = await model(*args, **kwargs)
return result
except Exception as e:
self.logger.error(f"Error during profiling execution: {str(e)}")
raise
async def cleanup(self) -> None:
"""Cleanup resources after profiling."""
try:
# Add any cleanup logic here
self.logger.info("Cleanup completed successfully")
except Exception as e:
self.logger.error(f"Cleanup failed: {str(e)}")
raise
async def main() -> None:
"""Main entry point for the application."""
try:
memoraith = Memoraith()
await memoraith.setup()
# Example usage would go here
await memoraith.cleanup()
except Exception as e:
logging.error(f"Application error: {str(e)}")
raise
if __name__ == "__main__":
asyncio.run(main())