File size: 3,907 Bytes
401e2db
0b3c1c2
563d084
 
401e2db
 
 
 
 
 
 
 
563d084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b3c1c2
 
401e2db
 
563d084
3891e74
563d084
 
 
 
 
 
 
 
 
401e2db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563d084
401e2db
 
3891e74
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
# Import the necessary libraries for creating the interface
import gradio as gr
from transformers import pipeline

# Verify model loading
def verify_model():
    try:
        gif_generator = pipeline('text-to-video', model='hotshotco/Hotshot-XL')
        return "Model loaded successfully!"
    except Exception as e:
        return str(e)

# Define a function to apply regex choices to the prompt text
def apply_regex_choices(text):
    import re  # Import the regular expressions module
    pattern = re.compile(r'\(([^)]+)\)')  # Define the regex pattern to find text within parentheses
    while True:
        match = pattern.search(text)  # Search for the pattern in the text
        if not match:  # If no match is found, break the loop
            break
        choices = match.group(1).split('|')  # Split the choices within the parentheses by '|'
        chosen = choices[0]  # Select the first option from the choices (or use random.choice(choices) for randomness)
        text = text[:match.start()] + chosen + text[match.end():]  # Replace the matched text with the chosen option
    return text  # Return the modified text

# Define a function to generate the GIF using the provided prompt
def generate_gif(prompt):
    final_prompt = apply_regex_choices(prompt)  # Apply regex choices to the prompt
    gif_generator = pipeline('text-to-video', model='hotshotco/Hotshot-XL')  # Load the text-to-video pipeline with the specified model
    gif = gif_generator(final_prompt)  # Generate the GIF using the final prompt
    with open("output.gif", "wb") as f:  # Open a file to save the GIF
        f.write(gif)  # Write the GIF to the file
    return "output.gif"  # Return the file path of the generated GIF

# Define the default prompt template with regex options
prompt_template = """
Create a GIF of a 'Technical Difficulties' television program interruption. The scene should show colored glitchy and typical 'technical difficulties' cable interruptions, (using|with) demonic symbols glitching out along with the colors of the usual 'tech difficulties' broadcasts. The setting should be (an older|a vintage) tv cable broadcast signal interruption. The style should be a realistic cable interruption interlaced with (demonic imagery|dark symbols) glitching out along with the typical color page of interruption in connection. The GIF should last for 1 second.
"""

# Create the main Gradio interface
main_iface = gr.Interface(
    fn=generate_gif,  # The function to call when the user interacts with the interface
    inputs=gr.Textbox(lines=10, placeholder="Enter your custom prompt here or use the default template", value=prompt_template),  # Input textbox for the prompt
    outputs="file",  # Output will be a file (the generated GIF)
    title="Structured Prompt GIF Generator",  # Title of the interface
    description="Generate GIFs from text prompts using the Hotshot-XL model. Customize the prompt or use the default template to generate GIFs.",  # Description of the interface
    examples=[  # Adding example prompts
        ["Create a GIF of a cat jumping over a fence in a sunny backyard. The style should be realistic. The GIF should last for 2 seconds."],
        ["Create a GIF of a rocket launching into space. The scene should show the rocket taking off from the launchpad with smoke and fire. The style should be cinematic. The GIF should last for 3 seconds."]
    ]
)

# Create the model verification interface
test_iface = gr.Interface(
    fn=verify_model,
    inputs=None,
    outputs="text",
    title="Test Model Loading",
    description="Click the button to test if the model loads correctly."
)

# Combine both interfaces into a single Gradio Block
block = gr.Blocks()
with block:
    gr.Markdown("# Structured Prompt GIF Generator")
    gr.Tab("Generate GIFs", main_iface)
    gr.Tab("Verify Model", test_iface)

# Launch the Gradio interface
block.launch()