File size: 16,273 Bytes
07cfdaf
 
 
4f62561
07cfdaf
 
 
 
 
 
 
 
14e2b9a
07cfdaf
 
 
 
 
 
 
 
4f62561
07cfdaf
4f62561
 
07cfdaf
4f62561
 
 
07cfdaf
4f62561
 
 
 
 
 
07cfdaf
4f62561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cfdaf
 
 
 
4f62561
 
 
 
07cfdaf
4f62561
 
 
07cfdaf
 
4f62561
 
07cfdaf
4f62561
 
 
 
 
 
 
 
07cfdaf
 
4f62561
07cfdaf
 
 
4f62561
 
07cfdaf
4f62561
07cfdaf
4f62561
07cfdaf
4f62561
 
 
07cfdaf
4f62561
 
 
 
 
 
 
 
 
07cfdaf
 
4f62561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cfdaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14e2b9a
 
 
 
 
4f62561
 
 
 
 
 
 
 
07cfdaf
4f62561
 
 
 
 
 
 
a57493c
 
4f62561
 
07cfdaf
4f62561
 
 
 
 
 
751c8bc
 
 
 
 
 
07cfdaf
4f62561
 
 
 
 
 
 
 
07cfdaf
 
 
 
 
 
 
 
 
4f62561
 
 
07cfdaf
 
4f62561
 
 
 
 
 
 
 
 
 
07cfdaf
 
14e2b9a
07cfdaf
4f62561
 
07cfdaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f62561
07cfdaf
 
 
 
 
 
 
 
14e2b9a
43293ec
 
 
 
 
 
 
07cfdaf
 
 
 
4f62561
 
07cfdaf
 
 
 
4f62561
 
07cfdaf
 
4f62561
07cfdaf
4f62561
 
 
 
07cfdaf
 
14e2b9a
 
 
07cfdaf
 
 
 
 
 
 
 
 
 
14e2b9a
07cfdaf
7ced770
07cfdaf
 
7ced770
 
 
 
 
 
 
07cfdaf
 
 
 
 
 
4f62561
07cfdaf
4f62561
 
 
 
 
 
07cfdaf
 
 
 
 
 
 
 
 
 
 
4f62561
07cfdaf
 
 
 
 
 
 
 
4f62561
07cfdaf
 
6e4d5c2
4f62561
07cfdaf
 
4f62561
 
07cfdaf
 
 
 
 
 
 
 
 
 
 
 
 
14e2b9a
 
ecf5bc5
14e2b9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cfdaf
 
 
 
 
14e2b9a
07cfdaf
4f62561
 
 
07cfdaf
 
 
4f62561
 
 
07cfdaf
4f62561
07cfdaf
 
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import os
import requests
import time
import functools
import threading
import uuid
import base64
from pathlib import Path
from dotenv import load_dotenv
import gradio as gr
import random
import torch
import io
from PIL import Image, ImageDraw, ImageFont
from transformers import AutoTokenizer, AutoModelForSequenceClassification

load_dotenv()
API_KEY = os.getenv("WAVESPEED_API_KEY")
if not API_KEY:
    raise ValueError("WAVESPEED_API_KEY is not set in environment variables")


MODEL_URL = "TostAI/nsfw-text-detection-large"
TITLE = "🖼️🔍 Image Prompt Safety Classifier 🛡️"
DESCRIPTION = "✨ Enter an image generation prompt to classify its safety level! ✨"

# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_URL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_URL)

# Define class names with emojis and detailed descriptions
CLASS_NAMES = {
    0: "✅ SAFE - This prompt is appropriate and harmless.",
    1: "⚠️ QUESTIONABLE - This prompt may require further review.",
    2: "🚫 UNSAFE - This prompt is likely to generate inappropriate content."
}


@functools.lru_cache(maxsize=128)
def classify_text(text):
    inputs = tokenizer(text,
                       return_tensors="pt",
                       truncation=True,
                       padding=True,
                       max_length=1024)

    with torch.no_grad():
        outputs = model(**inputs)

    logits = outputs.logits
    predicted_class = torch.argmax(logits, dim=1).item()

    return predicted_class, CLASS_NAMES[predicted_class]


