Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- inference.py +3 -0
- server/token_optimiser_environment.py +22 -17
inference.py
CHANGED
|
@@ -228,6 +228,9 @@ async def run_episode(llm: OpenAI) -> None:
|
|
| 228 |
|
| 229 |
if done:
|
| 230 |
break
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
# Score = average reward across steps, clamped to [0, 1]
|
| 233 |
score = sum(rewards) / len(rewards) if rewards else 0.0
|
|
|
|
| 228 |
|
| 229 |
if done:
|
| 230 |
break
|
| 231 |
+
|
| 232 |
+
# Wait between steps to avoid rate limiting
|
| 233 |
+
await asyncio.sleep(2.5)
|
| 234 |
|
| 235 |
# Score = average reward across steps, clamped to [0, 1]
|
| 236 |
score = sum(rewards) / len(rewards) if rewards else 0.0
|
server/token_optimiser_environment.py
CHANGED
|
@@ -50,7 +50,7 @@ class TokenOptimiserEnvironment(Environment):
|
|
| 50 |
# Hybrid LLM client — reads credentials from env vars at startup
|
| 51 |
api_key = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
|
| 52 |
api_base = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 53 |
-
self._model = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-
|
| 54 |
if OpenAI and api_key:
|
| 55 |
self._llm = OpenAI(base_url=api_base, api_key=api_key)
|
| 56 |
else:
|
|
@@ -178,24 +178,29 @@ class TokenOptimiserEnvironment(Environment):
|
|
| 178 |
|
| 179 |
def _call_llm(self, prompt: str) -> tuple[str, int, int]:
|
| 180 |
"""
|
| 181 |
-
Call the real LLM with
|
| 182 |
-
Returns (response_text, input_tokens, output_tokens).
|
| 183 |
-
Falls back to rule-based simulation if LLM is unavailable.
|
| 184 |
"""
|
| 185 |
if self._llm is not None:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
# Rule-based fallback
|
| 201 |
return self._fallback_simulate(prompt)
|
|
|
|
| 50 |
# Hybrid LLM client — reads credentials from env vars at startup
|
| 51 |
api_key = os.getenv("HF_TOKEN") or os.getenv("API_KEY")
|
| 52 |
api_base = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 53 |
+
self._model = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
|
| 54 |
if OpenAI and api_key:
|
| 55 |
self._llm = OpenAI(base_url=api_base, api_key=api_key)
|
| 56 |
else:
|
|
|
|
| 178 |
|
| 179 |
def _call_llm(self, prompt: str) -> tuple[str, int, int]:
|
| 180 |
"""
|
| 181 |
+
Call the real LLM with retries.
|
|
|
|
|
|
|
| 182 |
"""
|
| 183 |
if self._llm is not None:
|
| 184 |
+
import time
|
| 185 |
+
for attempt in range(2): # Try twice
|
| 186 |
+
try:
|
| 187 |
+
resp = self._llm.chat.completions.create(
|
| 188 |
+
model=self._model,
|
| 189 |
+
messages=[{"role": "user", "content": prompt}],
|
| 190 |
+
max_tokens=200,
|
| 191 |
+
temperature=0.3,
|
| 192 |
+
)
|
| 193 |
+
text = (resp.choices[0].message.content or "").strip()
|
| 194 |
+
in_tok = resp.usage.prompt_tokens if resp.usage else len(prompt.split())
|
| 195 |
+
out_tok = resp.usage.completion_tokens if resp.usage else len(text.split())
|
| 196 |
+
return text, in_tok, out_tok
|
| 197 |
+
except Exception as e:
|
| 198 |
+
if "429" in str(e) or "Too Many Requests" in str(e):
|
| 199 |
+
print(f"[ENV] Rate limited, waiting 3s (attempt {attempt+1})...")
|
| 200 |
+
time.sleep(3)
|
| 201 |
+
else:
|
| 202 |
+
print(f"[ENV] LLM call failed: {e}")
|
| 203 |
+
break
|
| 204 |
|
| 205 |
# Rule-based fallback
|
| 206 |
return self._fallback_simulate(prompt)
|