Skip to content

Commit ce1891d

Browse files
committed
script-generation-implemented
1 parent 31e439a commit ce1891d

File tree

8 files changed

+1399
-9
lines changed

8 files changed

+1399
-9
lines changed

app/api/get-video-script/route.jsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { generateCompletion } from "@/configs/aimodel";
2+
import { NextResponse } from "next/server";
3+
4+
5+
export async function POST(req){
6+
7+
try {
8+
const { prompt } = await req.json();
9+
const completion = await generateCompletion(prompt);
10+
return new NextResponse(completion.choices[0].message.content, { status: 200 });
11+
} catch (e) {
12+
13+
return new NextResponse({"Internal Server Error":e}, { status: 500 });
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react'
2+
import {
3+
AlertDialog,
4+
AlertDialogContent,
5+
AlertDialogTitle,
6+
} from "@/components/ui/alert-dialog"
7+
import Image from 'next/image'
8+
9+
function CustomLoading({loading}) {
10+
return (
11+
<AlertDialog open={loading}>
12+
<AlertDialogTitle>
13+
<AlertDialogContent className='bg-white'>
14+
<div className='bg-white flex flex-col items-center my-10 justify-center'>
15+
<Image src='/soon.gif' width={100} height={100} alt='loading' />
16+
<h2>
17+
Generating your Video...Keep Calm
18+
</h2>
19+
</div>
20+
</AlertDialogContent>
21+
</AlertDialogTitle>
22+
</AlertDialog>
23+
24+
)
25+
}
26+
27+
export default CustomLoading

app/dashboard/create-new/page.jsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import SelectStyle from './_components/SelectStyle'
55
import {Outfit} from 'next/font/google'
66
import SelectDuration from './_components/SelectDuration'
77
import { Button } from '@/components/ui/button'
8+
import axios from 'axios'
9+
import CustomLoading from './_components/Loading'
810

911
const outfit = Outfit({subsets: ["latin-ext"],weight: "600"});
1012

1113
function CreateNew() {
1214

1315
const [formData,setFormData]= useState([])
16+
const [loading,setLoading]=useState(false)
17+
const [videoScript,setVideoScript]=useState()
18+
1419
const onHandleChange=(fieldName,fieldValue)=>{
1520
console.log(fieldName,fieldValue)
1621
setFormData(
@@ -20,6 +25,26 @@ function CreateNew() {
2025
})
2126
)
2227
}
28+
29+
const onCreateClickHandler=()=>{
30+
GetVideoScript();
31+
}
32+
33+
//Script to generate video
34+
const GetVideoScript = async ()=>{
35+
setLoading(true)
36+
const fixedPrompt =
37+
"Write a Script that generates "+formData.duration+" video on the topic "+formData.topic+" along with AI Image prompt in "+formData.imageStyle+" Style for each scene and give me the result in JSON format with imagePrompt and contentText as field and without the word json or anything else, just the response. No Plain text";
38+
const result = await axios.post('/api/get-video-script',
39+
40+
{prompt: fixedPrompt}
41+
).then((response) => {
42+
console.log(response.data)
43+
setVideoScript(response.data)
44+
})
45+
setLoading(false)
46+
}
47+
2348
return (
2449
<div className='md:px-20'>
2550
<h2 className={`text-4xl text-primary text-center ${outfit.className}`}>Create A New Video</h2>
@@ -35,11 +60,13 @@ function CreateNew() {
3560
<SelectDuration onUserSelect={onHandleChange}/>
3661

3762
{/* Create Button */}
38-
<Button className='mt-10 w-full h-10'>
63+
<Button className='mt-10 w-full h-10' onClick={onCreateClickHandler}>
3964
Generate The Video
4065
</Button>
4166

4267
</div>
68+
69+
<CustomLoading loading={loading}/>
4370
</div>
4471
)
4572
}

components/ui/alert-dialog.jsx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use client"
2+
3+
import * as React from "react"
4+
import * as AlertDialogPrimitive from "@radix-ui/react-alert-dialog"
5+
6+
import { cn } from "@/lib/utils"
7+
import { buttonVariants } from "@/components/ui/button"
8+
9+
const AlertDialog = AlertDialogPrimitive.Root
10+
11+
const AlertDialogTrigger = AlertDialogPrimitive.Trigger
12+
13+
const AlertDialogPortal = AlertDialogPrimitive.Portal
14+
15+
const AlertDialogOverlay = React.forwardRef(({ className, ...props }, ref) => (
16+
<AlertDialogPrimitive.Overlay
17+
className={cn(
18+
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
19+
className
20+
)}
21+
{...props}
22+
ref={ref} />
23+
))
24+
AlertDialogOverlay.displayName = AlertDialogPrimitive.Overlay.displayName
25+
26+
const AlertDialogContent = React.forwardRef(({ className, ...props }, ref) => (
27+
<AlertDialogPortal>
28+
<AlertDialogOverlay />
29+
<AlertDialogPrimitive.Content
30+
ref={ref}
31+
className={cn(
32+
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
33+
className
34+
)}
35+
{...props} />
36+
</AlertDialogPortal>
37+
))
38+
AlertDialogContent.displayName = AlertDialogPrimitive.Content.displayName
39+
40+
const AlertDialogHeader = ({
41+
className,
42+
...props
43+
}) => (
44+
<div
45+
className={cn("flex flex-col space-y-2 text-center sm:text-left", className)}
46+
{...props} />
47+
)
48+
AlertDialogHeader.displayName = "AlertDialogHeader"
49+
50+
const AlertDialogFooter = ({
51+
className,
52+
...props
53+
}) => (
54+
<div
55+
className={cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2", className)}
56+
{...props} />
57+
)
58+
AlertDialogFooter.displayName = "AlertDialogFooter"
59+
60+
const AlertDialogTitle = React.forwardRef(({ className, ...props }, ref) => (
61+
<AlertDialogPrimitive.Title ref={ref} className={cn("text-lg font-semibold", className)} {...props} />
62+
))
63+
AlertDialogTitle.displayName = AlertDialogPrimitive.Title.displayName
64+
65+
const AlertDialogDescription = React.forwardRef(({ className, ...props }, ref) => (
66+
<AlertDialogPrimitive.Description
67+
ref={ref}
68+
className={cn("text-sm text-muted-foreground", className)}
69+
{...props} />
70+
))
71+
AlertDialogDescription.displayName =
72+
AlertDialogPrimitive.Description.displayName
73+
74+
const AlertDialogAction = React.forwardRef(({ className, ...props }, ref) => (
75+
<AlertDialogPrimitive.Action ref={ref} className={cn(buttonVariants(), className)} {...props} />
76+
))
77+
AlertDialogAction.displayName = AlertDialogPrimitive.Action.displayName
78+
79+
const AlertDialogCancel = React.forwardRef(({ className, ...props }, ref) => (
80+
<AlertDialogPrimitive.Cancel
81+
ref={ref}
82+
className={cn(buttonVariants({ variant: "outline" }), "mt-2 sm:mt-0", className)}
83+
{...props} />
84+
))
85+
AlertDialogCancel.displayName = AlertDialogPrimitive.Cancel.displayName
86+
87+
export {
88+
AlertDialog,
89+
AlertDialogPortal,
90+
AlertDialogOverlay,
91+
AlertDialogTrigger,
92+
AlertDialogContent,
93+
AlertDialogHeader,
94+
AlertDialogFooter,
95+
AlertDialogTitle,
96+
AlertDialogDescription,
97+
AlertDialogAction,
98+
AlertDialogCancel,
99+
}

configs/aimodel.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import OpenAI from "openai";
2+
3+
4+
const openai = new OpenAI({
5+
apiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY,
6+
});
7+
8+
export async function generateCompletion(prompt) {
9+
try {
10+
const completion = await openai.chat.completions.create({
11+
model: "gpt-4o",
12+
temperature: 0.5,
13+
top_p: 0.95,
14+
max_tokens: 8192,
15+
messages: [
16+
{
17+
role: "system",
18+
content: "You are a Video Script Writer and AI Image Prompt Engineer. You do all the tasks with sincerity.",
19+
},
20+
{
21+
role: "user",
22+
content: prompt,
23+
},
24+
],
25+
});
26+
27+
return completion;
28+
} catch (error) {
29+
console.error("Error in AI completion: ", error);
30+
throw error;
31+
}
32+
}

0 commit comments

Comments
 (0)