Capture image response from pandasai as flask api response

I am trying to leverage pandasai for generating chart based on input provided with OpenAI as the LLM. And then expose the generated image response using flask API.

from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
import pandas as pd
import os
from flask import send_file, request, jsonify
from flask import Flask
import numpy as np #The Python numerical algebra package
import pandas as pd #For working with data frames
import io as io
import matplotlib.pyplot as plt

app = Flask(__name__)

port = int(os.environ.get('PORT', 3000))

def getChart():
    #Accessing the language model of OpenAI through key
    llm = OpenAI(api_token='<<<<<my_api_token>>>>')
    #Passing the language model to pandasai
    pandas_ai = PandasAI(llm)
    df1 = pd.DataFrame({
    "country": ["United States", "United Kingdom", "France", "Germany", "Italy", "Spain", "Canada", "Australia", "Japan", "China"],
    "gdp": [21400000, 2940000, 2830000, 3870000, 2160000, 1350000, 1780000, 1320000, 516000, 14000000],
    "happiness_index": [7.3, 7.2, 6.5, 7.0, 6.0, 6.3, 7.3, 7.3, 5.9, 5.0]
    # Enter Prompt related to data or Select from Pre-defined for demo purposes.
    prompt = 'Plot the piechart of countries showing for each the gpd, using different colors for each bar'
    response =, prompt=prompt,

    # bytes_image = io.BytesIO()
    # plt.savefig(bytes_image, format='png')
    # return send_file(bytes_image, mimetype='image/jpeg')
    return send_file(response, mimetype='image/jpeg')

if __name__ == '__main__':'', port=port)

Here, pops up an image when the api url localhost:3000/gdpChart is invoked. However, I am unable to set this image as a response to the API.

My query is: How would I set the image response (from pandasai) to flaskapi response so that image would be displayed in the browser as response…