import gradio as gr import requests import io from PIL import Image import json from image_processing import downscale_image, limit_colors, convert_to_grayscale, convert_to_black_and_white import logging class SomeClass: def __init__(self): self.images = [] with open('loras.json', 'r') as f: loras = json.load(f) def update_selection(selected_state: gr.SelectData): logging.debug(f"Inside update_selection, selected_state: {selected_state}") selected_lora_index = selected_state.index selected_lora = loras[selected_lora_index] new_placeholder = f"Type a prompt for {selected_lora['title']}" lora_repo = selected_lora["repo"] updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨" return ( gr.update(placeholder=new_placeholder), updated_text, selected_state ) def run_lora(prompt, selected_state, progress=gr.Progress(track_tqdm=True)): logging.debug(f"Inside run_lora, selected_state: {selected_state}") if not selected_state: logging.error("selected_state is None or empty.") raise gr.Error("You must select a LoRA before proceeding.") # Popup error when no LoRA is selected selected_lora_index = selected_state.index # Changed this line selected_lora = loras[selected_lora_index] api_url = f"https://api-inference.huggingface.co/models/{selected_lora['repo']}" trigger_word = selected_lora["trigger_word"] #token = os.getenv("API_TOKEN") payload = { "inputs": f"{prompt} {trigger_word}", "parameters":{"negative_prompt": "bad art, ugly, watermark, deformed"}, } #headers = {"Authorization": f"Bearer {token}"} # Add a print statement to display the API request print(f"API Request: {api_url}") #print(f"API Headers: {headers}") print(f"API Payload: {payload}") error_count = 0 pbar = tqdm(total=None, desc="Loading model") while(True): response = requests.post(api_url, json=payload) if response.status_code == 200: return Image.open(io.BytesIO(response.content)) elif response.status_code == 503: #503 is triggered when the model is doing cold boot. It also gives you a time estimate from when the model is loaded but it is not super precise time.sleep(1) pbar.update(1) elif response.status_code == 500 and error_count < 5: print(response.content) time.sleep(1) error_count += 1 continue else: logging.error(f"API Error: {response.status_code}") raise gr.Error("API Error: Unable to fetch the image.") # Raise a Gradio error here def apply_post_processing(image, downscale, limit_colors, grayscale, black_and_white): processed_image = image.copy() if downscale > 1: processed_image = downscale_image(processed_image, downscale) if limit_colors: processed_image = limit_colors(processed_image) if grayscale: processed_image = convert_to_grayscale(processed_image) if black_and_white: processed_image = convert_to_black_and_white(processed_image) return processed_image with gr.Blocks() as app: title = gr.Markdown("# artificialguybr LoRA portfolio") description = gr.Markdown("### This is a Pixel Art Generator using SD Loras.") selected_state = gr.State() with gr.Row(): gallery = gr.Gallery([(item["image"], item["title"]) for item in loras], label="LoRA Gallery", allow_preview=False, columns=1) with gr.Column(): prompt_title = gr.Markdown("### Click on a LoRA in the gallery to create with it") selected_info = gr.Markdown("") with gr.Row(): prompt = gr.Textbox(label="Prompt", show_label=False, lines=1, max_lines=1, placeholder="Type a prompt after selecting a LoRA") button = gr.Button("Run") result = gr.Image(interactive=False, label="Generated Image") post_processed_result = gr.Image(interactive=False, label="Post-Processed Image") # Accordion moved here, inside the same gr.Blocks context with gr.Accordion(label="Pixel art", open=True): with gr.Row(): enabled = gr.Checkbox(label="Enable", value=False) downscale = gr.Slider(label="Downscale", minimum=1, maximum=32, step=2, value=8) need_rescale = gr.Checkbox(label="Rescale to original size", value=True) with gr.Tabs(): with gr.TabItem("Color"): enable_color_limit = gr.Checkbox(label="Enable", value=False) palette_size_color = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16) quantization_methods_color = gr.Radio(choices=["Median Cut", "Maximum Coverage", "Fast Octree"], label="Colors Quantization Method", value="Median Cut") dither_methods_color = gr.Radio(choices=["None", "Floyd-Steinberg"], label="Colors Dither Method", value="None") k_means_color = gr.Checkbox(label="Enable k-means for color quantization", value=True) with gr.TabItem("Grayscale"): enable_grayscale = gr.Checkbox(label="Enable", value=False) palette_size_gray = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16) quantization_methods_gray = gr.Radio(choices=["Median Cut", "Maximum Coverage", "Fast Octree"], label="Colors Quantization Method", value="Median Cut") dither_methods_gray = gr.Radio(choices=["None", "Floyd-Steinberg"], label="Colors Dither Method", value="None") k_means_gray = gr.Checkbox(label="Enable k-means for color quantization", value=True) with gr.TabItem("Black and white"): enable_black_and_white = gr.Checkbox(label="Enable", value=False) inverse_black_and_white = gr.Checkbox(label="Inverse", value=False) threshold_black_and_white = gr.Slider(label="Threshold", minimum=1, maximum=256, step=1, value=128) with gr.TabItem("Custom color palette"): enable_custom_palette = gr.Checkbox(label="Enable", value=False) palette_image = gr.Image(label="Color palette image", type="pil") palette_size_custom = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16) dither_methods_custom = gr.Radio(choices=["None", "Floyd-Steinberg"], label="Colors Dither Method", value="None") post_process_button = gr.Button("Apply Post-Processing") # The rest of your code for setting up the app gallery.select(update_selection, outputs=[prompt, selected_info, selected_state]) prompt.submit(fn=run_lora, inputs=[prompt, selected_state], outputs=[result, post_processed_result]) post_process_button.click(fn=apply_post_processing, inputs=[post_processed_result, downscale, enable_color_limit, enable_grayscale, enable_black_and_white], outputs=[post_processed_result]) app.queue(max_size=20, concurrency_count=5) app.launch()