Spaces:
Running
on
Zero
Running
on
Zero
import os | |
import subprocess | |
os.chdir('diffusers_repo') | |
subprocess.check_call(['pip', 'install', '.']) | |
import gradio as gr | |
import spaces | |
import torch | |
from diffusers import CosmosTextToImagePipeline | |
import random | |
# Available checkpoints | |
model_14b_id = "diffusers-internal-dev/ct2i-14B" | |
model_2b_id = "diffusers-internal-dev/ct2i-2B" | |
# Load both pipelines once to avoid repeated loading | |
print("Loading 14B model...") | |
pipe_14b = CosmosTextToImagePipeline.from_pretrained(model_14b_id, torch_dtype=torch.bfloat16) | |
pipe_14b.to("cuda") | |
print("Loading 2B model...") | |
pipe_2b = CosmosTextToImagePipeline.from_pretrained(model_2b_id, torch_dtype=torch.bfloat16) | |
pipe_2b.to("cuda") | |
print("Both models loaded successfully!") | |
def generate_image(prompt, negative_prompt, seed, randomize_seed, model_choice, progress=gr.Progress(track_tqdm=True)): | |
# Select the appropriate pipeline based on model choice | |
if model_choice == "14B": | |
pipe = pipe_14b | |
else: # "2B Model" | |
pipe = pipe_2b | |
if randomize_seed: | |
actual_seed = random.randint(0, 1000000) | |
else: | |
actual_seed = seed | |
generator = torch.Generator().manual_seed(actual_seed) | |
output = pipe( | |
prompt=prompt, negative_prompt=negative_prompt, generator=generator | |
).images[0] | |
return output, actual_seed | |
# Define the Gradio Blocks interface | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# Cosmos Predict-2 Text-to-Image Generator | |
Enter a detailed prompt to generate an image using the Cosmos model. | |
You can also provide a negative prompt to guide the generation away from certain elements. | |
Choose between the 14B model (higher quality, slower) or 2B model (faster, smaller). | |
""" | |
) | |
with gr.Row(): | |
with gr.Column(): | |
prompt_input = gr.Textbox( | |
label="Prompt", | |
lines=5, | |
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.", | |
placeholder="Enter your descriptive prompt here..." | |
) | |
negative_prompt_input = gr.Textbox( | |
label="Negative Prompt", | |
lines=3, | |
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.", | |
placeholder="Enter what you DON'T want to see in the image..." | |
) | |
with gr.Row(): | |
randomize_seed_checkbox = gr.Checkbox( | |
label="Randomize Seed", | |
value=True | |
) | |
seed_input = gr.Slider( | |
minimum=0, | |
maximum=1000000, | |
value=1, | |
step=1, | |
label="Seed" | |
) | |
model_radio = gr.Radio( | |
choices=["14B", "2B"], | |
value="14B", | |
label="Model Selection", | |
) | |
generate_button = gr.Button("Generate Image") | |
with gr.Column(): | |
output_image = gr.Image(label="Generated Image", type="pil") | |
generate_button.click( | |
fn=generate_image, | |
inputs=[prompt_input, negative_prompt_input, seed_input, randomize_seed_checkbox, model_radio], | |
outputs=[output_image, seed_input] | |
) | |
if __name__ == "__main__": | |
demo.launch() |