File size: 981 Bytes
0362b52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import magic
import io

def is_valid_image(file_content):
    """Validate if the uploaded file is an image."""
    mime = magic.Magic(mime=True)
    file_type = mime.from_buffer(file_content)
    return file_type.startswith('image/')

def render_uploader():
    """Render the image upload component with validation."""
    
    uploaded_file = st.file_uploader(
        "Upload Product Image (Optional)",
        type=["png", "jpg", "jpeg"],
        help="Upload a product image to enhance or modify"
    )
    
    if uploaded_file is not None:
        # Read file content
        file_content = uploaded_file.getvalue()
        
        # Validate image
        if not is_valid_image(file_content):
            st.error("Please upload a valid image file")
            return None
        
        # Preview image
        st.image(file_content, caption="Uploaded Image", use_column_width=True)
        
        return uploaded_file
        
    return None