Spaces:
Runtime error
Runtime error
Commit
·
c0fb24c
1
Parent(s):
6bf73bb
Auto deploy
Browse files- stCompressService.py +22 -5
stCompressService.py
CHANGED
@@ -36,7 +36,7 @@ def loadModel(qp: int, local: pathlib.Path, device, mse: bool):
|
|
36 |
|
37 |
|
38 |
|
39 |
-
@st.
|
40 |
def compressImage(image: torch.Tensor, model: BaseCompressor, crop: bool) -> File:
|
41 |
image = convert_image_dtype(image)
|
42 |
|
@@ -52,7 +52,7 @@ def compressImage(image: torch.Tensor, model: BaseCompressor, crop: bool) -> Fil
|
|
52 |
return File(headers[0], binaries[0])
|
53 |
|
54 |
|
55 |
-
@st.
|
56 |
def decompressImage(sourceFile: File, model: BaseCompressor) -> torch.ByteTensor:
|
57 |
binaries = sourceFile.Content
|
58 |
|
@@ -135,8 +135,14 @@ def main(debug: bool, quiet: bool, qp: int, disable_gpu: bool):
|
|
135 |
|
136 |
result = decompressImage(binaryFile, model)
|
137 |
st.image(result.cpu().permute(1, 2, 0).numpy())
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
try:
|
141 |
image = Image.open(uploadedFile)
|
142 |
except PIL.UnidentifiedImageError:
|
@@ -160,8 +166,19 @@ def main(debug: bool, quiet: bool, qp: int, disable_gpu: bool):
|
|
160 |
|
161 |
st.text(str(result))
|
162 |
|
163 |
-
|
|
|
|
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
st.markdown("""
|
166 |
<br/>
|
167 |
<br/>
|
|
|
36 |
|
37 |
|
38 |
|
39 |
+
@st.cache
|
40 |
def compressImage(image: torch.Tensor, model: BaseCompressor, crop: bool) -> File:
|
41 |
image = convert_image_dtype(image)
|
42 |
|
|
|
52 |
return File(headers[0], binaries[0])
|
53 |
|
54 |
|
55 |
+
@st.cache
|
56 |
def decompressImage(sourceFile: File, model: BaseCompressor) -> torch.ByteTensor:
|
57 |
binaries = sourceFile.Content
|
58 |
|
|
|
135 |
|
136 |
result = decompressImage(binaryFile, model)
|
137 |
st.image(result.cpu().permute(1, 2, 0).numpy())
|
138 |
+
|
139 |
+
downloadButton = st.empty()
|
140 |
+
|
141 |
+
done = downloadButton.download_button("Click to download restored image", data=bytes(encode_png(result.cpu()).tolist()), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["png"]), mime="image/png")
|
142 |
+
|
143 |
+
if done:
|
144 |
+
downloadButton.empty()
|
145 |
+
elif uploadedFile.name.lower().endswith((".png", ".jpg", ".jpeg")):
|
146 |
try:
|
147 |
image = Image.open(uploadedFile)
|
148 |
except PIL.UnidentifiedImageError:
|
|
|
166 |
|
167 |
st.text(str(result))
|
168 |
|
169 |
+
downloadButton = st.empty()
|
170 |
+
|
171 |
+
done = st.download_button("Click to download compressed file", data=result.serialize(), file_name=".".join(uploadedFile.name.split(".")[:-1] + ["mcq"]), mime="image/mcq")
|
172 |
|
173 |
+
if done:
|
174 |
+
downloadButton.empty()
|
175 |
+
else:
|
176 |
+
st.markdown("""
|
177 |
+
<img src="https://img.shields.io/badge/ERROR-red?style=for-the-badge" alt="ERROR"/>
|
178 |
+
|
179 |
+
> Not supported image formate. Please try other images.
|
180 |
+
""", unsafe_allow_html=True)
|
181 |
+
return
|
182 |
st.markdown("""
|
183 |
<br/>
|
184 |
<br/>
|