class ClientManager:
    _instances = {}
    _lock = threading.Lock()

    @classmethod
    def get_manager(cls, client_id=None):
        if not client_id:
            client_id = str(uuid.uuid4())

        with cls._lock:
            if client_id not in cls._instances:
                cls._instances[client_id] = ClientGenerationManager()
            return cls._instances[client_id]

    @classmethod
    def cleanup_old_clients(cls, max_age=3600):  # 1 hour default
        current_time = time.time()
        with cls._lock:
            to_remove = []
            for client_id, manager in cls._instances.items():
                if (hasattr(manager, "last_activity")
                        and current_time - manager.last_activity > max_age):
                    to_remove.append(client_id)

            for client_id in to_remove:
                del cls._instances[client_id]


class ClientGenerationManager:

    def __init__(self):
        self.lock = threading.Lock()
        self.last_activity = time.time()
        self.request_timestamps = []  # Track timestamps of requests

    def update_activity(self):
        with self.lock:
            self.last_activity = time.time()

    def add_request_timestamp(self):
        with self.lock:
            self.request_timestamps.append(time.time())

    def has_exceeded_limit(self, limit=20):
        with self.lock:
            current_time = time.time()
            # Filter timestamps to only include those within the last hour
            self.request_timestamps = [
                ts for ts in self.request_timestamps
                if current_time - ts <= 3600
            ]
            return len(self.request_timestamps) >= limit


class SessionManager:
    _instances = {}
    _lock = threading.Lock()

    @classmethod
    def get_manager(cls, session_id=None):
        if session_id is None:
            session_id = str(uuid.uuid4())

        with cls._lock:
            if session_id not in cls._instances:
                cls._instances[session_id] = GenerationManager()
            return session_id, cls._instances[session_id]

    @classmethod
    def cleanup_old_sessions(cls, max_age=3600):  # 1 hour default
        current_time = time.time()
        with cls._lock:
            to_remove = []
            for session_id, manager in cls._instances.items():
                if (hasattr(manager, "last_activity")
                        and current_time - manager.last_activity > max_age):
                    to_remove.append(session_id)

            for session_id in to_remove:
                del cls._instances[session_id]


class GenerationManager:

    def __init__(self):
        self.last_activity = time.time()
        self.request_timestamps = []  # Track timestamps of requests

    def update_activity(self):
        self.last_activity = time.time()

    def add_request_timestamp(self):
        self.request_timestamps.append(time.time())

    def has_exceeded_limit(self,
                           limit=10):  # Default limit: 10 requests per hour
        current_time = time.time()
        # Filter timestamps to only include those within the last hour
        self.request_timestamps = [
            ts for ts in self.request_timestamps if current_time - ts <= 3600
        ]
        return len(self.request_timestamps) >= limit


@torch.no_grad()
def classify_prompt(prompt):
    inputs = tokenizer(prompt,
                       return_tensors="pt",
                       truncation=True,
                       max_length=512)
    outputs = model(**inputs)
    return torch.argmax(outputs.logits).item()


def image_to_base64(file_path):
    with open(file_path, "rb") as f:
        return base64.b64encode(f.read()).decode()


def decode_base64_to_image(base64_str):
    image_data = base64.b64decode(base64_str)
    return Image.open(io.BytesIO(image_data))


