Spaces:
Paused
Paused
billdthompson commited on
Commit ·
c71f0d6
1
Parent(s): b57ef1e
adding access key
Browse files1234456730 unmodified lines383940414243444546474849505152535455565758596061626364653 unmodified lines6970717273747560 unmodified lines136137138139140141142143144import osimport loggingimport torchfrom fastapi import FastAPIfrom fastapi import FastAPI, HTTPException, Dependsfrom pydantic import BaseModelfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
30 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 lines max_new_tokens: int = 256 temperature: float = 0.7 top_p: float = 0.9 access_key: str
# ---------------------------# Model loader/unloader with HF token support60 unmodified lines
@app.post("/generate")async def generate(request: GenerateRequest): # Validate access key first validate_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