소스 검색

first commit

kevin 2 년 전
커밋
5ef48ad975
14개의 변경된 파일231개의 추가작업 그리고 0개의 파일을 삭제
  1. 14 0
      .dockerignore
  2. 76 0
      .gitignore
  3. 7 0
      Dockerfile
  4. 25 0
      Dockerfile.base
  5. 3 0
      build_docker_base.sh
  6. 10 0
      configure.sh
  7. 11 0
      docker-compose.yaml
  8. 22 0
      gunicorn.conf.py
  9. 38 0
      main.py
  10. 3 0
      requirements.txt
  11. 11 0
      run.sh
  12. 4 0
      start_docker.sh
  13. 3 0
      stop_docker.sh
  14. 4 0
      wsgi.py

+ 14 - 0
.dockerignore

@@ -0,0 +1,14 @@
+venv
+.gitignore
+.dockerignore
+.idea
+.git
+mysql
+Dockerfile
+Dockerfile.base
+configure.sh
+build_docker_base.sh
+start_docker.sh
+stop_docker.sh
+hello*
+README.md

+ 76 - 0
.gitignore

@@ -0,0 +1,76 @@
+# ---> Python
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+env/
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*,cover
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+/lfw.tgz
+/lfw/
+
+.idea
+application_data
+data
+mysql
+venv
+sql
+
+*.jpg
+*.jpeg
+
+.DS_Store
+
+hello*

+ 7 - 0
Dockerfile

@@ -0,0 +1,7 @@
+FROM chatbot_base
+
+COPY . .
+
+EXPOSE 8348
+
+CMD ["/usr/src/app/run.sh", "--no-venv", "--has-db"]

+ 25 - 0
Dockerfile.base

@@ -0,0 +1,25 @@
+FROM python:3.10-slim-bullseye AS chatbot_base
+
+WORKDIR /usr/src/app
+
+RUN apt-get -y update && \
+    apt-get -y upgrade && apt-get install --no-install-recommends -y --fix-missing \
+    build-essential \
+    cmake \
+    gfortran \
+    git \
+    wget \
+    curl \
+    pkg-config \
+    python3-dev \
+    software-properties-common \
+    zip \
+    nmap \
+    net-tools \
+    vim-tiny \
+    nano \
+    && apt-get clean && rm -rf /tmp/* /var/tmp/*
+
+COPY requirements.txt .
+
+RUN pip install -r requirements.txt

+ 3 - 0
build_docker_base.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+docker build -f Dockerfile.base --target chatbot_base --no-cache -t chatbot_base .

+ 10 - 0
configure.sh

@@ -0,0 +1,10 @@
+#! /bin/bash
+
+# Non-Docker only
+ABSPATH=$(cd "$(dirname "$0")"; pwd -P)
+if ! python -m venv "$ABSPATH"/venv;
+then
+  exit 1
+fi
+source "$ABSPATH"/venv/bin/activate
+python -m pip install -r "$ABSPATH"/requirements.txt

+ 11 - 0
docker-compose.yaml

@@ -0,0 +1,11 @@
+version: '3'
+services:
+  web:
+    build: .
+    restart: always
+    ports:
+      - "8348:8348"
+    environment:
+      WORKERS: 8
+      TIMEOUT: 60
+      OPENAI_KEY: "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"

+ 22 - 0
gunicorn.conf.py

@@ -0,0 +1,22 @@
+import time
+import os
+
+bind = '0.0.0.0:8348'
+workers = os.environ.get("WORKERS", "8")
+timeout = os.environ.get("TIMEOUT", "180")
+preload_app = True
+enable_stdio_inheritance = True
+loglevel = 'debug'
+accesslog = '-'
+pidfile = "app.pid"
+
+def on_starting(server):
+    worker_count = int(workers)
+
+    # Insert a 1-second gap between workers
+    gap_duration = 1
+
+    for i in range(worker_count):
+        # Add a delay between each worker
+        time.sleep(gap_duration)
+        print(f"Starting worker {i + 1}/{worker_count}")

+ 38 - 0
main.py

@@ -0,0 +1,38 @@
+import json
+import openai
+from flask import Flask, request, jsonify
+import re
+import os
+import time
+from openai import ChatCompletion
+
+app = Flask(__name__)
+ssl = None
+openai_key = os.environ.get("OPENAI_KEY","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")
+openai.api_key = openai_key
+app.chat_messages = [
+        {"role": "system", "content": "Kamu adalah asisten yang membantu dan selalu menjawab dengan singkat"}
+]
+
+@app.route('/', methods=['GET', 'POST'])
+def test():
+    return jsonify({"status": "0"})
+
+@app.route('/gpt', methods=['POST'])
+def gpt():
+    chat_messages = app.chat_messages.copy()
+    json_payload = request.get_json()
+    if not json_payload:
+        json_payload = []
+    for message in json_payload:
+        chat_messages.append(message)
+    response = openai.ChatCompletion.create(model="gpt-3.5-turbo",  # GPT-3.5 Turbo engine
+        messages=json_payload,
+        max_tokens=400,)
+    return response.choices[0].message
+
+# Press the green button in the gutter to run the script.
+if __name__ == '__main__':
+    app.run(host='0.0.0.0', port=8348, debug=True, ssl_context=ssl)
+
+# See PyCharm help at https://www.jetbrains.com/help/pycharm/

+ 3 - 0
requirements.txt

@@ -0,0 +1,3 @@
+Flask==2.3.2
+gunicorn==20.1.0
+openai==0.27.8

+ 11 - 0
run.sh

@@ -0,0 +1,11 @@
+#!/bin/bash
+
+VENV=1
+if [ "$1" == "--no-venv" ]; then
+  VENV=0
+fi
+ABSPATH=$(cd "$(dirname "$0")"; pwd -P)
+if [ $VENV -eq 1 ]; then
+  source "$ABSPATH"/venv/bin/activate
+fi
+gunicorn wsgi:app

+ 4 - 0
start_docker.sh

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+docker-compose build --no-cache
+docker-compose up "$@"

+ 3 - 0
stop_docker.sh

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+docker-compose down

+ 4 - 0
wsgi.py

@@ -0,0 +1,4 @@
+from main import app
+
+if __name__ == "__main__":
+    app.run()