import requests import gradio as gr # Set your Pixelcut API key directly API_KEY = "sk_7c19e4f2ad434a3ebc70fdd85ade5309" # Function to remove background from a local image file def remove_bg(image_path): try: with open(image_path, "rb") as image_file: response = requests.post( "https://api.developer.pixelcut.ai/v1/remove-background", headers={ "Accept": "application/json", "X-API-KEY": API_KEY, }, files={"image": image_file}, data={"format": "png"}, ) # Handle API response if response.status_code == 200: result = response.json() return result["output_url"] # Return the output image URL from API else: return f"Error: {response.status_code} - {response.json()['error']}" except Exception as e: return f"Exception: {e}" # Define the Tryonn function without CSS def tryonn(person_image, garment_image): # Process the images using the Pixelcut API person_result = remove_bg(person_image) garment_result = remove_bg(garment_image) # If both images are processed successfully, combine them (mock-up display) if "Error" not in person_result and "Error" not in garment_result: return person_result, garment_result, "✅ Success!" else: return person_result, garment_result, "❌ Failed, check errors." # Gradio interface setup (CSS removed) with gr.Blocks() as Tryon: gr.Markdown("## Upload a Person Image and Garment Image") with gr.Row(): person_input = gr.Image(type="filepath", label="Person Image") garment_input = gr.Image(type="filepath", label="Garment Image") output_person = gr.Image(label="Processed Person Image") output_garment = gr.Image(label="Processed Garment Image") status = gr.Textbox(label="Status") submit_button = gr.Button("Run Try-On") submit_button.click( fn=tryonn, inputs=[person_input, garment_input], outputs=[output_person, output_garment, status], ) Tryon.launch() example_path = os.path.join(os.path.dirname(__file__), 'assets') garm_list = os.listdir(os.path.join(example_path,"cloth")) garm_list_path = [os.path.join(example_path,"cloth",garm) for garm in garm_list] human_list = os.listdir(os.path.join(example_path,"human")) human_list_path = [os.path.join(example_path,"human",human) for human in human_list] css=""" #col-left { margin: 0 auto; max-width: 430px; } #col-mid { margin: 0 auto; max-width: 430px; } #col-right { margin: 0 auto; max-width: 430px; } #col-showcase { margin: 0 auto; max-width: 1100px; } #button { color: blue; } """ def load_description(fp): with open(fp, 'r', encoding='utf-8') as f: content = f.read() return content def change_imgs(image1, image2): return image1, image2 with gr.Blocks(css=css) as Tryon: gr.HTML(load_description("assets/new_title.md")) with gr.Row(): with gr.Column(elem_id = "col-left"): gr.HTML("""