Created
February 12, 2021 12:27
-
-
Save cosmicle0/453d38dcbd50535c76d0ddca68157281 to your computer and use it in GitHub Desktop.
Flask Chatterbot API (https://ai.bongo.ninja/)
This file contains hidden or 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
import os | |
from flask import Flask, request, abort, render_template | |
from chatterbot import ChatBot | |
from chatterbot.trainers import ListTrainer | |
from chatterbot.trainers import ChatterBotCorpusTrainer | |
from hurry.filesize import size | |
import psycopg2 as pg | |
app = Flask(__name__, template_folder='.') | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0") | |
conn = pg.connect(user="USER", password="PASSWORD", host="HOST", port="PORT", database="DATABASE") | |
cur = conn.cursor() | |
bot = ChatBot( | |
'NAME', | |
storage_adapter='chatterbot.storage.SQLStorageAdapter', | |
logic_adapters=[ | |
'chatterbot.logic.MathematicalEvaluation', | |
'chatterbot.logic.BestMatch' | |
], | |
database_uri='postgresql://USER:PASSWORD@HOST:PORT/DATABASE' | |
) | |
@app.route('/') | |
def index(): | |
cur.execute("select pg_size_pretty(pg_database_size('aidb'));") | |
aidbsize = cur.fetchone()[0] | |
return render_template('index.html', db_size=aidbsize) | |
@app.route('/api/ai', methods=['GET']) | |
def api(): | |
try: | |
input = request.args.get('msg') | |
out = bot.get_response(input) | |
return str(out) | |
except: | |
abort(400) |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>NAME</title> | |
</head> | |
<body> | |
<style> | |
body { | |
font-family: Tahoma, Verdana, Arial, sans-serif; | |
} | |
</style> | |
<h1>NAME</h1> | |
<hr> | |
<p>An API for chatterbot made using Flask.</p> | |
<div> | |
<h2>API</h2> | |
<p><b>Request URL: </b>http://HOST/api/ai</p> | |
<p><b>Request Method: </b>GET</p> | |
<div><p><b>Required Query Parameters:</b></p><pre>msg: String. Message to the Bot.</pre></div> | |
<div><h4>Example:</h4> | |
<pre><b>GET</b> http://HOST/api/ai?msg=hello</pre> | |
</div> | |
</div> | |
<br> | |
<p>Database Size: <b>{{ db_size }}</b></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment