import gradio as gr from PIL import Image import numpy as np import os import json import tempfile import replicate # type: ignore file_path = "comicidapi.json" # Define the replicate API token auth_token = os.environ.get("REPLICATE_API_TOKEN") print (auth_token) def load_workflow_from_file(file_path): with open(file_path, 'r') as file: return json.load(file) def process_image(image, style, character, prompt1): workflow = dict() pil_image = Image.fromarray((image)) prompt = f"Generate an image based on {style} style, graphic novel art, character as {character}, with high resolution, UHD, background of the image as {prompt1}" # Save the image to a temporary file with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: pil_image = Image.fromarray(image) pil_image.save(temp_file.name) print (temp_file.name) input_file = open(f"{temp_file.name}", "rb") workflow = load_workflow_from_file(file_path) workflow["6"]["inputs"]["text"] = f"{prompt}" #prompt # Define the API endpoind output = replicate.run( "fofr/any-comfyui-workflow:68ece9a0cd9b8de5708e012f64fa5deccd1ab7571a2e207cac0bd463b7107e79", input = { "workflow_json": json.dumps(workflow), "randomise_seeds": True, "return_temp_files": False, "input_file": input_file }, ) print(output) return(output[0]) iface = gr.Interface( fn=process_image, inputs=[ gr.Image(label = "Upload your Image"), gr.Dropdown(choices=["Graphic Illustration","Anime", "ComicStrip", "Disney", "Pixar 3D", "Pixel Art", "Gothic", "Chibi"," Oil Painting"], label="Style"), gr.Dropdown(choices=["SuperMan","Cinderella","Snow-White","Elsa Frozen", "Batman", "Ariel Mermaid", "Wonder Woman", "Iron-Man","Spider-Man","Captain America","Batgirl","Blackwidow","Hulk"], label="Character"), gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Describe the background of the image") ], outputs=[ gr.Image(label="Your transformed Image") ], title="Kartoonify Yourself", description="Select a style, character, and enter a prompt to process the image.\n In case of any error, try again with a better quality image where the face is clear and with better resolution", ) # Launch the Gradio app iface.launch()