Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,52 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
import time
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
# Get the
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Define the function that handles video generation
|
| 17 |
-
def generate_video(input_text
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 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
|
| 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 |
-
-
|
| 69 |
-
-
|
| 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
|