Spaces:
Running
on
Zero
Running
on
Zero
v3p1
Browse files
utils.py
CHANGED
@@ -6,8 +6,7 @@ import json
|
|
6 |
import torch
|
7 |
from PIL import Image, PngImagePlugin
|
8 |
from datetime import datetime
|
9 |
-
from
|
10 |
-
from typing import Callable, Dict, Optional, Tuple
|
11 |
from diffusers import (
|
12 |
DDIMScheduler,
|
13 |
DPMSolverMultistepScheduler,
|
@@ -15,8 +14,6 @@ from diffusers import (
|
|
15 |
EulerAncestralDiscreteScheduler,
|
16 |
EulerDiscreteScheduler,
|
17 |
)
|
18 |
-
import base64
|
19 |
-
from io import BytesIO
|
20 |
|
21 |
MAX_SEED = np.iinfo(np.int32).max
|
22 |
|
@@ -46,7 +43,7 @@ def aspect_ratio_handler(aspect_ratio: str, custom_width: int, custom_height: in
|
|
46 |
width, height = parse_aspect_ratio(aspect_ratio)
|
47 |
return width, height
|
48 |
|
49 |
-
def get_scheduler(scheduler_config: Dict, name: str) -> Optional[
|
50 |
scheduler_factory_map = {
|
51 |
"DPM++ 2M Karras": lambda: DPMSolverMultistepScheduler.from_config(scheduler_config, use_karras_sigmas=True),
|
52 |
"DPM++ SDE Karras": lambda: DPMSolverSinglestepScheduler.from_config(scheduler_config, use_karras_sigmas=True),
|
@@ -95,6 +92,15 @@ def is_google_colab():
|
|
95 |
except:
|
96 |
return False
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def validate_json_parameters(json_str):
|
99 |
try:
|
100 |
params = json.loads(json_str)
|
@@ -106,9 +112,4 @@ def validate_json_parameters(json_str):
|
|
106 |
except json.JSONDecodeError:
|
107 |
raise ValueError("Invalid JSON format")
|
108 |
except Exception as e:
|
109 |
-
raise ValueError(f"Error parsing JSON: {str(e)}")
|
110 |
-
|
111 |
-
def image_to_base64(image):
|
112 |
-
buffered = BytesIO()
|
113 |
-
image.save(buffered, format="PNG")
|
114 |
-
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
6 |
import torch
|
7 |
from PIL import Image, PngImagePlugin
|
8 |
from datetime import datetime
|
9 |
+
from typing import Dict, Optional, Tuple
|
|
|
10 |
from diffusers import (
|
11 |
DDIMScheduler,
|
12 |
DPMSolverMultistepScheduler,
|
|
|
14 |
EulerAncestralDiscreteScheduler,
|
15 |
EulerDiscreteScheduler,
|
16 |
)
|
|
|
|
|
17 |
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
|
|
|
43 |
width, height = parse_aspect_ratio(aspect_ratio)
|
44 |
return width, height
|
45 |
|
46 |
+
def get_scheduler(scheduler_config: Dict, name: str) -> Optional[DDIMScheduler]:
|
47 |
scheduler_factory_map = {
|
48 |
"DPM++ 2M Karras": lambda: DPMSolverMultistepScheduler.from_config(scheduler_config, use_karras_sigmas=True),
|
49 |
"DPM++ SDE Karras": lambda: DPMSolverSinglestepScheduler.from_config(scheduler_config, use_karras_sigmas=True),
|
|
|
92 |
except:
|
93 |
return False
|
94 |
|
95 |
+
def get_image_metadata(image_path):
|
96 |
+
try:
|
97 |
+
with Image.open(image_path) as img:
|
98 |
+
metadata = img.info.get('metadata', '{}')
|
99 |
+
return json.loads(metadata)
|
100 |
+
except Exception as e:
|
101 |
+
print(f"Error reading metadata from {image_path}: {str(e)}")
|
102 |
+
return {}
|
103 |
+
|
104 |
def validate_json_parameters(json_str):
|
105 |
try:
|
106 |
params = json.loads(json_str)
|
|
|
112 |
except json.JSONDecodeError:
|
113 |
raise ValueError("Invalid JSON format")
|
114 |
except Exception as e:
|
115 |
+
raise ValueError(f"Error parsing JSON: {str(e)}")
|
|
|
|
|
|
|
|
|
|