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

[BUG] Images not displaying with GradioUI even when passed as the final answer #662

Open
jsmidt opened this issue Feb 16, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@jsmidt
Copy link
Contributor

jsmidt commented Feb 16, 2025

The code in gradio_ui.py code suggests that if a png filename is passed as the final answer it will display the image.

elif isinstance(final_answer, AgentImage):
    yield gr.ChatMessage(
        role="assistant",
        content={"path": final_answer.to_string(), "mime_type": "image/png"},
    )

I passed this prompt: "Plot sin(x) from 0 to 1. Then save the plot as my_plot.png. Then give my_plot.png for the final answer so that the image displays." I used a code agent with GradioUI code:

from smolagents import CodeAgent, GradioUI, HfApiModel, LiteLLMModel

AUTHORIZED_IMPORTS = [
    "numpy",
    "matplotlib",
    "seaborn",
]

agent = CodeAgent(tools=[], model=HfApiModel(), max_steps=20, verbosity_level=2,additional_authorized_imports=AUTHORIZED_IMPORTS)

GradioUI(agent).launch()

See the screenshot below. The agent submitted my_plot.png as the final answer. But as the screenshot shows it did not display the png. Thanks.

Image

@jsmidt jsmidt added the bug Something isn't working label Feb 16, 2025
@keetrap
Copy link
Contributor

keetrap commented Feb 17, 2025

@jsmidt
Try using:
AUTHORIZED_IMPORTS = [
"numpy",
"matplotlib",
"PIL",
"io"
]
"Plot a sine wave from 0 to 2pi and return it as Image"

@jsmidt
Copy link
Contributor Author

jsmidt commented Feb 20, 2025

That worked! Looks like it was just a matter of having those extra authorized imports. Thank you for this. Looks like not a bug, unless displaying plots is expected to be supported by default, in which case perhaps these imports should be authorized by default.

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