Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Make a simple gradio app UI for the following image generation API: import requests…

  1. User

    Make a simple gradio app UI for the following image generation API:

    import requests

    Define the API endpoint and parameters

    api_url = "https://l-d83c.onrender.com/leonardo" payload = { "prompt": "Latina, Photogenic, Golden Hour Lighting", "nprompt": "", "steps": 44, "gd": 5,

    "width": 832,
    "height": 1216,
    "alchemy": True,
    "pr": True
    

    }

    Makes a POST request to the API

    try: headers = { "accept": "application/json", "content-type": "application/json" } response = requests.post(api_url, json=payload, headers=headers)

    # Check if we hit the rate limit
    if response.status_code == 429:
        print("You have reached your limit of 50 generation per hour, you will get 50 more image generations after an hour!!")
    
    # Parse the JSON response
    data = response.json()
    
    # Print result URL
    image_result = data.get("result")
    if image_result:
        print(f"Image URL: {image_result}")
    

    except Exception as e: print("An error occurred:", e)

  2. Assistant

    To create a Gradio app UI for the given image generation API, you can use the gradio Python library, which provides a simple way to create web UIs for Python functions. Below is an example of how to create a Gradio interface for the API:

    import requests
    import gradio as gr
    
    # Define the API function
    def generate_image(prompt, nprompt, steps, gd, width, height, alchemy, pr):
        api_url = "https://l-d83c.onrender.com/leonardo"
        payload = {
            "prompt": prompt,
            "nprompt": nprompt,
            "steps": steps,
            "gd": gd,
            "width": width,
            "height": height,
            "alchemy": alchemy,
            "pr": pr
        }
        headers = {
            "accept": "application/json",
            "content-type": "application/json"
        }
        response = requests.post(api_url, json=payload, headers=headers)
    
        if response.status_code == 429:
            return "You have reached your limit of 50 generation per hour, you will get 50 more image generations after an hour!!"
    
        data = response.json()
        image_result = data.get("result")
        if image_result:
            return image_result
        else:
            return "Failed to generate image"
    
    # Create a Gradio interface
    iface = gr.Interface(
        fn=generate_image,
        inputs=[
            gr.Textbox(label="Prompt", placeholder="Enter a description for the image"),
            gr.Textbox(label="Negative Prompt", placeholder="Enter a description to avoid in the image", optional=True),
            gr.Slider(label="Steps", min_value=1, max_value=100, default=44),
            gr.Slider(label="Guidance", min_value=1, max_value=10, default=5),
            gr.Slider(label="Width", min_value=128, max_value=2048, default=832),
            gr.Slider(label="Height", min_value=128, max_value=2048, default=1216),
            gr.Checkbox(label="Use Alchemy", default=True),
            gr.Checkbox(label="Use PR", default=True)
        ],
        outputs=[
            gr.Image(label="Generated Image")
        ],
        live=False
    )
    
    # Launch the app
    iface.launch()
    

    Install the gradio library, if you haven't already, using pip install gradio.

    This script creates a Gradio app with text inputs for the prompt and negative prompt, sliders for steps, guidance, width, and height, and checkboxes for the alchemy and pr options. The output is displayed as an image.

    To launch the Gradio app, you simply run the Python script, and it should open in your default web browser. You can also access the Gradio interface through the provided local URL in your terminal.

    Keep in mind that the server where the API is hosted should be able to handle POST requests coming from the Gradio app, and CORS (Cross-Origin Resource Sharing) should be properly configured to allow requests from the Gradio front-end.