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="Image Deblurring App", page_icon="🔍", layout="wide", ) st.title("Image Deblurring Application") st.markdown(""" Turn your blurry photos into clear, sharp images using AI technology. 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("Deblurring Options") # Additional options could be added here in the future st.markdown("This application uses a deep learning model to remove blur from images.") 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") 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_column_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 DeblurGAN - Image Restoration Project")