NEXAS commited on
Commit
6c5474b
Β·
verified Β·
1 Parent(s): 98e80b2

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +61 -36
src/streamlit_app.py CHANGED
@@ -1,53 +1,78 @@
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
32
- img_array = np.expand_dims(img_array, axis=0)
33
- output = model.predict(img_array)[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:
 
 
 
41
  image = Image.open(uploaded_file).convert("RGB")
42
- st.image(image, caption="πŸ–ΌοΈ Original Image", use_column_width=True)
43
 
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")
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image, ImageOps
4
  import tensorflow as tf
5
  from tensorflow import keras
 
 
6
  import io
 
 
 
 
 
 
7
 
8
+ # ----------------------------
9
+ # Load the model
10
+ # ----------------------------
11
  @st.cache_resource
12
+ def load_model():
13
+ model = keras.models.load_model(
14
+ "src/naxi_lowlight.keras",
15
+ custom_objects={
16
+ "charbonnier_loss": lambda y_true, y_pred: tf.reduce_mean(tf.sqrt(tf.square(y_true - y_pred) + 1e-6)),
17
+ "psnr_metric": lambda y_true, y_pred: tf.image.psnr(y_pred, y_true, max_val=1.0)
18
+ }
19
+ )
20
  return model
21
 
22
+ model = load_model()
23
 
24
+ # ----------------------------
25
+ # Inference function
26
+ # ----------------------------
27
+ def enhance_image_pil(pil_img):
28
+ image = keras.utils.img_to_array(pil_img).astype("float32") / 255.0
29
+ image = np.expand_dims(image, axis=0)
30
+ output = model.predict(image)[0]
31
  output = np.clip(output * 255.0, 0, 255).astype(np.uint8)
32
  return Image.fromarray(output)
33
 
34
+ # ----------------------------
35
+ # Streamlit UI
36
+ # ----------------------------
37
+ st.set_page_config(page_title="NaxiLowLight Enhancement", layout="centered")
38
+ st.title("πŸŒ™ NaxiLowLight: Low-Light Image Enhancer")
39
+
40
+ st.write("Upload a low-light image to enhance it using a deep learning model.")
41
 
42
+ uploaded_file = st.file_uploader("Choose a low-light image", type=["jpg", "jpeg", "png"])
43
+
44
+ if uploaded_file is not None:
45
+ # Load image
46
  image = Image.open(uploaded_file).convert("RGB")
 
47
 
48
+ # Enhance
49
+ st.write("✨ Enhancing image...")
50
+ enhanced_image = enhance_image_pil(image)
51
+ autocontrast_image = ImageOps.autocontrast(image)
52
+
53
+ # Show results
54
+ st.write("πŸ“· **Comparison:**")
55
+ col1, col2, col3 = st.columns(3)
56
+
57
+ with col1:
58
+ st.image(image, caption="Original", use_column_width=True)
59
+
60
+ with col2:
61
+ st.image(autocontrast_image, caption="Autocontrast", use_column_width=True)
62
+
63
+ with col3:
64
+ st.image(enhanced_image, caption="NaxiLowLight Enhanced", use_column_width=True)
65
 
66
+ # Download
67
+ buf = io.BytesIO()
68
+ enhanced_image.save(buf, format="PNG")
69
+ byte_im = buf.getvalue()
70
 
71
+ st.download_button(
72
+ label="⬇️ Download Enhanced Image",
73
+ data=byte_im,
74
+ file_name="enhanced_image.png",
75
+ mime="image/png"
76
+ )
77
+ else:
78
+ st.info("Upload a PNG or JPG image to get started.")