mirror of
https://github.com/aljazceru/dev-gpt.git
synced 2025-12-21 07:34:20 +01:00
feat: frontend
This commit is contained in:
60
server.py
60
server.py
@@ -1,7 +1,12 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from pydantic import BaseModel, HttpUrl
|
||||
from typing import Optional, Dict
|
||||
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
from main import main
|
||||
|
||||
app = FastAPI()
|
||||
@@ -9,13 +14,13 @@ app = FastAPI()
|
||||
# Define the request model
|
||||
class CreateRequest(BaseModel):
|
||||
executor_name: str
|
||||
input_executor_description: str
|
||||
executor_description: str
|
||||
input_modality: str
|
||||
input_doc_field: str
|
||||
output_modality: str
|
||||
output_doc_field: str
|
||||
input_test_in: HttpUrl
|
||||
input_test_out: str
|
||||
test_in: str
|
||||
test_out: str
|
||||
|
||||
# Define the response model
|
||||
class CreateResponse(BaseModel):
|
||||
@@ -25,17 +30,38 @@ class CreateResponse(BaseModel):
|
||||
|
||||
@app.post("/create", response_model=CreateResponse)
|
||||
async def create_endpoint(request: CreateRequest):
|
||||
try:
|
||||
result = main(
|
||||
executor_name=request.executor_name,
|
||||
input_executor_description=request.input_executor_description,
|
||||
input_modality=request.input_modality,
|
||||
input_doc_field=request.input_doc_field,
|
||||
output_modality=request.output_modality,
|
||||
output_doc_field=request.output_doc_field,
|
||||
input_test_in=request.input_test_in,
|
||||
input_test_out=request.input_test_out,
|
||||
)
|
||||
return CreateResponse(result=result, success=True, message=None)
|
||||
except Exception as e:
|
||||
return CreateResponse(result=None, success=False, message=str(e))
|
||||
|
||||
result = await main(
|
||||
executor_name=request.executor_name,
|
||||
executor_description=request.executor_description,
|
||||
input_modality=request.input_modality,
|
||||
input_doc_field=request.input_doc_field,
|
||||
output_modality=request.output_modality,
|
||||
output_doc_field=request.output_doc_field,
|
||||
test_in=request.test_in,
|
||||
test_out=request.test_out,
|
||||
do_validation=False
|
||||
)
|
||||
return CreateResponse(result=result, success=True, message=None)
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Add a custom exception handler for RequestValidationError
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content={"detail": exc.errors()},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("server:app", host="0.0.0.0", port=8000, log_level="info")
|
||||
|
||||
Reference in New Issue
Block a user