import React, { useState } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Button } from './ui/button'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from './ui/form'; import { Input } from './ui/input'; import { cn } from '@/lib/utils'; import { Card } from './ui/card'; import { Loader2 } from 'lucide-react'; // Define form schema const formSchema = z.object({ prompt: z.string().min(1, { message: 'Prompt is required', }).max(1000, { message: 'Prompt must be less than 1000 characters', }), model: z.enum(["Wan-AI/Wan2.1-T2V-14B"]).default("Wan-AI/Wan2.1-T2V-14B"), }); type FormValues = z.infer; export default function VideoGenerator() { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [videoUrl, setVideoUrl] = useState(null); // Default form values const defaultValues: FormValues = { prompt: '', model: "Wan-AI/Wan2.1-T2V-14B", }; // Initialize form const form = useForm({ resolver: zodResolver(formSchema), defaultValues, }); // Handle form submission const onSubmit = async (data: FormValues) => { setIsLoading(true); setError(null); try { const response = await fetch('/api/generate-video', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Failed to generate video'); } const result = await response.json(); setVideoUrl(result.videoUrl); } catch (err: any) { setError(err.message || 'An error occurred while generating the video'); console.error('Error generating video:', err); } finally { setIsLoading(false); } }; return (
( Prompt Describe the video you want to generate. )} />
{error && (
{error}
)}
{videoUrl ? (
) : (

Your generated video will appear here

)}
); }