-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
using_flask.py
68 lines (50 loc) · 1.83 KB
/
using_flask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Example of using displaying PandasAI charts in Flask
Usage:
flask –-app using_flask.py run
"""
import os
import pandas as pd
from flask import Flask, render_template, request
from pandasai import SmartDatalake
from pandasai.responses.response_parser import ResponseParser
app = Flask(__name__)
# This class overrides default behaviour how dataframe is returned
# By Default PandasAI returns the SmartDataFrame
class PandasDataFrame(ResponseParser):
def __init__(self, context) -> None:
super().__init__(context)
def format_dataframe(self, result):
# Returns Pandas Dataframe instead of SmartDataFrame
return result["value"]
employees_df = pd.DataFrame(
{
"EmployeeID": [1, 2, 3, 4, 5],
"Name": ["John", "Emma", "Liam", "Olivia", "William"],
"Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}
)
salaries_df = pd.DataFrame(
{
"EmployeeID": [1, 2, 3, 4, 5],
"Salary": [5000, 6000, 4500, 7000, 5500],
}
)
# By default, unless you choose a different LLM, it will use BambooLLM.
# You can get your free API key signing up at https://pandabi.ai (you can also configure it in your .env file)
os.environ["PANDASAI_API_KEY"] = "your-api-key"
agent = SmartDatalake(
[employees_df, salaries_df],
config={"verbose": True, "response_parser": PandasDataFrame},
)
@app.route("/pandasai", methods=["GET", "POST"])
def pandasai():
if request.method == "POST":
# prompt question such as "Return a dataframe of name against salaries"
query = request.form["query"]
response = agent.chat(query)
# Returns the response as Pandas DataFrame object in html
return render_template("sample_flask_salaries.html", response=response)
return render_template("sample_flask_salaries.html")
if __name__ == "__main__":
app.run()