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()