|
|
import streamlit as st |
|
|
import requests |
|
|
import os |
|
|
import sys |
|
|
from PIL import Image |
|
|
import io |
|
|
import time |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
API_URL = "http://localhost:8001" |
|
|
|
|
|
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! |
|
|
""") |
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader( |
|
|
"Choose a blurry image...", type=["jpg", "jpeg", "png", "bmp"]) |
|
|
|
|
|
|
|
|
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("---") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
if uploaded_file is not None: |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
with col1: |
|
|
st.subheader("Original Image") |
|
|
image = Image.open(uploaded_file) |
|
|
st.image(image, use_container_width=True) |
|
|
|
|
|
|
|
|
process_button = st.button("Deblur Image") |
|
|
|
|
|
if process_button: |
|
|
with st.spinner("Deblurring your image... Please wait."): |
|
|
try: |
|
|
|
|
|
files = { |
|
|
"file": ("image.jpg", uploaded_file.getvalue(), "image/jpeg") |
|
|
} |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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)}") |
|
|
|
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown("Powered by NAFNet - Image Restoration Project") |
|
|
|
|
|
def main(): |
|
|
pass |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |