Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using loaded interface as a function in Blocks return error #2369

Closed
1 task done
fffiloni opened this issue Sep 29, 2022 · 5 comments
Closed
1 task done

Using loaded interface as a function in Blocks return error #2369

fffiloni opened this issue Sep 29, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@fffiloni
Copy link

Describe the bug

I want to use Stable Diffusion as a loaded interface in a Block, and use it as a function run by a button.
Calling that function leads to a AttributeError: 'Dataset' object has no attribute 'serialize'error in logs

Is there an existing issue for this?

  • I have searched the existing issues

Reproduction

import gradio as gr

stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")

def get_images(prompt):
    images = stable_diffusion(prompt, fn_index=0)
    return images

with gr.Blocks() as demo:
    
    with gr.Column():
        with gr.Row():
            prompt = gr.Text()
            gallery = gr.Gallery()
        submit_btn = gr.Button()
        submit_btn.click(get_images, inputs=[prompt], outputs=[gallery])
demo.launch()

Screenshot

No response

Logs

Fetching interface from: https://huggingface.co/spaces/stabilityai/stable-diffusion
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/gradio/routes.py", line 273, in run_predict
    output = await app.blocks.process_api(
  File "/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py", line 746, in process_api
    result = await self.call_function(fn_index, inputs, iterator)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py", line 657, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/home/user/.local/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/user/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "app.py", line 6, in get_images
    images = stable_diffusion(prompt, fn_index=0)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py", line 561, in __call__
    serialized_input = block.serialize(params[i])
AttributeError: 'Dataset' object has no attribute 'serialize'


### System Info

```shell
Gradio version: 3.4
Running on Huggingface

Severity

annoying

@fffiloni fffiloni added the bug Something isn't working label Sep 29, 2022
@freddyaboulton
Copy link
Collaborator

Thanks for filing @fffiloni !

My guess is that fn_index=0 probably corresponds to the examples component and not the actual text to image generation. I would check with fn_index=2.

@fffiloni
Copy link
Author

Hello @freddyaboulton !

I tried with fn_index=2 and fn_index=1 but get another error:

Fetching interface from: https://huggingface.co/spaces/stabilityai/stable-diffusion
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/gradio/routes.py", line 275, in run_predict
    output = await app.blocks.process_api(
  File "/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py", line 764, in process_api
    predictions = self.postprocess_data(fn_index, result["prediction"], state)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py", line 731, in postprocess_data
    output_value = block.postprocess(prediction_value)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/components.py", line 3297, in postprocess
    img = processing_utils.encode_url_or_file_to_base64(img)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/processing_utils.py", line 37, in encode_url_or_file_to_base64
    return encode_file_to_base64(path, encryption_key=encryption_key)
  File "/home/user/.local/lib/python3.8/site-packages/gradio/processing_utils.py", line 59, in encode_file_to_base64
    with open(f, "rb") as file:
IsADirectoryError: [Errno 21] Is a directory: '/home/user/app/1422c498-85b2-487e-b5f5-cd5fe3974606'

@freddyaboulton freddyaboulton self-assigned this Sep 30, 2022
@freddyaboulton
Copy link
Collaborator

Thanks for the info @fffiloni ! Will look into this

@freddyaboulton
Copy link
Collaborator

Hi @fffiloni ! I think I understand what's happening - the stable diffusion space is returning a path to a gallery. The images in the gallery are stored as files in that path. If you modify your function to return a list of the image files instead it works

import gradio as gr
import os

stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")

def get_images(prompt):
    gallery_dir = stable_diffusion(prompt, fn_index=2)
    return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)]

with gr.Blocks() as demo:
    
    with gr.Column():
        with gr.Row():
            prompt = gr.Text()
            gallery = gr.Gallery()
        submit_btn = gr.Button()
        submit_btn.click(get_images, inputs=[prompt], outputs=[gallery])
demo.launch()

stable_diffusion_fn

@fffiloni
Copy link
Author

fffiloni commented Oct 5, 2022

Ouh nice @freddyaboulton ! Yes it works !
Thank you very much for your time investigating ;)

@fffiloni fffiloni closed this as completed Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants