Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from diffusers import CosmosTextToImagePipeline
|
4 |
+
import random
|
5 |
+
|
6 |
+
# Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
|
7 |
+
model_id = "diffusers-internal-dev/ct2i-14B"
|
8 |
+
|
9 |
+
# Load the pipeline once to avoid repeated loading
|
10 |
+
pipe = CosmosTextToImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
11 |
+
pipe.to("cuda")
|
12 |
+
|
13 |
+
def generate_image(prompt, negative_prompt, seed, randomize_seed):
|
14 |
+
"""
|
15 |
+
Generates an image based on the provided prompt and negative prompt.
|
16 |
+
Randomizes the seed if randomize_seed is True, otherwise uses the provided seed.
|
17 |
+
Returns the generated image and the actual seed used, which will update the slider.
|
18 |
+
"""
|
19 |
+
if randomize_seed:
|
20 |
+
actual_seed = random.randint(0, 1000000)
|
21 |
+
else:
|
22 |
+
actual_seed = seed
|
23 |
+
|
24 |
+
generator = torch.Generator().manual_seed(actual_seed)
|
25 |
+
output = pipe(
|
26 |
+
prompt=prompt, negative_prompt=negative_prompt, generator=generator
|
27 |
+
).images[0]
|
28 |
+
return output, actual_seed
|
29 |
+
|
30 |
+
# Define the Gradio Blocks interface
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown(
|
33 |
+
"""
|
34 |
+
# Cosmos Text-to-Image Generator
|
35 |
+
Enter a detailed prompt to generate an image using the Cosmos model.
|
36 |
+
You can also provide a negative prompt to guide the generation away from certain elements.
|
37 |
+
"""
|
38 |
+
)
|
39 |
+
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column():
|
42 |
+
prompt_input = gr.Textbox(
|
43 |
+
label="Prompt",
|
44 |
+
lines=5,
|
45 |
+
value="A close-up shot captures a vibrant yellow scrubber vigorously working on a grimy plate, its bristles moving in circular motions to lift stubborn grease and food residue. The dish, once covered in remnants of a hearty meal, gradually reveals its original glossy surface. Suds form and bubble around the scrubber, creating a satisfying visual of cleanliness in progress. The sound of scrubbing fills the air, accompanied by the gentle clinking of the dish against the sink. As the scrubber continues its task, the dish transforms, gleaming under the bright kitchen lights, symbolizing the triumph of cleanliness over mess.",
|
46 |
+
placeholder="Enter your descriptive prompt here..."
|
47 |
+
)
|
48 |
+
negative_prompt_input = gr.Textbox(
|
49 |
+
label="Negative Prompt",
|
50 |
+
lines=3,
|
51 |
+
value="The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality.",
|
52 |
+
placeholder="Enter what you DON'T want to see in the image..."
|
53 |
+
)
|
54 |
+
with gr.Row():
|
55 |
+
randomize_seed_checkbox = gr.Checkbox(
|
56 |
+
label="Randomize Seed",
|
57 |
+
value=TRue
|
58 |
+
)
|
59 |
+
seed_input = gr.Slider(
|
60 |
+
minimum=0,
|
61 |
+
maximum=1000000,
|
62 |
+
value=1,
|
63 |
+
step=1,
|
64 |
+
label="Seed"
|
65 |
+
)
|
66 |
+
|
67 |
+
generate_button = gr.Button("Generate Image")
|
68 |
+
|
69 |
+
with gr.Column():
|
70 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
71 |
+
|
72 |
+
generate_button.click(
|
73 |
+
fn=generate_image,
|
74 |
+
inputs=[prompt_input, negative_prompt_input, seed_input, randomize_seed_checkbox],
|
75 |
+
outputs=[output_image, seed_input] # Return both image and the actual seed back to the seed_input slider
|
76 |
+
)
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
demo.launch()
|