Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -14
src/streamlit_app.py
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow import keras
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
|
|
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Set Hugging Face cache dir to a writable path
|
| 12 |
-
HF_CACHE_DIR = "./hf_cache"
|
| 13 |
-
os.environ["HF_HOME"] = HF_CACHE_DIR
|
| 14 |
-
os.environ["HF_HUB_CACHE"] = HF_CACHE_DIR
|
| 15 |
-
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
| 16 |
-
|
| 17 |
-
# Streamlit UI
|
| 18 |
st.set_page_config(page_title="NaxiLowLight Enhancer", layout="centered")
|
| 19 |
st.title("π NaxiLowLight - Low-Light Image Enhancer")
|
| 20 |
|
|
|
|
| 21 |
@st.cache_resource
|
| 22 |
-
def
|
| 23 |
repo_id = "NEXAS/low_light_enhance"
|
| 24 |
filename = "model.h5"
|
| 25 |
-
model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=HF_CACHE_DIR)
|
| 26 |
-
return keras.models.load_model(model_path, compile=False)
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
def enhance_image(image: Image.Image) -> Image.Image:
|
| 31 |
image_resized = image.resize((256, 256))
|
| 32 |
img_array = keras.utils.img_to_array(image_resized) / 255.0
|
|
@@ -35,6 +34,7 @@ def enhance_image(image: Image.Image) -> Image.Image:
|
|
| 35 |
output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
|
| 36 |
return Image.fromarray(output)
|
| 37 |
|
|
|
|
| 38 |
uploaded_file = st.file_uploader("π€ Upload a low-light image", type=["jpg", "jpeg", "png"])
|
| 39 |
|
| 40 |
if uploaded_file:
|
|
@@ -44,8 +44,10 @@ if uploaded_file:
|
|
| 44 |
if st.button("β¨ Enhance Image"):
|
| 45 |
with st.spinner("Enhancing image..."):
|
| 46 |
enhanced = enhance_image(image)
|
| 47 |
-
st.image(enhanced, caption="π Enhanced by NaxiLowLight", use_column_width=True)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
img_bytes = io.BytesIO()
|
| 50 |
enhanced.save(img_bytes, format="PNG")
|
| 51 |
st.download_button("πΎ Download Enhanced Image", data=img_bytes.getvalue(), file_name="enhanced.png", mime="image/png")
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
import streamlit as st
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow import keras
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
# Set Streamlit page
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
st.set_page_config(page_title="NaxiLowLight Enhancer", layout="centered")
|
| 13 |
st.title("π NaxiLowLight - Low-Light Image Enhancer")
|
| 14 |
|
| 15 |
+
# β
Load model using temporary directory (no caching to /.cache!)
|
| 16 |
@st.cache_resource
|
| 17 |
+
def load_model_from_huggingface_temp():
|
| 18 |
repo_id = "NEXAS/low_light_enhance"
|
| 19 |
filename = "model.h5"
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 22 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=temp_dir)
|
| 23 |
+
model = keras.models.load_model(model_path, compile=False)
|
| 24 |
+
return model
|
| 25 |
|
| 26 |
+
model = load_model_from_huggingface_temp()
|
| 27 |
+
|
| 28 |
+
# π§ Enhancement function
|
| 29 |
def enhance_image(image: Image.Image) -> Image.Image:
|
| 30 |
image_resized = image.resize((256, 256))
|
| 31 |
img_array = keras.utils.img_to_array(image_resized) / 255.0
|
|
|
|
| 34 |
output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
|
| 35 |
return Image.fromarray(output)
|
| 36 |
|
| 37 |
+
# π€ Upload & Enhance UI
|
| 38 |
uploaded_file = st.file_uploader("π€ Upload a low-light image", type=["jpg", "jpeg", "png"])
|
| 39 |
|
| 40 |
if uploaded_file:
|
|
|
|
| 44 |
if st.button("β¨ Enhance Image"):
|
| 45 |
with st.spinner("Enhancing image..."):
|
| 46 |
enhanced = enhance_image(image)
|
|
|
|
| 47 |
|
| 48 |
+
st.image(enhanced, caption="π Enhanced Image", use_column_width=True)
|
| 49 |
+
|
| 50 |
+
# Download
|
| 51 |
img_bytes = io.BytesIO()
|
| 52 |
enhanced.save(img_bytes, format="PNG")
|
| 53 |
st.download_button("πΎ Download Enhanced Image", data=img_bytes.getvalue(), file_name="enhanced.png", mime="image/png")
|