def generate_image(
    image_file,
    prompt,
    seed,
    session_id,
    enable_safety_checker,
    request: gr.Request,
):
    try:
        client_ip = request.client.host
        x_forwarded_for = request.headers.get('x-forwarded-for')
        if x_forwarded_for:
            client_ip = x_forwarded_for
        print(f"Client IP: {client_ip}")
        client_generation_manager = ClientManager.get_manager(client_ip)
        client_generation_manager.update_activity()
        if client_generation_manager.has_exceeded_limit(limit=10):
            error_message = "❌ Your network has exceeded the limit of 10 requests per hour. Please try again later."
            yield error_message, None, "", None
            return

        client_generation_manager.add_request_timestamp()
        """Generate images with big status box during generation"""
        # Get or create a session manager
        session_id, manager = SessionManager.get_manager(session_id)
        manager.update_activity()

        # # Check if the user has exceeded the request limit
        # if manager.has_exceeded_limit(
        #         limit=10):  # Set the limit to 10 requests per hour
        #     error_message = "❌ You have exceeded the limit of 10 requests per hour. Please try again later."
        #     yield error_message, None, "", None
        #     return

        # Add the current request timestamp
        manager.add_request_timestamp()

        if not prompt or prompt.strip() == "":
            # Handle empty prompt case
            error_message = "⚠️ Please enter a prompt first"
            yield error_message, None, "", None
            return

        error_messages = []
        if not image_file:
            error_messages.append("Please upload an image file")
        elif not Path(image_file).exists():
            error_messages.append("File does not exist")
        if not prompt.strip():
            error_messages.append("Prompt cannot be empty")
        if error_messages:
            error_message = "❌ Input validation failed: " + ", ".join(
                error_messages)
            yield error_message, None, "", None
            return

        # Check if the prompt is safe
        classification, message = classify_text(prompt)
        if classification == 2:  # UNSAFE
            yield "❌ NSFW prompt detected", None, "", None
            return

        # Status message
        status_message = f"🔄 PROCESSING: '{prompt}'"
        yield status_message, None, "", None

        try:
            base64_image = image_to_base64(image_file)
            input_image = decode_base64_to_image(base64_image)
        except Exception as e:
            error_message = f"❌ File processing failed: {str(e)}"
            yield error_message, None, "", None
            return

        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {API_KEY}",
        }
        payload = {
            "enable_safety_checker": enable_safety_checker,
            "image": base64_image,
            "prompt": prompt,
            "seed": int(seed) if seed != -1 else random.randint(0, 999999)
        }

        response = requests.post(
            "https://api.wavespeed.ai/api/v3/wavespeed-ai/flux-kontext-dev-ultra-fast",
            headers=headers,
            json=payload,
            timeout=30)
        response.raise_for_status()

        request_id = response.json()["data"]["id"]
        result_url = f"https://api.wavespeed.ai/api/v3/predictions/{request_id}/result"
        start_time = time.time()

        for _ in range(60):
            time.sleep(1.0)
            resp = requests.get(result_url, headers=headers)
            resp.raise_for_status()

            data = resp.json()["data"]
            status = data["status"]

            if status == "completed":
                elapsed = time.time() - start_time
                output_url = data["outputs"][0]
                has_nsfw_content = data["has_nsfw_contents"][0]
                if has_nsfw_content:
                    error_message = "❌ NSFW content detected in the output"
                    yield error_message, None, "", None
                else:
                    yield f"🎉 Generation successful! Time taken {elapsed:.1f}s", output_url, output_url, update_recent_gallery(
                        prompt, input_image, output_url)
                return
            elif status == "failed":
                raise Exception(data.get("error", "Unknown error"))
            else:
                error_message = f"⏳ Current status: {status.capitalize()}..."
                yield error_message, None, "", None

        raise Exception("Generation timed out")

    except Exception as e:
        error_message = f"❌ Generation failed: {str(e)}"
        yield error_message, None, "", None


# Schedule periodic cleanup of old sessions
def cleanup_task():
    SessionManager.cleanup_old_sessions()
    ClientManager.cleanup_old_clients()
    # Schedule the next cleanup
    threading.Timer(3600, cleanup_task).start()  # Run every hour


# Store recent generations
recent_generations = []

