1+ from typing import Optional , Union
2+ from pydantic import BaseModel , ConfigDict
3+ import mimetypes
4+ import os
5+
6+ class BaseAppInput (BaseModel ):
7+ pass
8+
9+ class BaseAppOutput (BaseModel ):
10+ pass
11+
12+ class BaseApp (BaseModel ):
13+ model_config = ConfigDict (arbitrary_types_allowed = True )
14+ async def setup (self ):
15+ pass
16+
17+ async def run (self , app_input : BaseAppInput ) -> BaseAppOutput :
18+ raise NotImplementedError ("run method must be implemented" )
19+
20+ async def unload (self ):
21+ pass
22+
23+
24+ class File (BaseModel ):
25+ """A class representing a file in the inference.sh ecosystem."""
26+ path : str # Absolute path to the file
27+ mime_type : Optional [str ] = None # MIME type of the file
28+ size : Optional [int ] = None # File size in bytes
29+ filename : Optional [str ] = None # Original filename if available
30+
31+ def __init__ (self , ** data ):
32+ super ().__init__ (** data )
33+ if not os .path .isabs (self .path ):
34+ self .path = os .path .abspath (self .path )
35+ self ._populate_metadata ()
36+
37+ def _populate_metadata (self ) -> None :
38+ """Populate file metadata from the path if it exists."""
39+ if os .path .exists (self .path ):
40+ if not self .mime_type :
41+ self .mime_type = self ._guess_mime_type ()
42+ if not self .size :
43+ self .size = self ._get_file_size ()
44+ if not self .filename :
45+ self .filename = self ._get_filename ()
46+
47+ @classmethod
48+ def from_path (cls , path : Union [str , os .PathLike ]) -> 'File' :
49+ """Create a File instance from a file path."""
50+ return cls (path = str (path ))
51+
52+ def _guess_mime_type (self ) -> Optional [str ]:
53+ """Guess the MIME type of the file."""
54+ return mimetypes .guess_type (self .path )[0 ]
55+
56+ def _get_file_size (self ) -> int :
57+ """Get the size of the file in bytes."""
58+ return os .path .getsize (self .path )
59+
60+ def _get_filename (self ) -> str :
61+ """Get the base filename from the path."""
62+ return os .path .basename (self .path )
63+
64+ def exists (self ) -> bool :
65+ """Check if the file exists."""
66+ return os .path .exists (self .path )
67+
68+ def refresh_metadata (self ) -> None :
69+ """Refresh all metadata from the file."""
70+ self ._populate_metadata ()
71+
72+ class Config :
73+ """Pydantic config"""
74+ arbitrary_types_allowed = True
0 commit comments