File size: 891 Bytes
351b56d
fc61a2f
 
351b56d
fc61a2f
c4b9265
fc61a2f
 
 
351b56d
 
 
 
 
fc61a2f
351b56d
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request, HTTPException
import httpx

app = FastAPI()

GCP_API_URL = "http://34.63.74.181/predict" # Your GCP FastAPI endpoint

@app.post("/predict")
async def predict(request: Request):
    try:
        data = await request.json()
    except Exception:
        raise HTTPException(status_code=400, detail="Invalid JSON in request body")

    async with httpx.AsyncClient() as client:
        try:
            response = await client.post(GCP_API_URL, json=data)
            response.raise_for_status()
            result = response.json()
        except httpx.RequestError as e:
            raise HTTPException(status_code=502, detail=f"Error contacting GCP VM: {e}")
        except httpx.HTTPStatusError as e:
            raise HTTPException(status_code=response.status_code, detail=f"GCP VM error: {response.text}")

    return result