breezywaves commited on
Commit
0637f4a
·
verified ·
1 Parent(s): d6aa41b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -30
app.py CHANGED
@@ -1,48 +1,52 @@
1
  import gradio as gr
 
2
  import os
3
- import replicate
4
  import time
5
- from dotenv import load_dotenv
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
9
 
10
- # Get the Replicate API token
11
- replicate_api_token = os.getenv("REPLICATE_API_TOKEN")
12
- if not replicate_api_token:
13
- print("Warning: REPLICATE_API_TOKEN not found in environment variables")
14
- print("You need to set this in your .env file or environment variables")
 
 
 
15
 
16
  # Define the function that handles video generation
17
- def generate_video(input_text, progress=gr.Progress()):
18
- if not input_text:
19
- return None, "Please enter a text prompt"
 
 
 
 
 
 
 
 
20
 
21
  try:
22
- progress(0, desc="Starting model...")
 
23
 
24
- # Run the model on Replicate directly
25
- output = replicate.run(
26
- "lightricks/ltx-video:6a9ba9d3141bbc5163bd02e4666c2fc0ccc788bf1c693ac0a5e3e9fafcf1c0a1",
27
- input={"prompt": input_text}
28
- )
29
 
30
- # The output will be a URL to the generated video
31
- if output:
32
- video_url = output[0] # Assuming the first output is the video URL
33
- progress(1, desc="Download complete")
34
- return video_url, "Video generation complete!"
35
- else:
36
- return None, "No output received from the model"
37
-
38
  except Exception as e:
39
- error_message = str(e)
40
- print(f"Error: {error_message}")
41
- return None, f"Error: {error_message}"
42
 
43
  # Set up Gradio interface
44
  with gr.Blocks() as demo:
45
- gr.Markdown("# Text to Video Generation with LTX-Video via Replicate")
46
 
47
  with gr.Row():
48
  with gr.Column():
@@ -65,8 +69,8 @@ with gr.Blocks() as demo:
65
  gr.Markdown("""
66
  ## Notes:
67
  - First generation may take several minutes as the model needs to warm up
68
- - This model is running on Replicate's infrastructure
69
- - You need a Replicate API token set as REPLICATE_API_TOKEN in your environment
70
  """)
71
 
72
  # Launch the app
 
1
  import gradio as gr
2
+ from dotenv import load_dotenv
3
  import os
4
+ from huggingface_hub import login
5
  import time
 
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
9
 
10
+ # Get the token from the environment variable
11
+ hf_token = os.getenv("HF_TOKEN")
12
+
13
+ # Authenticate with the Hugging Face token
14
+ if hf_token:
15
+ login(token=hf_token)
16
+ else:
17
+ print("Warning: HF_TOKEN not found in environment variables")
18
 
19
  # Define the function that handles video generation
20
+ def generate_video(input_text):
21
+ from huggingface_hub import InferenceClient
22
+
23
+ # Initialize the client with increased timeout
24
+ client = InferenceClient(
25
+ model="Lightricks/LTX-Video",
26
+ timeout=180 # Increase timeout to 3 minutes
27
+ )
28
+
29
+ # Add UI feedback
30
+ yield "Starting video generation... (this may take a few minutes)"
31
 
32
  try:
33
+ # Generate the video
34
+ video_bytes = client.text_to_video(input_text)
35
 
36
+ # Save to a temporary file
37
+ temp_path = f"generated_video_{int(time.time())}.mp4"
38
+ with open(temp_path, "wb") as f:
39
+ f.write(video_bytes)
 
40
 
41
+ return temp_path
42
+ except TimeoutError:
43
+ return "Model is still warming up. Please try again in a few minutes."
 
 
 
 
 
44
  except Exception as e:
45
+ return f"Error: {str(e)}"
 
 
46
 
47
  # Set up Gradio interface
48
  with gr.Blocks() as demo:
49
+ gr.Markdown("# Text to Video Generation with LTX-Video")
50
 
51
  with gr.Row():
52
  with gr.Column():
 
69
  gr.Markdown("""
70
  ## Notes:
71
  - First generation may take several minutes as the model needs to warm up
72
+ - If you get a timeout error, please try again in a few minutes
73
+ - This model is in staging mode and meant for testing purposes only
74
  """)
75
 
76
  # Launch the app