Spaces:
Sleeping

OpenEnv Contributor commited on
Commit
61cca39
·
1 Parent(s): c24afa3

fix: make HF_TOKEN optional at startup, validate at runtime for Space compatibility

Browse files
Files changed (1) hide show
  1. inference.py +14 -7
inference.py CHANGED
@@ -42,16 +42,12 @@ from cloud_soc_env import CloudSOCEnv, CloudState, SCENARIOS
42
  # Using Hugging Face Inference API for Qwen2.5-3B-Instruct
43
  API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1")
44
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-3B-Instruct")
45
- HF_TOKEN = os.getenv("HF_TOKEN")
46
 
47
- # Validate required token per hackathon requirements
48
- if HF_TOKEN is None:
49
- raise ValueError("HF_TOKEN environment variable is required")
50
-
51
- # Initialize OpenAI client with Hugging Face Inference API
52
  client = OpenAI(
53
  base_url=API_BASE_URL,
54
- api_key=HF_TOKEN
55
  )
56
 
57
  # Memory pressure settings (Mechanic #6)
@@ -181,6 +177,17 @@ def call_llm(messages: List[Dict[str, str]], temperature: float = 0.5, retry_cou
181
  Returns the raw response content.
182
  """
183
 
 
 
 
 
 
 
 
 
 
 
 
184
  # Adaptive temperature: increase slightly on retries to get different outputs
185
  # Lower baseline temp (0.5) for 3B model to be more deterministic
186
  adaptive_temp = min(0.9, temperature + (retry_count * 0.15))
 
42
  # Using Hugging Face Inference API for Qwen2.5-3B-Instruct
43
  API_BASE_URL = os.getenv("API_BASE_URL", "https://api-inference.huggingface.co/v1")
44
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-3B-Instruct")
45
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
46
 
47
+ # Initialize OpenAI client (HF_TOKEN will be validated at runtime for actual API calls)
 
 
 
 
48
  client = OpenAI(
49
  base_url=API_BASE_URL,
50
+ api_key=HF_TOKEN or "placeholder" # Allow Space to start, validate at runtime
51
  )
52
 
53
  # Memory pressure settings (Mechanic #6)
 
177
  Returns the raw response content.
178
  """
179
 
180
+ # Validate HF_TOKEN at runtime (required per hackathon guidelines)
181
+ if not HF_TOKEN:
182
+ error_msg = "HF_TOKEN environment variable is required for inference"
183
+ sys.stderr.write(f"[ERROR] {error_msg}\n")
184
+ sys.stderr.flush()
185
+ return json.dumps({
186
+ "thought": error_msg,
187
+ "tool": "aws.soc.get_alerts",
188
+ "args": {}
189
+ })
190
+
191
  # Adaptive temperature: increase slightly on retries to get different outputs
192
  # Lower baseline temp (0.5) for 3B model to be more deterministic
193
  adaptive_temp = min(0.9, temperature + (retry_count * 0.15))