Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit b127c8c

Browse files
authored
Merge pull request #47 from Integration-Automation/dev
Add docker file and flask example
2 parents 5469e66 + 70b26f6 commit b127c8c

File tree

6 files changed

+119
-0
lines changed

6 files changed

+119
-0
lines changed

Dockerfiles/Flask/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM ubuntu:latest
2+
LABEL authors="JE-Chen"
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
# Copy whole project to image
5+
COPY . /ReEdgeGPT_Flask
6+
# You need put your bing_cookies.json to Dockerfiles/Flask dir
7+
COPY Dockerfiles/Flask/bing_cookies.json /ReEdgeGPT_Flask
8+
# Flask init
9+
COPY Dockerfiles/Flask/main_flask.py /ReEdgeGPT_Flask
10+
COPY Dockerfiles/Flask/re_edge_gpt_blueprint.py /ReEdgeGPT_Flask
11+
# Workdir
12+
WORKDIR /ReEdgeGPT_Flask
13+
# Install dependency
14+
RUN apt-get update && \
15+
apt-get install -y python3.10 python3-pip python3.10-venv &&\
16+
python3 -m venv venv &&\
17+
. venv/bin/activate &&\
18+
pip install -r docker_requirements.txt
19+
# Os path
20+
ENV PATH="/venv/bin:$PATH"
21+
# Open port 8888
22+
EXPOSE 8888
23+
# Start Gunicorn and Flask server
24+
ENTRYPOINT ["/bin/bash", "-c", "source venv/bin/activate && \
25+
gunicorn --bind :8888 --workers=4 main_flask:app"]

Dockerfiles/Flask/main_flask.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
from typing import List, Union
3+
4+
from flask import Flask, Blueprint
5+
6+
from Dockerfiles.Flask.re_edge_gpt_blueprint import re_edge_gpt_blueprint_instance
7+
8+
9+
def create_app(blueprint_list: Union[List[Blueprint], None] = None) -> Flask:
10+
flask_app = Flask(__name__)
11+
if blueprint_list is not None:
12+
for blueprint in blueprint_list:
13+
flask_app.register_blueprint(blueprint)
14+
15+
return flask_app
16+
17+
18+
if __name__ == "__main__":
19+
# Init Flask with blueprint
20+
blueprints = [re_edge_gpt_blueprint_instance]
21+
app = create_app(blueprints)
22+
# Create new secret key using urandom 24
23+
app.secret_key = os.urandom(24)
24+
25+
26+
@app.route("/", methods=["GET"])
27+
async def index():
28+
return "INDEX"
29+
30+
31+
app.run(port=8888, debug=True)
32+
else:
33+
# Init Flask with blueprint
34+
blueprints = [re_edge_gpt_blueprint_instance]
35+
app = create_app(blueprints)
36+
# Create new secret key using urandom 24
37+
app.secret_key = os.urandom(24)
38+
39+
40+
@app.route("/", methods=["GET"])
41+
async def index():
42+
return "INDEX"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
from pathlib import Path
3+
4+
from flask import Blueprint, request
5+
6+
from re_edge_gpt import Chatbot, ConversationStyle
7+
8+
re_edge_gpt_blueprint_instance = Blueprint("re_edge_gpt_blueprint", __name__, url_prefix="/ReEdgeGPT")
9+
10+
11+
@re_edge_gpt_blueprint_instance.route("/chat", methods=["POST"])
12+
async def chat():
13+
prompt = request.get_json()["prompt"]
14+
bot = None
15+
try:
16+
cookies = json.loads(open(str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
17+
bot = await Chatbot.create(cookies=cookies)
18+
response = await bot.ask(
19+
prompt=prompt,
20+
conversation_style=ConversationStyle.balanced,
21+
simplify_response=True
22+
)
23+
return json.dumps(response, indent=2, ensure_ascii=False)
24+
except Exception as error:
25+
raise error
26+
finally:
27+
if bot is not None:
28+
await bot.close()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let httpRequest = new XMLHttpRequest();
2+
httpRequest.open("POST", "/ReEdgeGPT/chat", true)
3+
httpRequest.setRequestHeader("content-type", "application/json")
4+
httpRequest.send(JSON.stringify({
5+
"prompt": "Deer stew",
6+
}))
7+
httpRequest.onreadystatechange = function () {
8+
if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
9+
console.log(httpRequest.response)
10+
} else if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status !== 200) {
11+
console.log(httpRequest.response)
12+
}
13+
}

docker_requirements.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
aiohttp
2+
certifi
3+
httpx
4+
prompt_toolkit
5+
requests
6+
rich
7+
re_edge_gpt
8+
flask[async]
9+
regex
10+
gunicorn

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ prompt_toolkit
55
requests
66
rich
77
re_edge_gpt
8+
regex

0 commit comments

Comments
 (0)