File size: 2,555 Bytes
fc7e96f
0637f4a
251ca6b
0637f4a
d6aa41b
fc7e96f
251ca6b
 
 
0637f4a
 
 
 
 
 
 
 
fcee22c
1a7a7fd
0637f4a
 
 
 
 
 
169d25e
0637f4a
 
ca8cc77
 
a74dee5
d6aa41b
0637f4a
 
d6aa41b
0637f4a
 
 
 
d6aa41b
ca8cc77
 
0637f4a
ca8cc77
 
d6aa41b
ca8cc77
 
251ca6b
10c7e34
a74dee5
0637f4a
a74dee5
 
 
 
 
 
 
 
d6aa41b
a74dee5
 
 
 
 
 
 
d6aa41b
a74dee5
 
d6aa41b
 
 
0637f4a
 
d6aa41b
251ca6b
 
d6aa41b
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
import gradio as gr
from dotenv import load_dotenv
import os
from huggingface_hub import login
import time

# Load environment variables from .env file
load_dotenv()

# Get the token from the environment variable
hf_token = os.getenv("HF_TOKEN")

# Authenticate with the Hugging Face token
if hf_token:
    login(token=hf_token)
else:
    print("Warning: HF_TOKEN not found in environment variables")

# Define the function that handles video generation
def generate_video(input_text):
    from huggingface_hub import InferenceClient
    
    # Initialize the client with increased timeout
    client = InferenceClient(
        model="Lightricks/LTX-Video",
        timeout=1800  # Increase timeout to 3 minutes
    )
    
    # Add UI feedback - returning None for video and status message for text
    yield None, "Starting video generation... (this may take a few minutes)"
    
    try:
        # Generate the video
        video_bytes = client.text_to_video(input_text)
        
        # Save to a temporary file
        temp_path = f"generated_video_{int(time.time())}.mp4"
        with open(temp_path, "wb") as f:
            f.write(video_bytes)
        
        # Return both the video path and a success message
        return temp_path, "Video generation complete!"
    except TimeoutError:
        # Return None for video and error message for text
        return None, "Model is still warming up. Please try again in a few minutes."
    except Exception as e:
        # Return None for video and error message for text
        return None, f"Error: {str(e)}"

# Set up Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Text to Video Generation with LTX-Video")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Enter text prompt", 
                placeholder="A beautiful sunset over a mountain lake"
            )
            generate_btn = gr.Button("Generate Video")
            status_text = gr.Textbox(label="Status", interactive=False)
        
        with gr.Column():
            output_video = gr.Video(label="Generated Video")
    
    generate_btn.click(
        fn=generate_video,
        inputs=input_text,
        outputs=[output_video, status_text]
    )
    
    gr.Markdown("""
    ## Notes:
    - First generation may take several minutes as the model needs to warm up
    - If you get a timeout error, please try again in a few minutes
    - This model is in staging mode and meant for testing purposes only
    """)

# Launch the app
demo.launch()