with gr.Blocks(theme=gr.themes.Soft(),
               css="""
    .status-box { padding: 10px; border-radius: 5px; margin: 5px; }
    .safe { background: #e8f5e9; border: 1px solid #a5d6a7; }
    .warning { background: #fff3e0; border: 1px solid #ffcc80; }
    .error { background: #ffebee; border: 1px solid #ef9a9a; }
    """) as app:

    session_id = gr.State(str(uuid.uuid4()))

    gr.Markdown("# 🖼️FLUX Kontext Dev Ultra Fast Live")
    gr.Markdown(
        "FLUX Kontext dev is a new SOTA image editing model published by Black Forest Labs. We have deployed it on [WaveSpeedAI](https://wavespeed.ai/) for ultra-fast image editing. You can use it to edit images in various styles, add objects, or even change the mood of the image. It supports both text prompts and image inputs."
    )
    gr.Markdown(
        "- [FLUX Kontext dev on WaveSpeedAI](https://wavespeed.ai/models/wavespeed-ai/flux-kontext-dev)"
        "\n"
        "- [FLUX Kontext dev LoRA on WaveSpeedAI](https://wavespeed.ai/models/wavespeed-ai/flux-kontext-dev-lora)"
        "\n"
        "- [FLUX Kontext dev Ultra Fast on WaveSpeedAI](https://wavespeed.ai/models/wavespeed-ai/flux-kontext-dev-ultra-fast)"
        "\n"
        "- [FLUX Kontext dev LoRA Ultra Fast on WaveSpeedAI](https://wavespeed.ai/models/wavespeed-ai/flux-kontext-dev-lora-ultra-fast)"
    )

    with gr.Row():
        with gr.Column(scale=1):
            image_file = gr.Image(label="Upload Image",
                                  type="filepath",
                                  sources=["upload", "clipboard"],
                                  interactive=True,
                                  image_mode="RGB",
                                  value="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/para-attn/flux-original.png")
            prompt = gr.Textbox(label="Prompt",
                                placeholder="Please enter your prompt...",
                                lines=3,
                                value="Convert the image into Claymation style.")
            seed = gr.Number(label="seed",
                             value=-1,
                             minimum=-1,
                             maximum=999999,
                             step=1)
            random_btn = gr.Button("random🎲seed", variant="secondary")
            enable_safety = gr.Checkbox(label="🔒 Enable Safety Checker",
                                        value=True,
                                        interactive=False)
        with gr.Column(scale=1):
            output_image = gr.Image(label="Generated Result")
            status = gr.Textbox(label="Status", elem_classes=["status-box"])
            output_url = gr.Textbox(label="Image URL",
                                    interactive=True,
                                    visible=False)
            submit_btn = gr.Button("Start Generation", variant="primary")
    gr.Examples(
        examples=[
            [
                "Convert the image into Claymation style.",
                "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/para-attn/flux-original.png"
            ],
            [
                "Convert the image into Ghibli style.",
                "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/penguin.png"
            ],
            [
                "Add sunglasses to the face of the statue.",
                "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/flux_ip_adapter_input.jpg"
            ],
            # [
            #     'Convert the image into an ink sketch style.',
            #     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
            # ],
            # [
            #     'Add a butterfly to the scene.',
            #     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/controlnet_depth_result.png"
            # ]
        ],
        inputs=[prompt, image_file],
        label="Examples")

    with gr.Accordion("Recent Generations (last 16)", open=False):
        recent_gallery = gr.Gallery(label="Prompt and Output",
                                    columns=4,
                                    interactive=False)

    def get_recent_gallery_items():
        gallery_items = []
        for r in reversed(recent_generations):
            if any(x is None for x in r.values()):
                continue
            gallery_items.append((r["input"], f"Input: {r['prompt']}"))
            gallery_items.append((r["output"], f"Output: {r['prompt']}"))
        return gr.update(value=gallery_items)

    def update_recent_gallery(prompt, input_image, output_image):
        recent_generations.append({
            "prompt": prompt,
            "input": input_image,
            "output": output_image,
        })
        if len(recent_generations) > 16:
            recent_generations.pop(0)
        return get_recent_gallery_items()

    random_btn.click(fn=lambda: random.randint(0, 999999), outputs=seed)

    submit_btn.click(
        generate_image,
        inputs=[image_file, prompt, seed, session_id, enable_safety],
        outputs=[status, output_image, output_url, recent_gallery],
        api_name=False,
        max_batch_size=10,
        concurrency_limit=20,
        concurrency_id="generation",
    )

if __name__ == "__main__":
    # Start the cleanup task
    cleanup_task()
    app.queue(max_size=20).launch(
        server_name="0.0.0.0",
        max_threads=10,
        share=False,
    )