ai_backend / env.py
sumit989's picture
Update env.py
6baaf59 verified
# env.py β€” loads and validates all environment variables for CodeRescue
import os
from dotenv import load_dotenv
load_dotenv()
# ── API / Model ────────────────────────────────────────────────────────────────
API_BASE_URL: str = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME: str = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-Coder-7B-Instruct")
API_KEY: str = os.getenv("API_KEY") or os.getenv("HF_TOKEN") or ""
# ── CORS ───────────────────────────────────────────────────────────────────────
ALLOWED_ORIGINS: list[str] = os.getenv(
"ALLOWED_ORIGINS",
"https://sumit989bishnoi-crypto.github.io"
).split(",")
# ── Server ─────────────────────────────────────────────────────────────────────
PORT: int = int(os.getenv("PORT", 7860))
# ── Validation ─────────────────────────────────────────────────────────────────
def validate() -> None:
"""Call once at startup. Warns about missing critical variables."""
if not API_KEY:
print("[env] WARNING: No API_KEY or HF_TOKEN set β€” AI calls will fail.")
else:
print(f"[env] API_KEY : {'*' * 6}{API_KEY[-4:]}")
print(f"[env] API_BASE_URL: {API_BASE_URL}")
print(f"[env] MODEL_NAME : {MODEL_NAME}")
print(f"[env] ALLOWED_ORIGINS: {ALLOWED_ORIGINS}")
print(f"[env] PORT : {PORT}")
# ── CodeEnv ────────────────────────────────────────────────────────────────────
class CodeEnv:
def __init__(self):
self.tasks = [
{
"code": "a=5\nb=0\nprint(a/b)",
"solution": "a=5\nb=1\nprint(a/b)",
"difficulty": "easy"
},
{
"code": "print(x)",
"solution": "x=0\nprint(x)",
"difficulty": "medium"
},
{
"code": "for i in range(5)\n print(i)",
"solution": "for i in range(5):\n print(i)",
"difficulty": "hard"
}
]
self.index = 0
self.current = None
def reset(self):
self.current = self.tasks[self.index % len(self.tasks)]
self.index += 1
return self.state()
def state(self):
return {
"code": self.current["code"],
"difficulty": self.current["difficulty"]
}
def step(self, action):
fixed = action.get("fixed_code", "")
if self.current["solution"] in fixed:
reward = 1.0
elif len(fixed.strip()) > 0:
reward = 0.5
else:
reward = 0.0
reward = float(reward)
done = True
return self.state(), reward, done, {}
if __name__ == "__main__":
validate()