Spaces:
Sleeping
Sleeping
Update theme.py
Browse files
theme.py
CHANGED
|
@@ -1,67 +1,68 @@
|
|
| 1 |
-
import random
|
| 2 |
-
import replicate
|
| 3 |
-
import base64
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
os.environ["REPLICATE_API_TOKEN"] = os.getenv("REPLICATE_API_TOKEN")
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def create_flux_request(prompt_for_image_generation):
|
| 10 |
-
payload = {
|
| 11 |
-
"prompt": prompt_for_image_generation,
|
| 12 |
-
"guidance": 3.5,
|
| 13 |
-
"num_outputs": 1,
|
| 14 |
-
"aspect_ratio": "3:4"
|
| 15 |
-
}
|
| 16 |
-
output = replicate.run(
|
| 17 |
-
"black-forest-labs/flux-dev",
|
| 18 |
-
input=payload
|
| 19 |
-
)
|
| 20 |
-
return output
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def flux_generated_image(prompt_for_image_generation):
|
| 24 |
-
try:
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import replicate
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
os.environ["REPLICATE_API_TOKEN"] = os.getenv("REPLICATE_API_TOKEN")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def create_flux_request(prompt_for_image_generation):
|
| 10 |
+
payload = {
|
| 11 |
+
"prompt": prompt_for_image_generation,
|
| 12 |
+
"guidance": 3.5,
|
| 13 |
+
"num_outputs": 1,
|
| 14 |
+
"aspect_ratio": "3:4"
|
| 15 |
+
}
|
| 16 |
+
output = replicate.run(
|
| 17 |
+
"black-forest-labs/flux-dev",
|
| 18 |
+
input=payload
|
| 19 |
+
)
|
| 20 |
+
return output
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def flux_generated_image(prompt_for_image_generation):
|
| 24 |
+
try:
|
| 25 |
+
p = "Create Non NSFW image." + prompt_for_image_generation
|
| 26 |
+
flux_response_object = create_flux_request(p)
|
| 27 |
+
data_uri = flux_response_object[0].url
|
| 28 |
+
header, encoded = data_uri.split(',', 1)
|
| 29 |
+
file_data = base64.b64decode(encoded)
|
| 30 |
+
random_int_for_file_prefix = random.randint(1, 1000000)
|
| 31 |
+
output_image_file_name = f"{random_int_for_file_prefix}_ide_theme_image.png"
|
| 32 |
+
with open(output_image_file_name, "wb") as f:
|
| 33 |
+
f.write(file_data)
|
| 34 |
+
return {"success": True, "file_name": output_image_file_name}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"success": False, "error": e}
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def create_flux_request_seed(prompt_for_image_generation, seed, aspect_ratio):
|
| 40 |
+
print(f"YE SEED HAI MEPE:{seed}")
|
| 41 |
+
payload = {
|
| 42 |
+
"prompt": prompt_for_image_generation,
|
| 43 |
+
"guidance": 3.5,
|
| 44 |
+
"num_outputs": 1,
|
| 45 |
+
"aspect_ratio": str(aspect_ratio),
|
| 46 |
+
"seed": int(seed)
|
| 47 |
+
|
| 48 |
+
}
|
| 49 |
+
output = replicate.run(
|
| 50 |
+
"black-forest-labs/flux-dev",
|
| 51 |
+
input=payload
|
| 52 |
+
)
|
| 53 |
+
return output
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def flux_generated_image_seed(prompt_for_image_generation, seed, aspect_ratio):
|
| 57 |
+
try:
|
| 58 |
+
flux_response_object = create_flux_request_seed(prompt_for_image_generation, seed, aspect_ratio)
|
| 59 |
+
data_uri = flux_response_object[0].url
|
| 60 |
+
header, encoded = data_uri.split(',', 1)
|
| 61 |
+
file_data = base64.b64decode(encoded)
|
| 62 |
+
random_int_for_file_prefix = random.randint(1, 1000000)
|
| 63 |
+
output_image_file_name = f"{random_int_for_file_prefix}_ide_theme_image.png"
|
| 64 |
+
with open(output_image_file_name, "wb") as f:
|
| 65 |
+
f.write(file_data)
|
| 66 |
+
return {"success": True, "file_name": output_image_file_name}
|
| 67 |
+
except Exception as e:
|
| 68 |
+
return {"success": False, "error": e}
|