Certainly! Below is the code that you can paste directly into a Google Colab notebook. This code sets up a Gradio interface for a text-to-video AI model using the pre-trained ModelScope model, without requiring any API key. Everything used is free of cost and publicly available. The code is properly indented to avoid any indentation errors.
# Install necessary packages
!pip install --upgrade pip
!pip install modelscope
!pip install gradio
# Import necessary libraries
from modelscope.pipelines import pipeline
from modelscope.outputs import OutputKeys
import gradio as gr
import os
# Initialize the ModelScope text-to-video model
pipeline_task = 'text-to-video-synthesis'
model_id = 'damo/text-to-video-synthesis'
# Initialize the pipeline once globally to avoid reloading it each time
text2video_pipeline = pipeline(pipeline_task, model=model_id)
def generate_video(prompt):
# Generate the video
output = text2video_pipeline(prompt)
# Get the path to the generated video
video_path = output[OutputKeys.OUTPUT_VIDEO]
# Return the video path
return video_path
# Create a Gradio interface
iface = gr.Interface(
fn=generate_video,
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your prompt here..."),
outputs=gr.outputs.Video(label="Generated Video"),
title="Text to Video Generation",
description="Generate video from text using the pre-trained ModelScope model.",
)
# Launch the interface
iface.launch(debug=True)
Instructions:
- Open a new Google Colab notebook.
- Paste the above code into a code cell.
- Run the cell. This will install the necessary packages and launch the Gradio interface.
- Once the interface appears, enter your text prompt in the input box and click "Submit".
- Wait for the model to generate the video. This might take some time depending on the complexity of the prompt and the resources available.
Notes:
- Model Download Time: The first time you run the pipeline, it will download the pre-trained model weights, which may take several minutes depending on your internet connection.
- Resource Usage: Generating videos can be resource-intensive. Ensure that your Colab instance has sufficient resources. You might want to consider using a GPU runtime:
- Go to
Runtime > Change runtime type.
- Set Hardware accelerator to
GPU.
- Avoiding Indentation Errors: Ensure that you copy the entire code block without introducing extra spaces or missing any lines.
Example Usage:
After running the code, you can try prompts like:
"A peaceful beach at sunset with gentle waves."
"A futuristic city with flying cars and bright neon lights."
Troubleshooting:
- If you encounter any errors during installation, try running the installation commands separately and restarting the runtime.
- If the interface doesn't load, ensure that all packages are installed correctly and that you're running the cell in a Colab environment.
Let me know if you have any questions or need further assistance!