Update app.py
Browse files
app.py
CHANGED
|
@@ -61,6 +61,17 @@ def load_css(path: str) -> str:
|
|
| 61 |
return Path(path).read_text(encoding="utf-8")
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
class I18N:
|
| 65 |
"""
|
| 66 |
Simple i18n manager backed by a JSON string.
|
|
@@ -148,10 +159,8 @@ def as_pil_list(gallery_value: Any) -> List[Image.Image]:
|
|
| 148 |
return []
|
| 149 |
imgs: List[Image.Image] = []
|
| 150 |
for item in gallery_value:
|
| 151 |
-
if isinstance(item, tuple) and len(item) >= 1
|
| 152 |
-
|
| 153 |
-
else:
|
| 154 |
-
imgs.append(item)
|
| 155 |
return imgs
|
| 156 |
|
| 157 |
|
|
@@ -166,9 +175,9 @@ def ensure_pil(x: Any) -> Image.Image:
|
|
| 166 |
:raises TypeError: If unsupported type.
|
| 167 |
"""
|
| 168 |
if isinstance(x, Image.Image):
|
| 169 |
-
return x
|
| 170 |
if isinstance(x, torch.Tensor):
|
| 171 |
-
return to_pil_image(x.clamp(0, 1))
|
| 172 |
raise TypeError(f"Unsupported output type: {type(x)}")
|
| 173 |
|
| 174 |
|
|
@@ -1734,23 +1743,15 @@ class TTPApp:
|
|
| 1734 |
return demo
|
| 1735 |
|
| 1736 |
|
| 1737 |
-
def main() -> None:
|
| 1738 |
-
"""
|
| 1739 |
-
Gradio entrypoint.
|
| 1740 |
|
| 1741 |
-
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
| 1745 |
-
|
| 1746 |
-
|
| 1747 |
-
codegen = CodeGenerator()
|
| 1748 |
-
engine = TransformationEngine(factory)
|
| 1749 |
-
app = TTPApp(i18n=i18n, engine=engine, codegen=codegen)
|
| 1750 |
-
|
| 1751 |
-
demo = app.build()
|
| 1752 |
-
demo.launch(css=load_css(DEFAULT_CSS_PATH), ssr_mode=False, debug=False)
|
| 1753 |
|
|
|
|
| 1754 |
|
| 1755 |
if __name__ == "__main__":
|
| 1756 |
-
|
|
|
|
| 61 |
return Path(path).read_text(encoding="utf-8")
|
| 62 |
|
| 63 |
|
| 64 |
+
|
| 65 |
+
def to_rgb(img: Image.Image) -> Image.Image:
|
| 66 |
+
if img.mode == "RGB":
|
| 67 |
+
return img
|
| 68 |
+
if img.mode in ("RGBA", "LA"):
|
| 69 |
+
bg = Image.new("RGB", img.size, (255, 255, 255))
|
| 70 |
+
bg.paste(img, mask=img.split()[-1])
|
| 71 |
+
return bg
|
| 72 |
+
return img.convert("RGB")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
class I18N:
|
| 76 |
"""
|
| 77 |
Simple i18n manager backed by a JSON string.
|
|
|
|
| 159 |
return []
|
| 160 |
imgs: List[Image.Image] = []
|
| 161 |
for item in gallery_value:
|
| 162 |
+
im = item[0] if isinstance(item, tuple) and len(item) >= 1 else item
|
| 163 |
+
imgs.append(to_rgb(im))
|
|
|
|
|
|
|
| 164 |
return imgs
|
| 165 |
|
| 166 |
|
|
|
|
| 175 |
:raises TypeError: If unsupported type.
|
| 176 |
"""
|
| 177 |
if isinstance(x, Image.Image):
|
| 178 |
+
return to_rgb(x)
|
| 179 |
if isinstance(x, torch.Tensor):
|
| 180 |
+
return to_rgb(to_pil_image(x.clamp(0, 1)))
|
| 181 |
raise TypeError(f"Unsupported output type: {type(x)}")
|
| 182 |
|
| 183 |
|
|
|
|
| 1743 |
return demo
|
| 1744 |
|
| 1745 |
|
|
|
|
|
|
|
|
|
|
| 1746 |
|
| 1747 |
+
texts_json = load_texts_json(DEFAULT_I18N_PATH)
|
| 1748 |
+
i18n = I18N(texts_json, default_lang="EN")
|
| 1749 |
+
factory = TransformFactory()
|
| 1750 |
+
codegen = CodeGenerator()
|
| 1751 |
+
engine = TransformationEngine(factory)
|
| 1752 |
+
app = TTPApp(i18n=i18n, engine=engine, codegen=codegen)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1753 |
|
| 1754 |
+
demo = app.build()
|
| 1755 |
|
| 1756 |
if __name__ == "__main__":
|
| 1757 |
+
demo.launch(css=load_css(DEFAULT_CSS_PATH), ssr_mode=False, debug=False)
|