-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add Flask openai example (#941)
* 'Refactored by Sourcery' * chore: add Flask html example * chore: add Flask openai example * sourcery refactor integration tests * fix: Flask package install set to optional --------- Co-authored-by: Sourcery AI <>
- Loading branch information
Showing
13 changed files
with
252 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{% block content %} | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Pandas AI</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> | ||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script> | ||
</head> | ||
<body> | ||
|
||
<div class="container"> | ||
<h2>Pandas AI</h2> | ||
<br> | ||
<form class="form-horizontal" action="/pandasai" method="POST" enctype="multipart/form-data"> | ||
<div class="form-group"> | ||
<label class="control-label col-sm-2" for="query">Input:</label> | ||
<div class="col-sm-10"> | ||
<input type="text" class="form-control" id="query" placeholder="Enter Query" name="query" required> | ||
</div> | ||
</div> | ||
<br> | ||
<div class="form-group"> | ||
<div class="col-sm-offset-2 col-sm-10"> | ||
<button type="submit" value="Submit" class="btn btn-default">Submit</button> | ||
</div> | ||
</div> | ||
</form> | ||
{% if response %} | ||
<div class="card border-info mb-3" style="max-width: 18rem;"> | ||
<div class="card-header"> | ||
Pandas AI Discovery | ||
</div> | ||
<div class="card-body text-info"> | ||
<p class="card-text">{{ response }}</p> | ||
</div> | ||
</div> | ||
{% endif %} | ||
</div> | ||
|
||
</body> | ||
</html> | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Example of using displaying PandasAI charts in Flask | ||
Usage: | ||
flask –-app using_flask.py run | ||
""" | ||
import pandas as pd | ||
from flask import Flask, render_template, request | ||
|
||
from pandasai import SmartDatalake | ||
from pandasai.llm import OpenAI | ||
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], | ||
} | ||
) | ||
|
||
llm = OpenAI("OPENAI-KEY") | ||
dl = SmartDatalake( | ||
[employees_df, salaries_df], | ||
config={"llm": llm, "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 = dl.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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.