Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -27,6 +27,51 @@ class PropertyConfig:
|
|
27 |
image_settings: ImageSettings = field(default_factory=ImageSettings)
|
28 |
description: str = field(default="", metadata={"label": "Description"})
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def handle_load_metadata(image_data: ImageMeta | None) -> List[Any]:
|
31 |
"""
|
32 |
Processes image metadata and maps it to output components.
|
@@ -45,14 +90,14 @@ def handle_load_metadata(image_data: ImageMeta | None) -> List[Any]:
|
|
45 |
raw_values = transfer_metadata(output_fields, metadata, dataclass_fields)
|
46 |
|
47 |
output_values = [gr.skip()] * len(output_fields)
|
48 |
-
for i, (component, value) in enumerate(zip(output_fields, raw_values)):
|
49 |
if hasattr(component, 'root_label'):
|
50 |
output_values[i] = create_dataclass_instance(PropertyConfig, value)
|
51 |
else:
|
52 |
-
output_values[i] = gr.update(value=value)
|
53 |
|
54 |
return output_values
|
55 |
-
|
56 |
def save_image_with_metadata(image_data: Any, *inputs: Any) -> str | None:
|
57 |
"""
|
58 |
Saves an image with updated metadata to a file.
|
|
|
27 |
image_settings: ImageSettings = field(default_factory=ImageSettings)
|
28 |
description: str = field(default="", metadata={"label": "Description"})
|
29 |
|
30 |
+
def infer_type(s: str):
|
31 |
+
"""
|
32 |
+
Infers and converts a string to the most likely data type.
|
33 |
+
|
34 |
+
It attempts conversions in the following order:
|
35 |
+
1. Integer
|
36 |
+
2. Float
|
37 |
+
3. Boolean (case-insensitive 'true' or 'false')
|
38 |
+
If all conversions fail, it returns the original string.
|
39 |
+
|
40 |
+
Args:
|
41 |
+
s: The input string to be converted.
|
42 |
+
|
43 |
+
Returns:
|
44 |
+
The converted value (int, float, bool) or the original string.
|
45 |
+
"""
|
46 |
+
if not isinstance(s, str):
|
47 |
+
# If the input is not a string, return it as is.
|
48 |
+
return s
|
49 |
+
|
50 |
+
# 1. Try to convert to an integer
|
51 |
+
try:
|
52 |
+
return int(s)
|
53 |
+
except ValueError:
|
54 |
+
# Not an integer, continue...
|
55 |
+
pass
|
56 |
+
|
57 |
+
# 2. Try to convert to a float
|
58 |
+
try:
|
59 |
+
return float(s)
|
60 |
+
except ValueError:
|
61 |
+
# Not a float, continue...
|
62 |
+
pass
|
63 |
+
|
64 |
+
# 3. Check for a boolean value
|
65 |
+
# This explicit check is important because bool('False') evaluates to True.
|
66 |
+
s_lower = s.lower()
|
67 |
+
if s_lower == 'true':
|
68 |
+
return True
|
69 |
+
if s_lower == 'false':
|
70 |
+
return False
|
71 |
+
|
72 |
+
# 4. If nothing else worked, return the original string
|
73 |
+
return s
|
74 |
+
|
75 |
def handle_load_metadata(image_data: ImageMeta | None) -> List[Any]:
|
76 |
"""
|
77 |
Processes image metadata and maps it to output components.
|
|
|
90 |
raw_values = transfer_metadata(output_fields, metadata, dataclass_fields)
|
91 |
|
92 |
output_values = [gr.skip()] * len(output_fields)
|
93 |
+
for i, (component, value) in enumerate(zip(output_fields, raw_values)):
|
94 |
if hasattr(component, 'root_label'):
|
95 |
output_values[i] = create_dataclass_instance(PropertyConfig, value)
|
96 |
else:
|
97 |
+
output_values[i] = gr.update(value=infer_type(value))
|
98 |
|
99 |
return output_values
|
100 |
+
|
101 |
def save_image_with_metadata(image_data: Any, *inputs: Any) -> str | None:
|
102 |
"""
|
103 |
Saves an image with updated metadata to a file.
|