Refactor app entry point and initialize env variables
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
from synthetic_dataset_generator import launch
|
2 |
|
3 |
-
|
|
|
|
1 |
from synthetic_dataset_generator import launch
|
2 |
|
3 |
+
if __name__ == "__main__":
|
4 |
+
launch()
|
src/synthetic_dataset_generator/__init__.py
CHANGED
@@ -1,11 +1,14 @@
|
|
1 |
import inspect
|
2 |
-
|
3 |
from gradio import TabbedInterface
|
4 |
|
5 |
from synthetic_dataset_generator import ( # noqa
|
6 |
_distiset,
|
7 |
_inference_endpoints,
|
8 |
)
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
def launch(*args, **kwargs):
|
@@ -14,7 +17,6 @@ def launch(*args, **kwargs):
|
|
14 |
Parameters: https://www.gradio.app/docs/gradio/tabbedinterface
|
15 |
"""
|
16 |
from synthetic_dataset_generator.app import demo
|
17 |
-
|
18 |
return demo.launch(*args, **kwargs)
|
19 |
|
20 |
|
|
|
1 |
import inspect
|
|
|
2 |
from gradio import TabbedInterface
|
3 |
|
4 |
from synthetic_dataset_generator import ( # noqa
|
5 |
_distiset,
|
6 |
_inference_endpoints,
|
7 |
)
|
8 |
+
from synthetic_dataset_generator.load_env import init_environment
|
9 |
+
|
10 |
+
# Initialize environment variables
|
11 |
+
init_environment()
|
12 |
|
13 |
|
14 |
def launch(*args, **kwargs):
|
|
|
17 |
Parameters: https://www.gradio.app/docs/gradio/tabbedinterface
|
18 |
"""
|
19 |
from synthetic_dataset_generator.app import demo
|
|
|
20 |
return demo.launch(*args, **kwargs)
|
21 |
|
22 |
|
src/synthetic_dataset_generator/load_env.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from pathlib import Path
|
3 |
+
from typing import Dict
|
4 |
+
|
5 |
+
def load_env_file(env_path: Path | str) -> Dict[str, str]:
|
6 |
+
"""
|
7 |
+
Load environment variables from a .env file.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
env_path: Path to the .env file
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
Dict of environment variables loaded
|
14 |
+
|
15 |
+
Raises:
|
16 |
+
FileNotFoundError: If the .env file doesn't exist
|
17 |
+
"""
|
18 |
+
env_vars = {}
|
19 |
+
|
20 |
+
with open(env_path) as f:
|
21 |
+
for line in f:
|
22 |
+
line = line.strip()
|
23 |
+
if line and not line.startswith('#'):
|
24 |
+
key, value = line.split('=', 1)
|
25 |
+
key = key.strip()
|
26 |
+
value = value.strip()
|
27 |
+
os.environ[key] = value
|
28 |
+
env_vars[key] = value
|
29 |
+
|
30 |
+
return env_vars
|
31 |
+
|
32 |
+
|
33 |
+
def init_environment() -> Dict[str, str]:
|
34 |
+
"""
|
35 |
+
Initialize environment variables from .env file in project root.
|
36 |
+
|
37 |
+
Returns:
|
38 |
+
Dict of environment variables loaded
|
39 |
+
|
40 |
+
Raises:
|
41 |
+
FileNotFoundError: If the .env file doesn't exist
|
42 |
+
"""
|
43 |
+
root_dir = Path(__file__).parent.parent.parent
|
44 |
+
env_file = root_dir / ".env"
|
45 |
+
|
46 |
+
if not env_file.exists():
|
47 |
+
raise FileNotFoundError(
|
48 |
+
f"Environment file not found at {env_file}. "
|
49 |
+
"Please create a .env file in the project root directory."
|
50 |
+
)
|
51 |
+
|
52 |
+
return load_env_file(env_file)
|