RAHUL-13 commited on
Commit
62b5822
Β·
1 Parent(s): baec0a6

Fix: Improve environment variable loading in inference.py

Browse files

- Enhanced .env file loading to search multiple locations (script dir, cwd, home)
- Added validation to check for required environment variables before execution
- Added .strip() to remove whitespace from loaded environment values
- Provides clear error message if required variables are missing
- Fixes Phase 2 validation failure due to missing MODEL_NAME error

Files changed (1) hide show
  1. Downloads/hackathon/meta/inference.py +51 -14
Downloads/hackathon/meta/inference.py CHANGED
@@ -26,23 +26,60 @@ from openai import OpenAI
26
  from pathlib import Path
27
 
28
  # ─── Load Environment Variables from .env if it exists ───────────
29
- env_file = Path(__file__).parent / ".env"
30
- if env_file.exists():
31
- with open(env_file) as f:
32
- for line in f:
33
- line = line.strip()
34
- if line and not line.startswith("#"):
35
- key, _, value = line.partition("=")
36
- key = key.strip()
37
- value = value.strip()
38
- if key and value:
39
- os.environ.setdefault(key, value)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # ─── Configuration ────────────────────────────────────────────────
42
 
43
- API_BASE_URL = os.environ.get("API_BASE_URL", "")
44
- MODEL_NAME = os.environ.get("MODEL_NAME", "")
45
- HF_TOKEN = os.environ.get("HF_TOKEN", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Environment URL (the deployed HF Space)
48
  ENV_URL = os.environ.get(
 
26
  from pathlib import Path
27
 
28
  # ─── Load Environment Variables from .env if it exists ───────────
29
+ def load_env_file():
30
+ """Try to load .env file from multiple locations."""
31
+ possible_paths = [
32
+ Path(__file__).parent / ".env", # Same dir as script
33
+ Path.cwd() / ".env", # Current working directory
34
+ Path.home() / ".env", # Home directory
35
+ ]
36
+
37
+ for env_path in possible_paths:
38
+ if env_path.exists():
39
+ try:
40
+ with open(env_path) as f:
41
+ for line in f:
42
+ line = line.strip()
43
+ if line and not line.startswith("#"):
44
+ key, _, value = line.partition("=")
45
+ key = key.strip()
46
+ value = value.strip()
47
+ if key and value:
48
+ os.environ.setdefault(key, value)
49
+ return
50
+ except Exception:
51
+ continue
52
+
53
+ load_env_file()
54
 
55
  # ─── Configuration ────────────────────────────────────────────────
56
 
57
+ API_BASE_URL = os.environ.get("API_BASE_URL", "").strip()
58
+ MODEL_NAME = os.environ.get("MODEL_NAME", "").strip()
59
+ HF_TOKEN = os.environ.get("HF_TOKEN", "").strip()
60
+
61
+ # ─── Validate Required Environment Variables ──────────────────────
62
+
63
+ required_vars = {
64
+ "API_BASE_URL": API_BASE_URL,
65
+ "MODEL_NAME": MODEL_NAME,
66
+ "HF_TOKEN": HF_TOKEN,
67
+ }
68
+
69
+ missing_vars = [var for var, value in required_vars.items() if not value]
70
+ if missing_vars:
71
+ print(
72
+ f"❌ Missing environment variables: {', '.join(missing_vars)}",
73
+ file=sys.stderr,
74
+ )
75
+ print(
76
+ f"\nSet them before running:",
77
+ file=sys.stderr,
78
+ )
79
+ print(f"export API_BASE_URL=https://...", file=sys.stderr)
80
+ print(f"export MODEL_NAME=meta-llama/...", file=sys.stderr)
81
+ print(f"export HF_TOKEN=hf_...", file=sys.stderr)
82
+ sys.exit(1)
83
 
84
  # Environment URL (the deployed HF Space)
85
  ENV_URL = os.environ.get(