From b01874986fee671807f4f7a7ab2996b34c0f1eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aykut=20Karda=C5=9F?= Date: Sun, 3 Mar 2024 13:53:53 +0300 Subject: [PATCH] refactor: split code and improve style --- app/page.tsx | 311 +++++++------------------------- components/model-compare.tsx | 79 ++++++++ components/original-compare.tsx | 119 ++++++++++++ 3 files changed, 261 insertions(+), 248 deletions(-) create mode 100644 components/model-compare.tsx create mode 100644 components/original-compare.tsx diff --git a/app/page.tsx b/app/page.tsx index 151cd62..c0fbe34 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,32 +3,15 @@ /* eslint-disable @next/next/no-img-element */ /* eslint-disable @next/next/no-html-link-for-pages */ import * as fal from "@fal-ai/serverless-client"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useState } from "react"; +import Link from "next/link"; import { Input } from "@/components/ui/input"; import { ModelIcon } from "@/components/icons/model-icon"; -import Link from "next/link"; -import { - ReactCompareSlider, - ReactCompareSliderImage, -} from "react-compare-slider"; -import { cx } from "class-variance-authority"; - -function randomSeed() { - return Math.floor(Math.random() * 10000000).toFixed(0); -} - -fal.config({ - proxyUrl: "/api/proxy", -}); +import { cn } from "@/lib/utils"; +import ModelCompare from "@/components/model-compare"; +import OriginalCompare from "@/components/original-compare"; -const INPUT_DEFAULTS = { - _force_msgpack: new Uint8Array([]), - enable_safety_checker: true, - image_size: "square_hd", - sync_mode: true, - num_images: 1, - num_inference_steps: "2", -}; +fal.config({ proxyUrl: "/api/proxy" }); interface ModelResult { name: string; @@ -39,17 +22,18 @@ interface ModelResult { type CompareMode = "original" | "model"; export default function Lightning() { - const [image, setImage] = useState(null); const [mode, setMode] = useState("original"); - const [inferenceTime, setInferenceTime] = useState(NaN); const [position, setPosition] = useState(50); const [originalImage, setOriginalImage] = useState(null); const [modelOne, setModelOne] = useState(null); + const [modelOneLoading, setModelOneLoading] = useState(false); const [modelTwo, setModelTwo] = useState(null); + const [modelTwoLoading, setModelTwoLoading] = useState(false); const upscaleWithModelOne = async (file: File) => { let inferenceTime; + setModelOneLoading(true); const result: Record = await fal.subscribe("fal-ai/ccsr", { input: { @@ -73,15 +57,16 @@ export default function Lightning() { image: result.image.url as string, inferenceTime, }); - setImage(result.image.url as string); } - console.log(result); + setModelOneLoading(false); }; const upscaleWithModelTwo = async (file: File) => { let inferenceTime; + setModelTwoLoading(true); + const result: Record = await fal.subscribe("fal-ai/supir", { input: { image_url: file, @@ -104,14 +89,18 @@ export default function Lightning() { image: result.image.url as string, inferenceTime, }); - setImage(result.image.url as string); } + + setModelTwoLoading(false); }; const handleOnChange = async (file: File) => { const blobUrl = URL.createObjectURL(file); setOriginalImage(blobUrl); + setModelOne(null); + setModelTwo(null); + upscaleWithModelOne(file); upscaleWithModelTwo(file); }; @@ -126,10 +115,12 @@ export default function Lightning() {
-
-
-
- +
+
+
+ { @@ -137,231 +128,55 @@ export default function Lightning() { handleOnChange(e.target?.files?.[0]); } }} - className="font-light w-full mx-auto" + className="font-light mx-auto rounded-l-full h-10 pr-10 truncate" placeholder="Type something..." />
+
+ +
+ setMode("original")} + className={cn( + mode === "original" && "bg-neutral-200 text-black", + "w-1/2 cursor-pointer text-sm rounded-full h-8 px-3 flex items-center" + )} + > + Original + + setMode("model")} + className={cn( + mode === "model" && "bg-neutral-200 text-black", + "w-1/2 cursor-pointer text-sm rounded-full h-8 px-3 flex items-center" + )} + > + Model + +
+
{mode === "model" && ( -
-
- {image && ( -
- setPosition(position)} - itemOne={ - <> -
- CCSR - {modelOne && ( - - {modelOne - ? `${(modelOne.inferenceTime * 1000).toFixed( - 0 - )}ms` - : `n/a`} - - )} -
- - - } - itemTwo={ - <> -
- {modelTwo && ( - - {modelTwo - ? `${(modelTwo.inferenceTime * 1000).toFixed( - 0 - )}ms` - : `n/a`} - - )} - SUPIR -
- - - } - /> -
- )} -
-
+ )} {mode === "original" && ( -
-
- {image && modelOne && ( -
-
- Modal: - - CCSR Upscaler - -
-
- Inference time: - - {modelOne - ? `${(modelOne.inferenceTime * 1000).toFixed(0)}ms` - : `n/a`} - -
-
- )} - {image && ( -
- setPosition(position)} - itemOne={ - <> -
- Original -
- - - } - itemTwo={ - <> -
- CCSR -
- - - } - /> - {/* {image && ( - Dynamic Image - )} */} -
- )} -
- -
- {image && modelTwo && ( -
-
- Modal: - - SUPIR Upscaler - -
-
- Inference time: - - {modelTwo - ? `${(modelTwo.inferenceTime * 1000).toFixed(0)}ms` - : `n/a`} - -
-
- )} - {image && ( -
- setPosition(position)} - itemOne={ - <> -
- Original -
- - - } - itemTwo={ - <> -
- SUPIR -
- - - } - /> - {/* {image && ( - Dynamic Image - )} */} -
- )} -
-
+ )} -
-
- -
- setMode("original")} - className={cx( - mode === "original" && "bg-neutral-800", - "w-1/2 cursor-pointer text-neutral-400 text-sm rounded-full h-8 px-3 flex items-center" - )} - > - Original - - setMode("model")} - className={cx( - mode === "model" && "bg-neutral-800", - "w-1/2 cursor-pointer text-neutral-400 text-sm rounded-full h-8 px-3 flex items-center" - )} - > - Model - -
-
-

