# credit to kaido on tg # Import libraries import gradio as gr from rembg import remove from PIL import Image import io, requests # Background remover function with tempfile upload def remove_background(image: Image.Image) -> str: try: # Convert input image to bytes with io.BytesIO() as buffered: image.save(buffered, format="PNG", optimize=True) img_bytes = buffered.getvalue() # Process image output_bytes = remove( img_bytes, alpha_matting=True, alpha_matting_foreground_threshold=240, alpha_matting_background_threshold=10, alpha_matting_erode_size=10 ) # Upload to tmpfiles.org files = {"file": ("output.png", output_bytes, "image/png")} r = requests.post("https://tmpfiles.org/api/v1/upload", files=files) r.raise_for_status() data = r.json() # If upload succeeded, return the temporary URL if "data" in data and "url" in data["data"]: return data["data"]["url"] raise gr.Error("Upload failed. Please try again.") except Exception as e: print(f"Error: {str(e)}") raise gr.Error("Failed to process image.") # Example Images examples = [ ["https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Cat_August_2010-4.jpg/480px-Cat_August_2010-4.jpg"], ["https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/480px-Cat_November_2010-1a.jpg"], ["https://upload.wikimedia.org/wikipedia/commons/thumb/7/75/Internet1.jpg/640px-Internet1.jpg"] ] # Custom CSS for professional look custom_css = """ :root { --primary: #4f46e5; --secondary: #f9fafb; } body { font-family: 'Inter', sans-serif; } .gradio-container { max-width: 900px !important; margin: auto !important; } .dark { --primary: #6366f1; --secondary: #1e293b; } footer { display: none !important; } """ # Social Media & Credits socials = """
""" # Gradio Interface with gr.Blocks(title="AI ClearCut - Professional Background Remover", css=custom_css) as interface: gr.Markdown(""" #