-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Integration-Automation/dev
Add docker file and flask example
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 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,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"] |
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,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" |
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,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() |
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,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) | ||
} | ||
} |
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,10 @@ | ||
aiohttp | ||
certifi | ||
httpx | ||
prompt_toolkit | ||
requests | ||
rich | ||
re_edge_gpt | ||
flask[async] | ||
regex | ||
gunicorn |
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ prompt_toolkit | |
requests | ||
rich | ||
re_edge_gpt | ||
regex |