diff --git a/components/model-compare.tsx b/components/model-compare.tsx new file mode 100644 index 0000000..94aec0e --- /dev/null +++ b/components/model-compare.tsx @@ -0,0 +1,79 @@ +import { cn } from "@/lib/utils"; +import { + ReactCompareSlider, + ReactCompareSliderImage, +} from "react-compare-slider"; + +const ModelCompare = ({ + originalImage, + position, + setPosition, + modelOne, + modelTwo, +}) => { + return ( +

+
+ {originalImage ? ( +
+ setPosition(position)} + itemOne={ + <> +
+ CCSR + {modelOne && ( + + {modelOne + ? `${(modelOne.inferenceTime * 1000).toFixed(0)}ms` + : `n/a`} + + )} +
+ + + } + itemTwo={ + <> +
+ {modelTwo && ( + + {modelTwo + ? `${(modelTwo.inferenceTime * 1000).toFixed(0)}ms` + : `n/a`} + + )} + SUPIR +
+ + + } + /> +
+ ) : ( +
+ )} +
+
+ ); +}; + +export default ModelCompare; diff --git a/components/original-compare.tsx b/components/original-compare.tsx new file mode 100644 index 0000000..3562226 --- /dev/null +++ b/components/original-compare.tsx @@ -0,0 +1,119 @@ +import { + ReactCompareSlider, + ReactCompareSliderImage, +} from "react-compare-slider"; + +const OriginalCompare = ({ + originalImage, + position, + setPosition, + modelOne, + modelTwo, +}) => { + return ( +
+
+ {originalImage ? ( +
+ setPosition(position)} + itemOne={ + <> +
+ Original +
+ + + } + itemTwo={ + <> +
+ CCSR + {modelOne && ( + + {modelOne + ? `${(modelOne.inferenceTime * 1000).toFixed(0)}ms` + : `n/a`} + + )} +
+ + + } + /> + {/* {image && ( + Dynamic Image + )} */} +
+ ) : ( +
+ )} +
+ +
+ {originalImage ? ( +
+ setPosition(position)} + itemOne={ + <> +
+ Original +
+ + + } + itemTwo={ + <> +
+ {modelTwo && ( + + {modelTwo + ? `${(modelTwo.inferenceTime * 1000).toFixed(0)}ms` + : `n/a`} + + )} + SUPIR +
+ + + } + /> +
+ ) : ( +
+ )} +
+
+ ); +}; + +export default OriginalCompare;