Spaces:
Paused
Paused
billdthompson commited on
Commit ·
c71f0d6
1
Parent(s): b57ef1e
adding access key
Browse files123456730 unmodified lines3839404142433 unmodified lines47484950515260 unmodified lines113114115116117118import osimport loggingimport torchfrom fastapi import FastAPIfrom pydantic import BaseModelfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline30 unmodified lines# ---------------------------app = FastAPI(title="Instruction LLM Relay")# ---------------------------# Request schema# ---------------------------3 unmodified linesmax_new_tokens: int = 256temperature: float = 0.7top_p: float = 0.9# ---------------------------# Model loader/unloader with HF token support60 unmodified lines@app.post("/generate")async def generate(request: GenerateRequest):if request.model not in SUPPORTED_MODELS:return {"error": f"Unsupported model '{request.model}'. Choose from {list(SUPPORTED_MODELS.keys())}"}123456730 unmodified lines383940414243444546474849505152535455565758596061626364653 unmodified lines6970717273747560 unmodified lines136137138139140141142143144import osimport loggingimport torchfrom fastapi import FastAPI, HTTPException, Dependsfrom pydantic import BaseModelfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline30 unmodified lines# ---------------------------app = FastAPI(title="Instruction LLM Relay")# ---------------------------# Authentication# ---------------------------def get_access_key():"""Get the access key from environment variables"""return os.environ.get("ACCESS_KEY", None)def validate_access_key(provided_key: str):"""Validate the provided access key against the stored secret"""expected_key = get_access_key()if not expected_key:logger.warning("No ACCESS_KEY set in environment - authentication disabled")return True # Allow access if no key is set (for development)if not provided_key:raise HTTPException(status_code=401, detail="Access key required")if provided_key != expected_key:raise HTTPException(status_code=401, detail="Invalid access key")return True# ---------------------------# Request schema# ---------------------------3 unmodified linesmax_new_tokens: int = 256temperature: float = 0.7top_p: float = 0.9access_key: str# ---------------------------# Model loader/unloader with HF token support60 unmodified lines@app.post("/generate")async def generate(request: GenerateRequest):# Validate access key firstvalidate_access_key(request.access_key)if request.model not in SUPPORTED_MODELS:return {"error": f"Unsupported model '{request.model}'. Choose from {list(SUPPORTED_MODELS.keys())}"}
app/main.py+27-1