File size: 2,324 Bytes
e3f78f6
 
 
 
 
6e2d4fb
e3f78f6
 
 
 
 
 
77169b4
e3f78f6
77169b4
cc3ce98
e3f78f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import replicate
import base64
import os


os.environ["REPLICATE_API_TOKEN"] = os.getenv("REPLICATE_API_TOKEN")


def create_flux_request(prompt_for_image_generation):
    payload = {
        "prompt": prompt_for_image_generation,
        "guidance": 3,
        "num_outputs": 1,
        "aspect_ratio": "3:4",
        "disable_safety_checker": True
    }
    output = replicate.run(
        "black-forest-labs/flux-dev",
        input=payload
    )
    return output


def flux_generated_image(prompt_for_image_generation):
    try:
        p = "Create Non NSFW image." + prompt_for_image_generation
        flux_response_object = create_flux_request(p)
        data_uri = flux_response_object[0].url
        header, encoded = data_uri.split(',', 1)
        file_data = base64.b64decode(encoded)
        random_int_for_file_prefix = random.randint(1, 1000000)
        output_image_file_name = f"{random_int_for_file_prefix}_ide_theme_image.png"
        with open(output_image_file_name, "wb") as f:
            f.write(file_data)
        return {"success": True, "file_name": output_image_file_name}
    except Exception as e:
        return {"success": False, "error": e}


def create_flux_request_seed(prompt_for_image_generation, seed, aspect_ratio):
    print(f"YE SEED HAI MEPE:{seed}")
    payload = {
        "prompt": prompt_for_image_generation,
        "guidance": 3.5,
        "num_outputs": 1,
        "aspect_ratio": str(aspect_ratio),
        "seed": int(seed)

    }
    output = replicate.run(
        "black-forest-labs/flux-dev",
        input=payload
    )
    return output


def flux_generated_image_seed(prompt_for_image_generation, seed, aspect_ratio):
    try:
        flux_response_object = create_flux_request_seed(prompt_for_image_generation, seed, aspect_ratio)
        data_uri = flux_response_object[0].url
        header, encoded = data_uri.split(',', 1)
        file_data = base64.b64decode(encoded)
        random_int_for_file_prefix = random.randint(1, 1000000)
        output_image_file_name = f"{random_int_for_file_prefix}_ide_theme_image.png"
        with open(output_image_file_name, "wb") as f:
            f.write(file_data)
        return {"success": True, "file_name": output_image_file_name}
    except Exception as e:
        return {"success": False, "error": e}