File size: 3,714 Bytes
61d360d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import streamlit as st
import requests
import os
import sys
from PIL import Image
import io
import time
from pathlib import Path

# Set API URL
API_URL = "http://localhost:8001"  # Local FastAPI server URL

st.set_page_config(
    page_title="NAFNet Image Deblurring",
    page_icon="🔍",
    layout="wide",
)

st.title("NAFNet Image Deblurring Application")
st.markdown("""
Transform your blurry photos into clear, sharp images using the state-of-the-art NAFNet AI model.
Upload an image to get started!
""")

# File uploader
uploaded_file = st.file_uploader(
    "Choose a blurry image...", type=["jpg", "jpeg", "png", "bmp"])

# Sidebar controls
with st.sidebar:
    st.header("About NAFNet")
    
    st.markdown("""
    **NAFNet** (Nonlinear Activation Free Network) is a state-of-the-art image restoration model designed for tasks like deblurring.
    
    Key features:
    - High-quality image deblurring
    - Fast processing time
    - Preservation of image details
    """)
    
    st.markdown("---")
    
    # Check API status
    if st.button("Check API Status"):
        try:
            response = requests.get(f"{API_URL}/status/", timeout=5)
            if response.status_code == 200 and response.json().get("status") == "ok":
                st.success("✅ API is running and ready")
                
                # Display additional info if available
                memory_info = response.json().get("memory", {})
                if memory_info:
                    st.info(f"CUDA Memory: {memory_info.get('cuda_memory_allocated', 'N/A')}")
            else:
                st.error("❌ API is not responding properly")
        except:
            st.error("❌ Cannot connect to API")

# Process when upload is ready
if uploaded_file is not None:
    # Display the original image
    col1, col2 = st.columns(2)

    with col1:
        st.subheader("Original Image")
        image = Image.open(uploaded_file)
        st.image(image, use_container_width=True)

    # Process image button
    process_button = st.button("Deblur Image")

    if process_button:
        with st.spinner("Deblurring your image... Please wait."):
            try:
                # Prepare simplified file structure
                files = {
                    "file": ("image.jpg", uploaded_file.getvalue(), "image/jpeg")
                }
                
                # Send request to API
                response = requests.post(f"{API_URL}/deblur/", files=files, timeout=60)

                if response.status_code == 200:
                    with col2:
                        st.subheader("Deblurred Result")
                        deblurred_img = Image.open(io.BytesIO(response.content))
                        st.image(deblurred_img, use_column_width=True)

                    # Option to download the deblurred image
                    st.download_button(
                        label="Download Deblurred Image",
                        data=response.content,
                        file_name=f"deblurred_{uploaded_file.name}",
                        mime="image/png"
                    )
                else:
                    try:
                        error_details = response.json().get('detail', 'Unknown error')
                    except:
                        error_details = response.text
                    
                    st.error(f"Error: {error_details}")
            except Exception as e:
                st.error(f"An error occurred: {str(e)}")

# Footer
st.markdown("---")
st.markdown("Powered by NAFNet - Image Restoration Project")

def main():
    pass  # Streamlit already runs the script from top to bottom

if __name__ == "__main__":
    main()