MCPcopy Index your code
hub / github.com/replicate/scribble-diffusion / handleSubmit

Function handleSubmit

pages/index.js:30–96  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

28 const [welcomeOpen, setWelcomeOpen] = useState(false);
29
30 const handleSubmit = async (e) => {
31 e.preventDefault();
32
33 // track submissions so we can show a spinner while waiting for the next prediction to be created
34 setSubmissionCount(submissionCount + 1);
35
36 const prompt = e.target.prompt.value
37 .split(/\s+/)
38 .map((word) => (naughtyWords.en.includes(word) ? "something" : word))
39 .join(" ");
40
41 setError(null);
42 setIsProcessing(true);
43
44 const fileUrl = await uploadFile(scribble);
45
46 const body = {
47 prompt,
48 image: fileUrl,
49 structure: "scribble",
50 replicate_api_token: localStorage.getItem("replicate_api_token"),
51 };
52
53 const response = await fetch("/api/predictions", {
54 method: "POST",
55 headers: {
56 "Content-Type": "application/json",
57 },
58 body: JSON.stringify(body),
59 });
60 let prediction = await response.json();
61
62 setPredictions((predictions) => ({
63 ...predictions,
64 [prediction.id]: prediction,
65 }));
66
67 if (response.status !== 201) {
68 setError(prediction.detail);
69 return;
70 }
71
72 while (
73 prediction.status !== "succeeded" &&
74 prediction.status !== "failed"
75 ) {
76 await sleep(500);
77 const response = await fetch("/api/predictions/" + prediction.id, {
78 headers: {
79 Authorization: `Bearer ${localStorage.getItem(
80 "replicate_api_token"
81 )}`,
82 },
83 });
84 prediction = await response.json();
85 setPredictions((predictions) => ({
86 ...predictions,
87 [prediction.id]: prediction,

Callers

nothing calls this directly

Calls 2

uploadFileFunction · 0.85
sleepFunction · 0.85

Tested by

no test coverage detected