How to send imiages from system

user - show come papers on ML
bot - there are 1000 papers related to ML that i can provide
user- ok, show the distribution of the paper type
bot - creates an pie chart and send it to the user

example image created by bot -

stat

so basically I have to create an pie chart at runtime on the basis of user demand and send it to user. How can i send an image to user? I created a slot named image_location that holds the location of the image in my project folder and then I utter that image but all it does is show the image location filled in the slot image_location.

when and intend called show_Stat is detected then i called action_make_stats that create the graph and stores it’s location in the slot image_loc, and then I execute utter_show_Stats

image_loc: type: unfeaturized

utter_show_stats:

  • text: ‘stats: {image_loc}’

code snippet -

class ActionMakeStats(Action):

def name(self):
    return 'action_make_stats'

def run(self, dispatcher, tracker, domain):
    paper_domain = tracker.get_slot('paper_domain')

    domain_count = {}
    for domain in paper_domain:
        if domain in domain_count.keys():
            domain_count[domain] += 1
        else:
            domain_count[domain] = 1

    counts = list(domain_count.values()) 
    explode = [0] * len(counts)
    explode[counts.index(max(counts))] = 0.1

    plt.pie(
        x = counts,
        labels = list(domain_count.keys()),
        explode = explode,
        autopct='%.2f%%',
        shadow = True,
        startangle = 90
        )
    plt.savefig('stat.png')
    plt.close()

    return [SlotSet('image_loc', 'stat.png')]

instead of getting image from the bot i get a response “stat.png”

for now i testing it using rasa-x

If you testing using Rasa X UI, then it is capable of showing the image instead of text/image file name but the response needs to be in proper format. Below example demonstrates that:

For that the response is using below template:

templates:
  utter_greet:
  - text: "Hey! How are you?"
  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

The API Response is as below:

[
    {
        "recipient_id": "me",
        "text": "Here is something to cheer you up:"
    },
    {
        "recipient_id": "me",
        "image": "https://i.imgur.com/nGF1K8f.jpg"
    },
    {
        "recipient_id": "me",
        "text": "Did that help you?"
    }
]

Note the image instead of text in the response along with the url of the image.

So, you may want to check below:

  • When you are saving your plot (using plt.savefig('stat.png')), check where the file is getting stored on the server. Validate the URL by accessing that.
  • Send the URL along with tag that it is an image so that Rasa X UI can display that

Hey, can you tell me how to get the API response that you shown above

You can use any of the API testing tools like Postman. Send a post request in JSON format after passing Bearer Token for authorization:

You can read further in Rasa X HTTP API documentation:

can you give a code snippet for extracting the rasa response into a variable inside a python program.