coding-llm-space / api /main.py
girish00's picture
Upload folder using huggingface_hub
07a91a1 verified
"""FastAPI entrypoint for coding model."""
from fastapi import Depends, FastAPI, Request
from api.security import check_rate_limit, verify_api_key
from src.pipeline import CodingLLMPipeline
from src.schemas import GenerateRequest, GenerateResponse
app = FastAPI(title="Advanced Coding LLM API", version="1.0.0")
pipeline = CodingLLMPipeline()
@app.get("/health")
def health():
model_name = pipeline.model_bundle.active_model_name if pipeline.model_bundle else "not_loaded_yet"
return {"status": "ok", "model": model_name}
@app.post("/generate", response_model=GenerateResponse)
def generate(
payload: GenerateRequest,
request: Request,
_: None = Depends(verify_api_key),
):
client_host = request.client.host if request.client else "unknown"
check_rate_limit(client_host)
try:
result = pipeline.run(payload.instruction, payload.input)
return GenerateResponse(**result)
except Exception as exc:
# Keep response shape consistent with GenerateResponse model.
return GenerateResponse(
code="",
explanation=(
"Generation failed.\n\n"
f"Error: {exc}\n"
"Hint: Check model name/access and internet connection for first model download."
),
confidence=0.0,
important_tokens=[],
relevancy_score=0.0,
hallucination=True,
latency_ms=0,
)