File size: 21,269 Bytes
9a42933 b0f34ee 9a42933 bd74bd1 9a42933 ee5bd94 9a42933 1cf03f7 4ae2e41 b0f34ee 930b21b 4ae2e41 1cf03f7 ee5bd94 9a42933 930b21b d243e97 9a42933 bd74bd1 ee5bd94 9a42933 b0f34ee cfbfdc3 b0f34ee 9a42933 b0f34ee 80175c8 b0f34ee 4ae2e41 d243e97 930b21b ee5bd94 d85329e 80175c8 b0f34ee 80175c8 cfbfdc3 80175c8 9a42933 415446d 9a42933 b0f34ee d243e97 9a42933 d243e97 b0f34ee 9a42933 4ae2e41 b0f34ee ee5bd94 b0f34ee 930b21b 71c284d 930b21b ee5bd94 930b21b ee5bd94 930b21b d243e97 9a42933 d85329e d243e97 18e5e63 d85329e ee5bd94 6eef442 ee5bd94 d85329e 18e5e63 d243e97 18e5e63 ee5bd94 6eef442 ee5bd94 d85329e 18e5e63 d243e97 9a42933 d243e97 d85329e d243e97 1cf03f7 d243e97 d85329e d243e97 cfbfdc3 d243e97 ee5bd94 d243e97 9a42933 b0f34ee f9e9c71 4ae2e41 b0f34ee 4ae2e41 ee5bd94 4ae2e41 f9e9c71 4ae2e41 f9e9c71 4ae2e41 7b575e2 49a7080 4ae2e41 b0f34ee 4ae2e41 ee5bd94 4ae2e41 f9e9c71 4ae2e41 cfbfdc3 9a42933 4ae2e41 9a42933 4ae2e41 80175c8 4ae2e41 cfbfdc3 4ae2e41 9a42933 4ae2e41 930b21b 9a42933 4ae2e41 9a42933 d243e97 8bc9511 cfbfdc3 8bc9511 cfbfdc3 930b21b 8bc9511 9a42933 8bc9511 9a42933 4ae2e41 9a42933 4ae2e41 9a42933 7448744 9a42933 415446d 9a42933 b0f34ee d243e97 b0f34ee 930b21b b0f34ee 4ae2e41 2e5e37b b0f34ee 9a42933 2e5e37b 9a42933 2e5e37b 4ae2e41 b0f34ee 4ae2e41 b0f34ee d32768f b0f34ee 4ae2e41 b0f34ee 4ae2e41 bd74bd1 9a42933 bd74bd1 4ae2e41 7b575e2 4ae2e41 9a42933 4ae2e41 b0f34ee 4ae2e41 9a42933 |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
"use client"
import { useEffect, useRef, useState, useTransition } from "react"
import { useSpring, animated } from "@react-spring/web"
import { usePathname, useRouter, useSearchParams } from "next/navigation"
import { useToast } from "@/components/ui/use-toast"
import { cn } from "@/lib/utils"
import { headingFont } from "@/app/interface/fonts"
import { useCharacterLimit } from "@/lib/useCharacterLimit"
import { generateAnimation } from "@/app/server/actions/animation"
import { interpolateVideo } from "@/app/server/actions/interpolation"
import { getLatestPosts, getPost, postToCommunity } from "@/app/server/actions/community"
import { getSDXLModels } from "@/app/server/actions/models"
import { HotshotImageInferenceSize, Post, QualityLevel, QualityOption, SDXLModel } from "@/types"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"
import { TooltipProvider } from "@radix-ui/react-tooltip"
import { isRateLimitError } from "@/app/server/utils/isRateLimitError"
import { useCountdown } from "@/lib/useCountdown"
import { Countdown } from "../countdown"
const qualityOptions = [
{
level: "low",
label: "Low (~ 30 sec)"
},
{
level: "medium",
label: "Medium (~90 secs)"
}
] as QualityOption[]
type Stage = "generate" | "interpolate" | "finished"
export function Generate() {
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
const searchParamsEntries = searchParams ? Array.from(searchParams.entries()) : []
const [_isPending, startTransition] = useTransition()
const scrollRef = useRef<HTMLDivElement>(null)
const videoRef = useRef<HTMLVideoElement>(null)
const [isLocked, setLocked] = useState(false)
const [promptDraft, setPromptDraft] = useState("")
const [assetUrl, setAssetUrl] = useState("")
const [isOverSubmitButton, setOverSubmitButton] = useState(false)
const [models, setModels] = useState<SDXLModel[]>([])
const [selectedModel, setSelectedModel] = useState<SDXLModel>()
const [runs, setRuns] = useState(0)
const runsRef = useRef(0)
const [showModels, setShowModels] = useState(true)
const [communityRoll, setCommunityRoll] = useState<Post[]>([])
const [stage, setStage] = useState<Stage>("generate")
const [qualityLevel, setQualityLevel] = useState<QualityLevel>("low")
const { toast } = useToast()
const { progressPercent, remainingTimeInSec } = useCountdown({
isActive: isLocked,
timerId: runs, // everytime we change this, the timer will reset
durationInSec: /*stage === "interpolate" ? 30 :*/ 90, // it usually takes 40 seconds, but there might be lag
onEnd: () => {}
})
const { shouldWarn, colorClass, nbCharsUsed, nbCharsLimits } = useCharacterLimit({
value: promptDraft,
nbCharsLimits: 70,
warnBelow: 10,
})
const submitButtonBouncer = useSpring({
transform: isOverSubmitButton
? 'scale(1.05)'
: 'scale(1.0)',
boxShadow: isOverSubmitButton
? `0px 5px 15px 0px rgba(0, 0, 0, 0.05)`
: `0px 0px 0px 0px rgba(0, 0, 0, 0.05)`,
loop: true,
config: {
tension: 300,
friction: 10,
},
})
const handleSubmit = () => {
if (isLocked) { return }
if (!promptDraft) { return }
setShowModels(false)
setRuns(runsRef.current + 1)
setLocked(true)
setStage("generate")
scrollRef.current?.scroll({
top: 0,
behavior: 'smooth'
})
startTransition(async () => {
const huggingFaceLora = selectedModel ? selectedModel.repo.trim() : "KappaNeuro/studio-ghibli-style"
const triggerWord = selectedModel ? selectedModel.trigger_word : "Studio Ghibli Style"
// now you got a read/write object
const current = new URLSearchParams(searchParamsEntries)
current.set("prompt", promptDraft)
current.set("model", huggingFaceLora)
const search = current.toString()
router.push(`${pathname}${search ? `?${search}` : ""}`)
const size: HotshotImageInferenceSize = "608x416"
// 608x416 @ 25 steps -> 32 seconds
const steps = qualityLevel === "low" ? 30 : 45
let key = ""
try {
const res = await fetch("/api/get-key", {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
cache: 'no-store',
})
key = await res.text()
} catch (err) {
console.error("failed to get key, but this is not a blocker")
}
const params = {
positivePrompt: promptDraft,
negativePrompt: "",
huggingFaceLora,
triggerWord,
nbFrames: 10, // if duration is 1000ms then it means 8 FPS
duration: 1000, // in ms
steps,
size,
key
}
let rawAssetUrl = ""
try {
// console.log("starting transition, calling generateAnimation")
rawAssetUrl = await generateAnimation(params)
if (!rawAssetUrl) {
throw new Error("invalid asset url")
}
setAssetUrl(rawAssetUrl)
} catch (err) {
// check the rate limit
if (isRateLimitError(err)) {
console.error("error, too many requests")
toast({
title: "You can generate only one video per minute 👀",
description: "Please wait a bit before trying again 🤗",
})
setLocked(false)
return
} else {
toast({
title: "We couldn't generate your video 👀",
description: "We are probably over capacity, but you can try again 🤗",
})
}
console.log("generation failed! probably just a Gradio failure, so let's just run the round robin again!")
try {
rawAssetUrl = await generateAnimation(params)
} catch (err) {
// check the rate limit
if (isRateLimitError(err)) {
console.error("error, too many requests")
toast({
title: "Error: the free server is over capacity 👀",
description: "You can generate 2 videos per minute 🤗 Please try again in a moment!",
})
setLocked(false)
return
}
console.error(`generation failed again! ${err}`)
}
}
if (!rawAssetUrl) {
console.log("failed to generate the video, aborting")
setLocked(false)
return
}
setAssetUrl(rawAssetUrl)
let assetUrl = rawAssetUrl
setStage("interpolate")
setRuns(runsRef.current + 1)
try {
assetUrl = await interpolateVideo(rawAssetUrl)
if (!assetUrl) {
throw new Error("invalid interpolated asset url")
}
setAssetUrl(assetUrl)
} catch (err) {
console.log(`failed to interpolate the video, but this is not a blocker: ${err}`)
}
setLocked(false)
setStage("generate")
if (process.env.NEXT_PUBLIC_ENABLE_COMMUNITY_SHARING !== "true") {
return
}
try {
const post = await postToCommunity({
prompt: promptDraft,
model: huggingFaceLora,
assetUrl,
})
console.log("successfully submitted to the community!", post)
// now you got a read/write object
const current = new URLSearchParams(searchParamsEntries)
current.set("postId", post.postId.trim())
current.set("prompt", post.prompt.trim())
current.set("model", post.model.trim())
const search = current.toString()
router.push(`${pathname}${search ? `?${search}` : ""}`)
} catch (err) {
console.error(`not a blocker, but we failed to post to the community (reason: ${err})`)
}
})
}
useEffect(() => {
startTransition(async () => {
const models = await getSDXLModels()
setModels(models)
const defaultModel = models.find(model => model.repo.toLowerCase().includes("ghibli")) || models[0]
if (defaultModel) {
setSelectedModel(defaultModel)
}
// now we load URL params
const current = new URLSearchParams(searchParamsEntries)
// URL query params
const existingPostId = current.get("postId") || ""
const existingPrompt = current.get("prompt")?.trim() || ""
const existingModelName = current.get("model")?.toLowerCase().trim() || ""
// if and only if we don't have a post id, then we look at the other query params
if (existingPrompt) {
setPromptDraft(existingPrompt)
}
if (existingModelName) {
let existingModel = models.find(model => {
return (
model.repo.toLowerCase().trim().includes(existingModelName)
|| model.title.toLowerCase().trim().includes(existingModelName)
)
})
if (existingModel) {
setSelectedModel(existingModel)
}
}
// if we have a post id, then we use that to override all the previous values
if (existingPostId) {
try {
const post = await getPost(existingPostId)
if (post.assetUrl) {
setAssetUrl(post.assetUrl)
}
if (post.prompt) {
setPromptDraft(post.prompt)
}
if (post.model) {
const nameToFind = post.model.toLowerCase().trim()
const existingModel = models.find(model => {
return (
model.repo.toLowerCase().trim().includes(nameToFind)
|| model.title.toLowerCase().trim().includes(nameToFind)
)
})
if (existingModel) {
setSelectedModel(existingModel)
}
}
} catch (err) {
console.error(`failed to load the community post (${err})`)
}
}
})
}, [])
useEffect(() => {
startTransition(async () => {
const posts = await getLatestPosts({
maxNbPosts: 32,
shuffle: true,
})
if (posts?.length) {
setCommunityRoll(posts)
}
})
}, [])
const handleSelectCommunityPost = (post: Post) => {
if (isLocked) { return }
scrollRef.current?.scroll({
top: 0,
behavior: 'smooth'
})
// now you got a read/write object
const current = new URLSearchParams(searchParamsEntries)
current.set("postId", post.postId.trim())
current.set("prompt", post.prompt.trim())
current.set("model", post.model.trim())
const search = current.toString()
router.push(`${pathname}${search ? `?${search}` : ""}`)
if (post.assetUrl) {
setAssetUrl(post.assetUrl)
}
if (post.prompt) {
setPromptDraft(post.prompt)
}
if (post.model) {
const nameToFind = post.model.toLowerCase().trim()
const existingModel = models.find(model => {
return (
model.repo.toLowerCase().trim().includes(nameToFind)
|| model.title.toLowerCase().trim().includes(nameToFind)
)
})
if (existingModel) {
setSelectedModel(existingModel)
}
}
}
const handleClickPlay = () => {
videoRef.current?.play()
}
return (
<div
ref={scrollRef}
className={cn(
`fixed inset-0 w-screen h-screen`,
`flex flex-col items-center justify-center`,
// `transition-all duration-300 ease-in-out`,
`overflow-y-scroll`,
)}>
<TooltipProvider>
{isLocked ? <Countdown
progressPercent={progressPercent}
remainingTimeInSec={remainingTimeInSec}
/> : null}
<div
className={cn(
`flex flex-col`,
`w-full md:max-w-4xl lg:max-w-5xl xl:max-w-6xl max-h-[85vh]`,
`space-y-8`,
// `transition-all duration-300 ease-in-out`,
)}>
<div
className={cn(
`flex flex-col`,
`flex-grow rounded-2xl md:rounded-3xl`,
`backdrop-blur-lg bg-white/40`,
`border-2 border-white/10`,
`items-center`,
`space-y-6 md:space-y-8 lg:space-y-12 xl:space-y-14`,
`px-3 py-6 md:px-6 md:py-12 xl:px-8 xl:py-14`,
)}>
{assetUrl ? <div
className={cn(
`flex flex-col`,
`space-y-3 md:space-y-6`,
`items-center`,
)}>
{assetUrl.startsWith("data:video/mp4") || assetUrl.endsWith(".mp4")
? <video
ref={videoRef}
muted
autoPlay
loop
src={assetUrl}
onClick={handleClickPlay}
className="rounded-md overflow-hidden md:h-[400px] lg:h-[500px] xl:h-[560px]"
/> :
<img
src={assetUrl}
className={cn(
`w-[512px] object-cover`,
`rounded-2xl`
)}
/>}
</div> : null}
<div className={cn(
`flex flex-col md:flex-row`,
`space-y-3 md:space-y-0 md:space-x-3`,
` w-full md:max-w-[1024px]`,
`items-center justify-between`
)}>
<div className={cn(
`flex flex-row flex-grow w-full`
)}>
<input
type="text"
placeholder={`Imagine a funny gif`}
className={cn(
headingFont.className,
`w-full`,
`input input-bordered rounded-full`,
`transition-all duration-300 ease-in-out`,
`disabled:bg-sky-100 disabled:text-sky-500 disabled:border-transparent`,
isLocked
? `bg-sky-100 text-sky-500 border-transparent`
: `bg-sky-200 text-sky-600 selection:bg-sky-200`,
`text-left`,
`text-xl leading-10 px-6 h-16 pt-1`,
`selection:bg-sky-800 selection:text-sky-200`
)}
value={promptDraft}
onChange={e => setPromptDraft(e.target.value)}
onKeyDown={({ key }) => {
if (key === 'Enter') {
if (!isLocked) {
handleSubmit()
}
}
}}
disabled={isLocked}
/>
<div className={cn(
`flex flew-row ml-[-64px] items-center`,
`transition-all duration-300 ease-in-out`,
`text-base`,
`bg-sky-200`,
`rounded-full`,
`text-right`,
`p-1`,
headingFont.className,
colorClass,
shouldWarn && !isLocked ? "opacity-100" : "opacity-0"
)}>
<span>{nbCharsUsed}</span>
<span>/</span>
<span>{nbCharsLimits}</span>
</div>
</div>
<div className="flex flex-row w-52">
<animated.button
style={{
textShadow: "0px 0px 1px #000000ab",
...submitButtonBouncer
}}
onMouseEnter={() => setOverSubmitButton(true)}
onMouseLeave={() => setOverSubmitButton(false)}
className={cn(
`px-6 py-3`,
`rounded-full`,
`transition-all duration-300 ease-in-out`,
isLocked
? `bg-orange-500/20 border-orange-800/10`
: `bg-sky-500/80 hover:bg-sky-400/100 border-sky-800/20`,
`text-center`,
`w-full`,
`text-2xl text-sky-50`,
`border`,
headingFont.className,
// `transition-all duration-300`,
// `hover:animate-bounce`
)}
disabled={isLocked}
onClick={handleSubmit}
>
{isLocked
? (stage === "generate" ? `Generating..` : `Smoothing..`)
: "Generate"
}
</animated.button>
</div>
</div>
{/*
<div>
<p>Generation will take about 32 seconds</p>
</div>
*/}
</div>
<div
className={cn(
`flex flex-col`,
`flex-grow rounded-2xl md:rounded-3xl`,
`backdrop-blur-lg bg-white/40`,
`border-2 border-white/10`,
`items-center`,
`space-y-2 md:space-y-3 lg:space-y-4 xl:space-y-6`,
`px-3 py-6 md:px-6 md:py-12 xl:px-8 xl:py-16`,
)}>
<div className="flex flex-col text-center">
<h3 className={cn(
headingFont.className,
"text-3xl text-sky-600 mb-4"
)}>{models.length ? `You selected:` : ""}</h3>
<h3 className={cn(
headingFont.className,
"text-5xl text-sky-700 mb-4"
)}>{models.length ? `${(selectedModel?.title || "").replaceAll("-", " ")}` : "Loading styles.."}</h3>
</div>
<div className="grid grid-cols-4 sm:grid-cols-6 md:grid-cols-8 lg:grid-cols-10 xl:grid-cols-12 gap-2">
{models.map(model =>
<div key={model.repo}>
<Tooltip>
<TooltipTrigger asChild>
<div
className={isLocked ? 'cursor-not-allowed' : `cursor-pointer`}
onClick={() => {
if (!isLocked) { setSelectedModel(model) }
}}>
<img
src={
model.image.startsWith("http")
? model.image
: model.image.replace("images/", "/images/models/")
}
className={cn(
`transition-all duration-150 ease-in-out`,
`w-20 h-20 object-cover rounded-lg overflow-hidden`,
`border-4 border-transparent`,
isLocked ? '' : `hover:border-yellow-50 hover:scale-110`,
selectedModel?.repo === model.repo
? `scale-110 border-4 border-yellow-300 hover:border-yellow-300`
: ``
)}
></img>
</div>
</TooltipTrigger>
{!isLocked && <TooltipContent>
<p className="w-full max-w-xl">{model.title}</p>
</TooltipContent>}
</Tooltip>
</div>
)}
</div>
</div>
<div
className={cn(
`flex flex-col`,
`flex-grow rounded-2xl md:rounded-3xl`,
`backdrop-blur-lg bg-white/40`,
`border-2 border-white/10`,
`items-center`,
`space-y-2 md:space-y-3 lg:space-y-4 xl:space-y-6`,
`px-3 py-6 md:px-6 md:py-12 xl:px-8 xl:py-16`,
)}>
<div className="flex flex-row">
<h3 className={cn(
headingFont.className,
"text-4xl text-sky-600 mb-4"
)}>{communityRoll.length ? "Random community clips:" : "Loading community roll.."}</h3>
</div>
<div className="grid grid-cols-1 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 xl:grid-cols-8 gap-2">
{communityRoll.map(post =>
<div key={post.postId}>
<Tooltip>
<TooltipTrigger asChild>
<div
className={isLocked ? 'cursor-not-allowed' : `cursor-pointer`}
onClick={() => { handleSelectCommunityPost(post) }}>
<video
muted
autoPlay
loop
src={post.assetUrl}
className={cn(
`rounded-md overflow-hidden`,
`transition-all duration-150 ease-in-out`,
`w-40 h-30 object-cover rounded-lg overflow-hidden`,
`border-4 border-transparent`,
isLocked ? '' : `hover:border-yellow-50 hover:scale-110`,
)}
/>
</div>
</TooltipTrigger>
{!isLocked && <TooltipContent>
<p className="w-full max-w-xl">{post.prompt}</p>
</TooltipContent>}
</Tooltip>
</div>
)}
</div>
</div>
</div>
</TooltipProvider>
</div>
)
}
|