Spaces:
Sleeping
Sleeping
File size: 837 Bytes
1a96a44 ea0af80 214e325 ea0af80 214e325 ea0af80 214e325 ea0af80 214e325 ea0af80 1a96a44 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from model import generate_code
app = FastAPI()
# Define request body schema
class GenerateRequest(BaseModel):
prompt: str
max_tokens: int = 256 # Default value
@app.get("/")
def home():
return {"message": "Code Generation API is running!"}
@app.post("/generate")
def generate(request: GenerateRequest): # Expect JSON
if not request.prompt:
raise HTTPException(status_code=400, detail="Prompt cannot be empty.")
try:
code = generate_code(request.prompt, request.max_tokens)
return {"generated_code": code}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|