"use client";
import { fal } from "@fal-ai/client";
import { useState } from "react";
fal.config({
proxyUrl: "/api/fal/proxy",
});
type ErrorProps = {
error: any;
};
function Error(props: ErrorProps) {
if (!props.error) {
return null;
}
return (
Error {props.error.message}
);
}
const DEFAULT_ENDPOINT_ID = "fal-ai/llavav15-13b";
const DEFAULT_INPUT = {
prompt: "Do you know who drew this picture and what is the name of it?",
image_url: "https://llava-vl.github.io/static/images/monalisa.jpg",
max_new_tokens: 100,
temperature: 0.2,
top_p: 1,
};
export default function StreamingTest() {
// Input state
const [endpointId, setEndpointId] = useState(DEFAULT_ENDPOINT_ID);
const [input, setInput] = useState(
JSON.stringify(DEFAULT_INPUT, null, 2),
);
// Result state
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [events, setEvents] = useState([]);
const [elapsedTime, setElapsedTime] = useState(0);
const reset = () => {
setLoading(false);
setError(null);
setEvents([]);
setElapsedTime(0);
};
const run = async () => {
reset();
setLoading(true);
const start = Date.now();
try {
const stream = await fal.stream(endpointId, {
input: JSON.parse(input),
});
for await (const partial of stream) {
setEvents((events) => [partial, ...events]);
}
const result = await stream.done();
setEvents((events) => [result, ...events]);
} catch (error: any) {
setError(error);
} finally {
setLoading(false);
setElapsedTime(Date.now() - start);
}
};
return (
fal
queue
setEndpointId(e.target.value)}
/>
JSON Result
{`Elapsed Time (seconds): ${(elapsedTime / 1000).toFixed(2)}`}
{events.map((event, index) => (
{JSON.stringify(event, null, 2)}
))}
);
}