File size: 10,590 Bytes
f6fe860
 
 
 
 
 
3dc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6fe860
3dc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6fe860
 
3dc7f18
 
 
 
6842006
 
 
 
3dc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6fe860
 
3dc7f18
 
 
 
 
 
 
 
f6fe860
 
 
3dc7f18
 
 
 
 
6842006
 
3dc7f18
 
 
 
 
 
 
 
f6fe860
3dc7f18
 
 
 
 
 
 
 
 
 
 
6842006
 
 
3dc7f18
 
 
 
 
 
 
 
 
 
 
6842006
 
3dc7f18
 
 
 
 
 
 
f6fe860
3dc7f18
6842006
 
 
 
 
f6fe860
 
3dc7f18
b5da3d3
 
3dc7f18
b5da3d3
f6fe860
3dc7f18
 
6842006
 
 
 
 
3dc7f18
 
 
 
172e437
 
 
 
6842006
172e437
 
 
6842006
 
 
 
172e437
 
 
 
6842006
 
 
 
172e437
 
3dc7f18
 
6842006
 
 
 
 
 
 
 
 
 
3dc7f18
 
 
 
 
 
 
 
f6fe860
 
d810db5
3dc7f18
 
 
 
d810db5
3dc7f18
 
 
 
 
 
 
 
 
d810db5
 
3dc7f18
 
 
 
 
 
 
d810db5
f6fe860
3dc7f18
 
 
 
 
 
 
 
 
 
 
 
 
d810db5
8197187
3dc7f18
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import gradio as gr
import torch
from transformers import AutoConfig, AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from PIL import Image
import numpy as np
import spaces
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Constants
DEFAULT_WIDTH = 384
DEFAULT_HEIGHT = 384
PARALLEL_SIZE = 5
PATCH_SIZE = 16

# Load model and processor with error handling
def load_model():
    try:
        model_path = "deepseek-ai/Janus-Pro-7B"
        config = AutoConfig.from_pretrained(model_path)
        language_config = config.language_config
        language_config._attn_implementation = 'eager'

        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        logger.info(f"Loading model on device: {device}")

        vl_gpt = AutoModelForCausalLM.from_pretrained(
            model_path,
            language_config=language_config,
            trust_remote_code=True,
            torch_dtype=torch.bfloat16 if device.type == "cuda" else torch.float32
        ).to(device)

        vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
        return vl_gpt, vl_chat_processor, device
    
    except Exception as e:
        logger.error(f"Model loading failed: {str(e)}")
        raise RuntimeError("Failed to load model. Please check the model path and dependencies.")

try:
    vl_gpt, vl_chat_processor, device = load_model()
    tokenizer = vl_chat_processor.tokenizer
except RuntimeError as e:
    raise e

# Helper functions with improved memory management
def generate(input_ids, width, height, cfg_weight=5, temperature=1.0, parallel_size=5, progress=None):
    try:
        torch.cuda.empty_cache()
        tokens = torch.zeros((parallel_size * 2, len(input_ids)), dtype=torch.int, device=device)
        
        for i in range(parallel_size * 2):
            tokens[i, :] = input_ids
            if i % 2 != 0:
                tokens[i, 1:-1] = vl_chat_processor.pad_id

        with torch.no_grad():
            inputs_embeds = vl_gpt.language_model.get_input_embeddings()(tokens)
            generated_tokens = torch.zeros((parallel_size, 576), dtype=torch.int, device=device)

            pkv = None
            total_steps = 576
            for i in range(total_steps):
                if progress is not None:
                    progress((i + 1) / total_steps, desc="Generating image tokens")
                
                outputs = vl_gpt.language_model.model(
                    inputs_embeds=inputs_embeds,
                    use_cache=True,
                    past_key_values=pkv
                )
                pkv = outputs.past_key_values
                hidden_states = outputs.last_hidden_state
                logits = vl_gpt.gen_head(hidden_states[:, -1, :])

                logit_cond = logits[0::2, :]
                logit_uncond = logits[1::2, :]
                logits = logit_uncond + cfg_weight * (logit_cond - logit_uncond)

                probs = torch.softmax(logits / temperature, dim=-1)
                next_token = torch.multinomial(probs, num_samples=1)
                generated_tokens[:, i] = next_token.squeeze(dim=-1)

                next_token = torch.cat([next_token.unsqueeze(dim=1)] * 2, dim=1).view(-1)
                img_embeds = vl_gpt.prepare_gen_img_embeds(next_token)
                inputs_embeds = img_embeds.unsqueeze(dim=1)

        return generated_tokens
    
    except RuntimeError as e:
        logger.error(f"Generation error: {str(e)}")
        raise RuntimeError("Generation failed due to memory constraints. Try reducing the parallel size.")
    finally:
        torch.cuda.empty_cache()

def unpack(patches, width, height, parallel_size=5):
    try:
        patches = patches.detach().to(device='cpu', dtype=torch.float32).numpy()
        patches = patches.transpose(0, 2, 3, 1)
        patches = np.clip((patches + 1) / 2 * 255, 0, 255)
        return [Image.fromarray(patch.astype(np.uint8)) for patch in patches]
    except Exception as e:
        logger.error(f"Unpacking error: {str(e)}")
        raise RuntimeError("Failed to process generated image data.")

