Using API routes for inference calls

This commit is contained in:
Seth Forsgren 2022-12-11 22:51:40 -08:00
parent 3b1dbae899
commit 41a1391dbf
3 changed files with 51 additions and 22 deletions

View File

@ -1,6 +1,7 @@
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { useCallback, useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { import {
AppState, AppState,
InferenceInput, InferenceInput,
@ -8,14 +9,6 @@ import {
PromptInput, PromptInput,
} from "../types"; } from "../types";
// TODO(hayk): Get this into a configuration.
const SERVER_URL = "http://129.146.52.68:3013/run_inference/";
// Baseten worklet api url. Using cors-anywhere to get around CORS issues.
const BASETEN_URL =
"https://app.baseten.co/applications/2qREaXP/production/worklets/mP7KkLP/invoke";
// Temporary basten API key "irritating-haircut"
const BASETEN_API_KEY = "JocxKmyo.g0JreAA8dZy5F20PdMxGAV34a4VGGpom";
interface ModelInferenceProps { interface ModelInferenceProps {
alpha: number; alpha: number;
seed: number; seed: number;
@ -124,26 +117,17 @@ export default function ModelInference({
setNumRequestsMade((n) => n + 1); setNumRequestsMade((n) => n + 1);
let headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
// Customize for baseten // Customize for baseten
const serverUrl = useBaseten ? BASETEN_URL : SERVER_URL; const apiHandler = useBaseten ? "/api/baseten" : "/api/server";
const payload = useBaseten const payload = useBaseten
? { worklet_input: inferenceInput } ? { worklet_input: inferenceInput }
: inferenceInput; : inferenceInput;
if (useBaseten) {
headers["Authorization"] = `Api-Key ${BASETEN_API_KEY}`;
}
const response = await fetch(serverUrl, { const response = await fetch(apiHandler, {
method: "POST", method: "POST",
headers: headers,
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
const data = await response.json(); const data = await response.json();
console.log(`Got result #${numResponsesReceived}`); console.log(`Got result #${numResponsesReceived}`);
@ -154,11 +138,20 @@ export default function ModelInference({
inferenceInput, inferenceInput,
JSON.parse(data.worklet_output.model_output) JSON.parse(data.worklet_output.model_output)
); );
} else { }
// Note, data is currently wrapped in a data field
else if (data?.data?.worklet_output?.model_output) {
newResultCallback(
inferenceInput,
JSON.parse(data.data.worklet_output.model_output)
);
}
else {
console.error("Baseten call failed: ", data); console.error("Baseten call failed: ", data);
} }
} else { } else {
newResultCallback(inferenceInput, data); // Note, data is currently wrapped in a data field
newResultCallback(inferenceInput, data.data);
} }
setNumResponsesReceived((n) => n + 1); setNumResponsesReceived((n) => n + 1);

19
pages/api/baseten.js Normal file
View File

@ -0,0 +1,19 @@
const BASETEN_URL = "https://app.baseten.co/applications/2qREaXP/production/worklets/mP7KkLP/invoke";
const BASETEN_API_KEY = "JocxKmyo.g0JreAA8dZy5F20PdMxGAV34a4VGGpom";
export default async function handler(req, res) {
let headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Authorization": `Api-Key ${BASETEN_API_KEY}`
};
const response = await fetch(BASETEN_URL, {
method: "POST",
headers: headers,
body: req.body,
});
const data = await response.json();
res.status(200).json({ data });
}

17
pages/api/server.js Normal file
View File

@ -0,0 +1,17 @@
const SERVER_URL = "http://129.146.52.68:3013/run_inference/";
export default async function handler(req, res) {
let headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
};
const response = await fetch(SERVER_URL, {
method: "POST",
headers: headers,
body: req.body,
});
const data = await response.json();
res.status(200).json({ data });
}