Unable to plot image in rasa-x

Hi,

I want to simply create a plot using actions.py

I am getting below error:

Exception occurred while handling uri: 'http://app:5055/webhook'
Traceback (most recent call last):
  File "/opt/venv/lib/python3.8/site-packages/sanic/app.py", line 931, in handle_request
    response = await response
  File "/opt/venv/lib/python3.8/site-packages/rasa_sdk/endpoint.py", line 104, in webhook
    result = await executor.run(action_call)
  File "/opt/venv/lib/python3.8/site-packages/rasa_sdk/executor.py", line 398, in run
    action(dispatcher, tracker, domain)
  File "/app/actions/actions.py", line 83, in run
    fig.savefig("./actions/neww.png")
  File "/opt/venv/lib/python3.8/site-packages/matplotlib/figure.py", line 2311, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 2210, in print_figure
    result = print_method(
  File "/opt/venv/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1639, in wrapper
    return func(*args, **kwargs)
  File "/opt/venv/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 510, in print_png
    mpl.image.imsave(
  File "/opt/venv/lib/python3.8/site-packages/matplotlib/image.py", line 1611, in imsave
    image.save(fname, **pil_kwargs)
  File "/opt/venv/lib/python3.8/site-packages/PIL/Image.py", line 2237, in save
    fp = builtins.open(filename, "w+b")
PermissionError: [Errno 13] Permission denied: './actions/neww.png'

Here is my actions.py file:

class ActionForecastRisk(Action):

    def name(self) -> Text:
        return "action_forecast_xyz"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        import seaborn as sns
        import matplotlib.pyplot as plt

        l = [41, 44, 46, 46, 47, 47, 48, 48, 49, 51, 52, 53, 53, 53, 53, 55, 55, 55,
            55, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 58, 58, 58,
            58, 59, 59, 59, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 61,
            61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 62, 62, 62, 62, 62, 62, 62, 62,
            62, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 64, 64, 65,
            65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 66, 66,
            67, 67, 67, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68, 69, 69, 69, 70, 70,
            70, 70, 71, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73,
            74, 74, 74, 74, 74, 75, 75, 75, 76, 77, 77, 78, 78, 79, 79, 79, 79, 80,
            80, 80, 80, 81, 81, 81, 81, 83, 84, 84, 85, 86, 86, 86, 86, 87, 87, 87,
            87, 87, 88, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 91, 91, 92,
            92, 93, 93, 93, 94, 95, 95, 96, 98, 98, 99, 100, 102, 104, 105, 107, 108,
            109, 110, 110, 113, 113, 115, 116, 118, 119, 121]

        plt = sns.distplot(l, kde=True, rug=False)
        fig = plt.get_figure()
        fig.savefig("./actions/neww.png")


        msg = "hiiiii"
        dispatcher.utter_message(text=msg, image = "./actions/neww.png")
        return []

@ChrisRahme Any thoughts?

@nik202 Any thoughts?

You don’t have permissions to the file.

Try creating an independent Python script which does that, do you have the same error?

Also try fig.savefig("neww.png")

What’s your OS? Rasa Version?

1 Like

@ChrisRahme I am unable to save file basically. I am doing that in actions.py as mentioned above. Is there any other method to generate plots/graphs and show?

I have deployed this using docker-compose on ubuntu machine and Rasa version is: 2.8.11 image

@Sajjadmanal are you using docker image? please share the code if you are? or some related file for the same.

@Sajjadmanal as expected :slight_smile: I have solution.

Okay please show me your Dockerfile or Docker Compose

This is docker-compose.override.yml file image

@Sajjadmanal mention user root in docker-compose.override.yml and it will sort your issue.

user: root

2 Likes

This is dockerfile:

@Sajjadmanal Trust me it will work, do spin the docker again.

Note: If there is any other error still action_server will show restarting, but this permission issue only come with user root rights.

1 Like

Trust Nik!

@Sajjadmanal and Chris too :slight_smile:

1 Like

Yes, that error is not there now. Thanks. But now, I am unable to see image. Here is the response on rasa-x:

image

@Sajjadmanal I have given the solution for the error issue " Permission denied" .

Some pointers for you:

Try first run the code in local mode (local machine) , install all the packages and libraries, while using requirements.txt and then try using the code in a server side. Good Luck!

Always mention me with @ and nik202.

@nik202 Thanks for the solution. At local too, the image gets stored properly and is viewable if I open the image. However, If I call it via rasa-x in the chat box, I get the same issue as above. Have raised a new issue for this one: Check here.