@torch.inference_mode()
@spaces.GPU(duration=120)
def generate_image(prompt, seed=None, guidance=5, t2i_temperature=1.0, progress=gr.Progress()):
    try:
        if not prompt.strip():
            raise gr.Error("Please enter a valid prompt.")

        if progress is not None:
            progress(0, desc="Initializing...")
        torch.cuda.empty_cache()

        # Seed management
        if seed is None:
            seed = torch.seed()
        else:
            seed = int(seed)
            
        torch.manual_seed(seed)
        if device.type == "cuda":
            torch.cuda.manual_seed(seed)

        messages = [{'role': '<|User|>', 'content': prompt}, {'role': '<|Assistant|>', 'content': ''}]
        text = vl_chat_processor.apply_sft_template_for_multi_turn_prompts(
            conversations=messages,
            sft_format=vl_chat_processor.sft_format,
            system_prompt=''
        ) + vl_chat_processor.image_start_tag

        input_ids = torch.tensor(tokenizer.encode(text), dtype=torch.long, device=device)
        
        if progress is not None:
            progress(0.1, desc="Generating image tokens...")
        
        generated_tokens = generate(
            input_ids,
            DEFAULT_WIDTH,
            DEFAULT_HEIGHT,
            cfg_weight=guidance,
            temperature=t2i_temperature,
            parallel_size=PARALLEL_SIZE,
            progress=progress
        )
        
        if progress is not None:
            progress(0.9, desc="Processing images...")
        patches = vl_gpt.gen_vision_model.decode_code(
            generated_tokens.to(dtype=torch.int),
            shape=[PARALLEL_SIZE, 8, DEFAULT_WIDTH // PATCH_SIZE, DEFAULT_HEIGHT // PATCH_SIZE]
        )
        
        images = unpack(patches, DEFAULT_WIDTH, DEFAULT_HEIGHT, PARALLEL_SIZE)
        return images

    except Exception as e:
        logger.error(f"Generation failed: {str(e)}", exc_info=True)
        if "index out of range" in str(e).lower():
            raise gr.Error("Image generation failed due to internal error. Please try again with different parameters.")
        else:
            raise gr.Error(f"Image generation failed: {str(e)}")

def create_interface():
    with gr.Blocks(title="Janus-Pro-7B Image Generator", theme=gr.themes.Soft()) as demo:
        gr.Markdown("""
        # Text-to-Image Generation with Janus-Pro-7B
        **Generate high-quality images from text prompts using DeepSeek's advanced multimodal AI model.**
        """)

        with gr.Row():
            with gr.Column(scale=3):
                prompt_input = gr.Textbox(
                    label="Prompt", 
                    placeholder="Describe the image you want to generate...", 
                    lines=3
                )
                generate_btn = gr.Button("Generate Images", variant="primary")
                
                with gr.Accordion("Advanced Settings", open=False):
                    with gr.Group():
                        seed_input = gr.Number(
                            label="Seed", 
                            value=None, 
                            precision=0, 
                            info="Leave empty for random seed"
                        )
                        guidance_slider = gr.Slider(
                            label="CFG Guidance Weight",
                            minimum=3,
                            maximum=10,
                            value=5,
                            step=0.5,
                            info="Higher values = more prompt adherence, lower values = more creativity"
                        )
                        temp_slider = gr.Slider(
                            label="Temperature",
                            minimum=0.1,
                            maximum=1.0,
                            value=1.0,
                            step=0.1,
                            info="Higher values = more randomness, lower values = more deterministic"
                        )

            with gr.Column(scale=2):
                output_gallery = gr.Gallery(
                    label="Generated Images", 
                    columns=2, 
                    height=600, 
                    preview=True
                )
                status = gr.Textbox(
                    label="Status", 
                    interactive=False
                )

        gr.Examples(
            examples=[
                ["A futuristic cityscape at sunset with flying cars and holographic advertisements"],
                ["An astronaut riding a horse in photorealistic style"],
                ["A cute robotic cat sitting on a stack of ancient books, digital art"]
            ],
            inputs=prompt_input
        )

        gr.Markdown("""
        ## Model Information
        - **Model:** [Janus-Pro-7B](https://huggingface.co/deepseek-ai/Janus-Pro-7B)
        - **Output Resolution:** 384x384 pixels
        - **Parallel Generation:** 5 images per request
        """)

        # Footer Section
        gr.Markdown("""
        <hr style="margin-top: 2em; margin-bottom: 1em;">
        <div style="text-align: center; color: #666; font-size: 0.9em;">
            Created with ❤️ by <a href="https://bilsimaging.com" target="_blank" style="color: #2563eb; text-decoration: none;">bilsimaging.com</a>
        </div>
        """)

        # Visitor Badge
        gr.HTML("""
        <div style="text-align: center; margin-top: 1em;">
            <a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FDeepseekJanusPro%2F">
                <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FDeepseekJanusPro%2F&countColor=%23263759" 
                     alt="Visitor Badge"
                     style="display: inline-block; margin: 0 auto;">
            </a>
        </div>
        """)

        generate_btn.click(
            generate_image,
            inputs=[prompt_input, seed_input, guidance_slider, temp_slider],
            outputs=output_gallery,
            api_name="generate"
        )

        demo.load(
            fn=lambda: f"Device Status: {'GPU ✅' if device.type == 'cuda' else 'CPU ⚠️'}",
            outputs=status,
            queue=False
        )

    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch(share=True)