File size: 12,174 Bytes
8398621
 
 
00d591a
6e62cd2
915f4ae
8398621
eb9629d
8398621
 
 
 
 
 
 
 
 
 
 
eb9629d
8398621
eb9629d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8398621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915f4ae
8398621
 
 
 
 
 
 
 
 
 
 
eb9629d
 
8398621
eb9629d
8398621
eb9629d
 
 
 
 
 
 
 
8398621
 
 
 
423b272
8398621
 
eb9629d
8398621
 
 
 
 
 
423b272
8398621
 
 
423b272
eb9629d
a7a4022
8398621
 
 
 
a7a4022
8398621
 
 
423b272
eb9629d
 
 
 
 
8398621
 
 
eb9629d
 
 
 
 
 
 
8398621
 
eb9629d
8398621
 
 
eb9629d
 
8398621
 
3f9f026
a7a4022
8398621
 
 
 
 
 
 
 
 
 
 
 
00d591a
d9c9363
eb9629d
8398621
 
 
eb9629d
d9c9363
eb9629d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8398621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb9629d
 
 
 
 
 
 
 
 
 
 
 
 
8398621
eb9629d
8398621
 
eb9629d
8398621
 
eb9629d
 
8398621
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb9629d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
915f4ae
 
8398621
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import spaces
import gradio as gr
import random
import os
import time
import torch
from diffusers import FluxPipeline
from transformers import pipeline

DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {DEVICE}")

DEFAULT_HEIGHT = 1024
DEFAULT_WIDTH = 1024
DEFAULT_GUIDANCE_SCALE = 3.5
DEFAULT_NUM_INFERENCE_STEPS = 15
DEFAULT_MAX_SEQUENCE_LENGTH = 512
HF_TOKEN = os.environ.get("HF_ACCESS_TOKEN")

# Cache for the pipelines
CACHED_PIPE = None
CACHED_LLM_PIPE = None

def load_llm_pipeline():
    """Load the LLM pipeline for prompt enhancement"""
    global CACHED_LLM_PIPE
    if CACHED_LLM_PIPE is not None:
        return CACHED_LLM_PIPE
    
    print("Loading LLM pipeline for prompt enhancement...")
    try:
        # Note: Using a smaller model that's actually available
        # You can replace this with "openai/gpt-oss-120b" if you have access
        llm_pipe = pipeline(
            "text-generation", 
            model="microsoft/Phi-3-mini-4k-instruct",  # Alternative smaller model
            torch_dtype=torch.bfloat16,
            device_map="auto"
        )
        CACHED_LLM_PIPE = llm_pipe
        print("LLM pipeline loaded successfully")
        return llm_pipe
    except Exception as e:
        print(f"Error loading LLM pipeline: {e}")
        # Fallback to a simpler model if the main one fails
        try:
            llm_pipe = pipeline(
                "text-generation",
                model="gpt2",  # Fallback to GPT-2
                device_map="auto"
            )
            CACHED_LLM_PIPE = llm_pipe
            print("Loaded fallback LLM pipeline (GPT-2)")
            return llm_pipe
        except Exception as e2:
            print(f"Error loading fallback LLM pipeline: {e2}")
            return None

def enhance_prompt(prompt, progress=gr.Progress()):
    """Enhance the prompt using LLM"""
    if not prompt:
        return prompt, "Please enter a prompt first."
    
    progress(0.3, desc="Enhancing prompt with AI...")
    
    try:
        llm_pipe = load_llm_pipeline()
        if llm_pipe is None:
            return prompt, "LLM pipeline not available, using original prompt."
        
        # Create enhancement prompt
        messages = [
            {
                "role": "system", 
                "content": "You are a helpful assistant that enhances image generation prompts. Make prompts more detailed, artistic, and visually descriptive while keeping the core concept. Add details about lighting, style, colors, mood, and composition. Keep the enhanced prompt under 200 words."
            },
            {
                "role": "user", 
                "content": f"Enhance this image generation prompt, making it more detailed and artistic: '{prompt}'"
            }
        ]
        
        # Generate enhanced prompt
        result = llm_pipe(
            messages,
            max_new_tokens=200,
            temperature=0.7,
            do_sample=True,
            top_p=0.9
        )
        
        # Extract the enhanced prompt from the response
        if isinstance(result, list) and len(result) > 0:
            enhanced = result[0].get('generated_text', '')
            # Extract only the assistant's response
            if isinstance(enhanced, list):
                for msg in enhanced:
                    if msg.get('role') == 'assistant':
                        enhanced = msg.get('content', prompt)
                        break
            elif isinstance(enhanced, str):
                # Clean up the response if needed
                enhanced = enhanced.strip()
                if enhanced.startswith("Enhanced prompt:"):
                    enhanced = enhanced.replace("Enhanced prompt:", "").strip()
            
            if enhanced and enhanced != prompt:
                return enhanced, "Prompt enhanced successfully!"
            else:
                return prompt, "Using original prompt."
        else:
            return prompt, "Enhancement failed, using original prompt."
            
    except Exception as e:
        print(f"Error during prompt enhancement: {e}")
        return prompt, f"Enhancement error: {e}. Using original prompt."

