File size: 7,651 Bytes
01bdc12
be72e32
01bdc12
 
f7247c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43a37a2
f7247c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be72e32
f7247c2
 
 
 
 
 
 
 
 
 
be72e32
 
f7247c2
 
 
be72e32
f7247c2
be72e32
f7247c2
be72e32
f7247c2
 
be72e32
f7247c2
 
 
 
 
 
 
 
 
be72e32
3210eb9
 
 
 
 
f7247c2
 
3210eb9
01bdc12
3210eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be72e32
3210eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import sys
import subprocess

# Debug: Print environment information
def print_env_info():
    print("=== ENVIRONMENT DEBUG INFO ===")
    print(f"Python executable: {sys.executable}")
    print(f"Python version: {sys.version}")
    print(f"Current working directory: {os.getcwd()}")
    print(f"Python path: {sys.path[:3]}...")  # First 3 entries
    
    # Check if we can find pip
    try:
        pip_result = subprocess.run([sys.executable, '-m', 'pip', '--version'], 
                                  capture_output=True, text=True)
        print(f"Pip version: {pip_result.stdout.strip()}")
    except Exception as e:
        print(f"Pip check failed: {e}")
    
    # List contents of current directory
    print(f"Directory contents: {os.listdir('.')}")
    if os.path.exists('diffusers_repo'):
        print("βœ“ diffusers_repo found")
    else:
        print("βœ— diffusers_repo NOT found")
    print("===============================")

print_env_info()

# Install diffusers from local repository
def install_diffusers():
    try:
        # Change to diffusers repo directory
        original_dir = os.getcwd()
        
        if not os.path.exists('diffusers_repo'):
            print("ERROR: diffusers_repo directory not found!")
            return False
            
        os.chdir('diffusers_repo')
        print(f"Changed to directory: {os.getcwd()}")
        
        # Use the exact same Python executable for pip
        python_exe = sys.executable
        print(f"Using Python executable: {python_exe}")
        
        # Install in editable mode with verbose output
        cmd = [python_exe, '-m', 'pip', 'install', '.', '--verbose']
        print(f"Running command: {' '.join(cmd)}")
        
        result = subprocess.run(cmd, capture_output=True, text=True)
        print("Installation stdout:", result.stdout[-1000:])  # Last 1000 chars
        if result.stderr:
            print("Installation stderr:", result.stderr[-1000:])  # Last 1000 chars
        
        # Change back to original directory
        os.chdir(original_dir)
        
        # Add diffusers_repo to Python path
        diffusers_path = os.path.join(original_dir, 'diffusers_repo')
        if diffusers_path not in sys.path:
            sys.path.insert(0, diffusers_path)
            print(f"Added to Python path: {diffusers_path}")
        
        # Test if we can import after installation
        try:
            import diffusers
            print(f"βœ“ Successfully imported diffusers from: {diffusers.__file__}")
            return True
        except ImportError as e:
            print(f"βœ— Failed to import diffusers after installation: {e}")
            return False
            
    except Exception as e:
        print(f"Installation failed with exception: {e}")
        return False

# Attempt installation
print("Starting diffusers installation...")
if not install_diffusers():
    print("Failed to install local diffusers, exiting...")
    sys.exit(1)
else:
    print("βœ“ Local diffusers installation successful!")

# Now import required modules
print("Importing required modules...")
try:
    import gradio as gr
    print("βœ“ Gradio imported")
    import spaces  
    print("βœ“ Spaces imported")
    import torch
    print("βœ“ Torch imported")
    from diffusers import CosmosTextToImagePipeline
    print("βœ“ CosmosTextToImagePipeline imported")
    import random
    print("βœ“ Random imported")
    print("All imports successful!")
except ImportError as e:
    print(f"βœ— Import failed: {e}")
    print("Checking installed packages...")
    try:
        result = subprocess.run([sys.executable, '-m', 'pip', 'list'], 
                              capture_output=True, text=True)
        print("Installed packages:")
        print(result.stdout)
    except:
        pass
    sys.exit(1)

# Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
model_id = "diffusers-internal-dev/ct2i-14B"

# Load the pipeline once to avoid repeated loading
pipe = CosmosTextToImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
pipe.to("cuda")

@spaces.GPU
def generate_image(prompt, negative_prompt, seed, randomize_seed):
    """
    Generates an image based on the provided prompt and negative prompt.
    Randomizes the seed if randomize_seed is True, otherwise uses the provided seed.
    Returns the generated image and the actual seed used, which will update the slider.
    """
    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 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.
        """
    )

    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  # Fixed: was "TRue"
                )
                seed_input = gr.Slider(
                    minimum=0,
                    maximum=1000000,
                    value=1,
                    step=1,
                    label="Seed"
                )
                
            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],
        outputs=[output_image, seed_input] # Return both image and the actual seed back to the seed_input slider
    )

if __name__ == "__main__":
    demo.launch()