Skip to content

Commit

Permalink
Merge pull request #47 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Add docker file and flask example
  • Loading branch information
JE-Chen committed Nov 7, 2023
2 parents 5469e66 + 70b26f6 commit b127c8c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Dockerfiles/Flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM ubuntu:latest
LABEL authors="JE-Chen"
ARG DEBIAN_FRONTEND=noninteractive
# Copy whole project to image
COPY . /ReEdgeGPT_Flask
# You need put your bing_cookies.json to Dockerfiles/Flask dir
COPY Dockerfiles/Flask/bing_cookies.json /ReEdgeGPT_Flask
# Flask init
COPY Dockerfiles/Flask/main_flask.py /ReEdgeGPT_Flask
COPY Dockerfiles/Flask/re_edge_gpt_blueprint.py /ReEdgeGPT_Flask
# Workdir
WORKDIR /ReEdgeGPT_Flask
# Install dependency
RUN apt-get update && \
apt-get install -y python3.10 python3-pip python3.10-venv &&\
python3 -m venv venv &&\
. venv/bin/activate &&\
pip install -r docker_requirements.txt
# Os path
ENV PATH="/venv/bin:$PATH"
# Open port 8888
EXPOSE 8888
# Start Gunicorn and Flask server
ENTRYPOINT ["/bin/bash", "-c", "source venv/bin/activate && \
gunicorn --bind :8888 --workers=4 main_flask:app"]
42 changes: 42 additions & 0 deletions Dockerfiles/Flask/main_flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
from typing import List, Union

from flask import Flask, Blueprint

from Dockerfiles.Flask.re_edge_gpt_blueprint import re_edge_gpt_blueprint_instance


def create_app(blueprint_list: Union[List[Blueprint], None] = None) -> Flask:
flask_app = Flask(__name__)
if blueprint_list is not None:
for blueprint in blueprint_list:
flask_app.register_blueprint(blueprint)

return flask_app


if __name__ == "__main__":
# Init Flask with blueprint
blueprints = [re_edge_gpt_blueprint_instance]
app = create_app(blueprints)
# Create new secret key using urandom 24
app.secret_key = os.urandom(24)


@app.route("/", methods=["GET"])
async def index():
return "INDEX"


app.run(port=8888, debug=True)
else:
# Init Flask with blueprint
blueprints = [re_edge_gpt_blueprint_instance]
app = create_app(blueprints)
# Create new secret key using urandom 24
app.secret_key = os.urandom(24)


@app.route("/", methods=["GET"])
async def index():
return "INDEX"
28 changes: 28 additions & 0 deletions Dockerfiles/Flask/re_edge_gpt_blueprint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
from pathlib import Path

from flask import Blueprint, request

from re_edge_gpt import Chatbot, ConversationStyle

re_edge_gpt_blueprint_instance = Blueprint("re_edge_gpt_blueprint", __name__, url_prefix="/ReEdgeGPT")


@re_edge_gpt_blueprint_instance.route("/chat", methods=["POST"])
async def chat():
prompt = request.get_json()["prompt"]
bot = None
try:
cookies = json.loads(open(str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies)
response = await bot.ask(
prompt=prompt,
conversation_style=ConversationStyle.balanced,
simplify_response=True
)
return json.dumps(response, indent=2, ensure_ascii=False)
except Exception as error:
raise error
finally:
if bot is not None:
await bot.close()
13 changes: 13 additions & 0 deletions Dockerfiles/Flask/test_javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let httpRequest = new XMLHttpRequest();
httpRequest.open("POST", "/ReEdgeGPT/chat", true)
httpRequest.setRequestHeader("content-type", "application/json")
httpRequest.send(JSON.stringify({
"prompt": "Deer stew",
}))
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
console.log(httpRequest.response)
} else if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status !== 200) {
console.log(httpRequest.response)
}
}
10 changes: 10 additions & 0 deletions docker_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
aiohttp
certifi
httpx
prompt_toolkit
requests
rich
re_edge_gpt
flask[async]
regex
gunicorn
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ prompt_toolkit
requests
rich
re_edge_gpt
regex

0 comments on commit b127c8c

Please sign in to comment.