def load_bnb_4bit_pipeline():
    """Load the 4-bit quantized pipeline"""
    global CACHED_PIPE
    if CACHED_PIPE is not None:
        return CACHED_PIPE
    
    print("Loading 4-bit BNB pipeline...")
    MODEL_ID = "derekl35/FLUX.1-dev-nf4"
    
    start_time = time.time()
    try:
        pipe = FluxPipeline.from_pretrained(
            MODEL_ID,
            torch_dtype=torch.bfloat16
        )
        pipe.enable_model_cpu_offload()
        end_time = time.time()
        mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
        print(f"4-bit BNB pipeline loaded in {end_time - start_time:.2f}s. Memory reserved: {mem_reserved:.2f} GB")
        CACHED_PIPE = pipe
        return pipe
    except Exception as e:
        print(f"Error loading 4-bit BNB pipeline: {e}")
        raise

@spaces.GPU(duration=240)
def generate_image(prompt, use_enhancement=False, progress=gr.Progress(track_tqdm=True)):
    """Generate image using 4-bit quantized model with optional prompt enhancement"""
    if not prompt:
        return None, prompt, "Please enter a prompt."
    
    enhanced_prompt = prompt
    enhancement_status = ""
    
    # Enhance prompt if requested
    if use_enhancement:
        enhanced_prompt, enhancement_status = enhance_prompt(prompt, progress)
    
    progress(0.5, desc="Loading 4-bit quantized model...")
    
    try:
        # Load the 4-bit pipeline
        pipe = load_bnb_4bit_pipeline()
        
        # Set up generation parameters
        pipe_kwargs = {
            "prompt": enhanced_prompt,
            "height": DEFAULT_HEIGHT,
            "width": DEFAULT_WIDTH,
            "guidance_scale": DEFAULT_GUIDANCE_SCALE,
            "num_inference_steps": DEFAULT_NUM_INFERENCE_STEPS,
            "max_sequence_length": DEFAULT_MAX_SEQUENCE_LENGTH,
        }
        
        # Generate seed
        seed = random.getrandbits(64)
        print(f"Using seed: {seed}")
        
        progress(0.7, desc="Generating image...")
        
        # Generate image
        gen_start_time = time.time()
        image = pipe(**pipe_kwargs, generator=torch.manual_seed(seed)).images[0]
        gen_end_time = time.time()
        
        print(f"Image generated in {gen_end_time - gen_start_time:.2f} seconds")
        mem_reserved = torch.cuda.memory_reserved(0)/1024**3 if DEVICE == "cuda" else 0
        print(f"Memory reserved: {mem_reserved:.2f} GB")
        
        status_msg = f"Generation complete! (Seed: {seed})"
        if enhancement_status:
            status_msg = f"{enhancement_status} | {status_msg}"
        
        return image, enhanced_prompt, status_msg
        
    except Exception as e:
        print(f"Error during generation: {e}")
        return None, enhanced_prompt, f"Error: {e}"

@spaces.GPU(duration=60)
def enhance_only(prompt, progress=gr.Progress()):
    """Only enhance the prompt without generating an image"""
    enhanced_prompt, status = enhance_prompt(prompt, progress)
    return enhanced_prompt, status

# Create Gradio interface
with gr.Blocks(title="FLUXllama Enhanced", theme=gr.themes.Soft()) as demo:
    gr.HTML(
        """
        <div style='text-align: center; margin-bottom: 20px;'>
            <h1>FLUXllama Enhanced</h1>
            <p>FLUX.1-dev 4-bit Quantized Version with AI Prompt Enhancement</p>
        </div>
        """
    )
    
    gr.HTML(
        """
        <div class='container' style='display:flex; justify-content:center; gap:12px; margin-bottom: 20px;'>
            <a href="https://huggingface.co/spaces/openfree/Best-AI" target="_blank">
                <img src="https://img.shields.io/static/v1?label=OpenFree&message=BEST%20AI%20Services&color=%230000ff&labelColor=%23000080&logo=huggingface&logoColor=%23ffa500&style=for-the-badge" alt="OpenFree badge">
            </a>
    
            <a href="https://discord.gg/openfreeai" target="_blank">
                <img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="Discord badge">
            </a>
        </div>
        """
    )
    
    with gr.Column():
        prompt_input = gr.Textbox(
            label="Enter your prompt",
            placeholder="e.g., A photorealistic portrait of an astronaut on Mars",
            lines=3
        )
        
        with gr.Row():
            enhance_checkbox = gr.Checkbox(
                label="🎨 Use AI Prompt Enhancement",
                value=False,
                info="Automatically enhance your prompt for better results"
            )
            enhance_only_button = gr.Button("✨ Enhance Only", variant="secondary", scale=1)
        
        enhanced_prompt_display = gr.Textbox(
            label="Enhanced Prompt (will appear after enhancement)",
            lines=3,
            interactive=False,
            visible=True
        )
        
        with gr.Row():
            generate_button = gr.Button("πŸš€ Generate Image", variant="primary", scale=2)
            generate_enhanced_button = gr.Button("🎨 Enhance & Generate", variant="primary", scale=2)
    
    output_image = gr.Image(
        label="Generated Image (4-bit Quantized)",
        type="pil",
        height=600
    )
    
    status_text = gr.Textbox(
        label="Status",
        interactive=False,
        lines=1
    )
    
    # Connect components
    generate_button.click(
        fn=lambda p: generate_image(p, use_enhancement=False),
        inputs=[prompt_input],
        outputs=[output_image, enhanced_prompt_display, status_text]
    )
    
    generate_enhanced_button.click(
        fn=lambda p: generate_image(p, use_enhancement=True),
        inputs=[prompt_input],
        outputs=[output_image, enhanced_prompt_display, status_text]
    )
    
    enhance_only_button.click(
        fn=enhance_only,
        inputs=[prompt_input],
        outputs=[enhanced_prompt_display, status_text]
    )
    
    # Enter key to submit (with enhancement checkbox consideration)
    prompt_input.submit(
        fn=generate_image,
        inputs=[prompt_input, enhance_checkbox],
        outputs=[output_image, enhanced_prompt_display, status_text]
    )
    
    # Example prompts
    gr.Examples(
        examples=[
            "A photorealistic portrait of an astronaut on Mars",
            "Water-color painting of a cat wearing sunglasses",
            "Neo-tokyo cyberpunk cityscape at night, rain-soaked streets, 8K",
            "A majestic dragon flying over a medieval castle at sunset",
            "Abstract art representing the concept of time and space",
            "Detailed oil painting of a steampunk clockwork city",
            "Underwater scene with bioluminescent creatures in deep ocean",
            "Japanese garden in autumn with falling maple leaves"
        ],
        inputs=prompt_input
    )
    
    gr.HTML(
        """
        <div style='text-align: center; margin-top: 20px; padding: 20px; background-color: #f0f0f0; border-radius: 10px;'>
            <h3>✨ Prompt Enhancement Feature</h3>
            <p>This app now includes AI-powered prompt enhancement! The enhancement feature will:</p>
            <ul style='text-align: left; display: inline-block;'>
                <li>Add artistic details and visual descriptions</li>
                <li>Specify lighting, mood, and atmosphere</li>
                <li>Include style and composition elements</li>
                <li>Make your prompts more effective for image generation</li>
            </ul>
            <p><strong>How to use:</strong></p>
            <p>1. Enter a simple prompt</p>
            <p>2. Click "✨ Enhance Only" to preview the enhanced version</p>
            <p>3. Click "🎨 Enhance & Generate" to enhance and generate in one step</p>
            <p>4. Or check the enhancement checkbox and click Generate</p>
        </div>
        """
    )

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