Spaces:
Runtime error
Runtime error
Commit
•
563d084
1
Parent(s):
5d030e6
Update app.py
Browse filescreate a regex structured generation prompt
app.py
CHANGED
@@ -1,146 +1,48 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
from diffusers import DiffusionPipeline
|
5 |
-
import torch
|
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 |
-
generator = generator
|
36 |
-
).images[0]
|
37 |
-
|
38 |
-
return image
|
39 |
-
|
40 |
-
examples = [
|
41 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
42 |
-
"An astronaut riding a green horse",
|
43 |
-
"A delicious ceviche cheesecake slice",
|
44 |
-
]
|
45 |
-
|
46 |
-
css="""
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
"""
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
-
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
-
|
140 |
-
run_button.click(
|
141 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
-
)
|
145 |
-
|
146 |
-
demo.queue().launch()
|
|
|
1 |
+
# Import the necessary library for creating the interface
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Import the pipeline function from the transformers library to load the model
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Define a function to apply regex choices to the prompt text
|
8 |
+
def apply_regex_choices(text):
|
9 |
+
import re # Import the regular expressions module
|
10 |
+
pattern = re.compile(r'\(([^)]+)\)') # Define the regex pattern to find text within parentheses
|
11 |
+
while True:
|
12 |
+
match = pattern.search(text) # Search for the pattern in the text
|
13 |
+
if not match: # If no match is found, break the loop
|
14 |
+
break
|
15 |
+
choices = match.group(1).split('|') # Split the choices within the parentheses by '|'
|
16 |
+
chosen = choices[0] # Select the first option from the choices (or use random.choice(choices) for randomness)
|
17 |
+
text = text[:match.start()] + chosen + text[match.end():] # Replace the matched text with the chosen option
|
18 |
+
return text # Return the modified text
|
19 |
+
|
20 |
+
# Define a function to generate the GIF using the provided prompt
|
21 |
+
def generate_gif(prompt):
|
22 |
+
final_prompt = apply_regex_choices(prompt) # Apply regex choices to the prompt
|
23 |
+
gif_generator = pipeline('text-to-video', model='hotshotco/Hotshot-XL') # Load the text-to-video pipeline with the specified model
|
24 |
+
gif = gif_generator(final_prompt) # Generate the GIF using the final prompt
|
25 |
+
with open("output.gif", "wb") as f: # Open a file to save the GIF
|
26 |
+
f.write(gif) # Write the GIF to the file
|
27 |
+
return "output.gif" # Return the file path of the generated GIF
|
28 |
+
|
29 |
+
# Define the default prompt template with regex options
|
30 |
+
prompt_template = """
|
31 |
+
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
|
34 |
+
# Create a Gradio interface
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=generate_gif, # The function to call when the user interacts with the interface
|
37 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter your custom prompt here or use the default template", default=prompt_template), # Input textbox for the prompt
|
38 |
+
outputs="file", # Output will be a file (the generated GIF)
|
39 |
+
title="Structured Prompt GIF Generator", # Title of the interface
|
40 |
+
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
|
41 |
+
examples=[ # Adding example prompts
|
42 |
+
["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."],
|
43 |
+
["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."]
|
44 |
+
]
|
45 |
+
)
|
46 |
+
|
47 |
+
# Launch the Gradio interface
|
48 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|