瀏覽代碼

update json response

kevin 2 年之前
父節點
當前提交
2c498bd07a
共有 4 個文件被更改,包括 30 次插入7 次删除
  1. 2 1
      .dockerignore
  2. 3 1
      docker-compose.yaml
  3. 24 3
      main.py
  4. 1 2
      start_docker.sh

+ 2 - 1
.dockerignore

@@ -12,4 +12,5 @@ start_docker.sh
 stop_docker.sh
 hello*
 README.md
-*.zip
+*.zip
+files

+ 3 - 1
docker-compose.yaml

@@ -4,7 +4,9 @@ services:
     build: .
     restart: always
     ports:
-      - "80:8348"
+      - "8438:8348"
+    volumes:
+      - "./files:/usr/src/app/files"
     environment:
       WORKERS: 2
       TIMEOUT: 120

+ 24 - 3
main.py

@@ -1,6 +1,6 @@
 import json
 import openai
-from flask import Flask, request, jsonify
+from flask import Flask, request, jsonify, send_from_directory, url_for
 import re
 import os
 import time
@@ -15,7 +15,12 @@ app.chat_messages = [
     {"role": "system",
      "content": "Jawab hanya dengan bahasa Indonesia"}
 ]
+UPLOAD_FOLDER = 'files'
+app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
 
+@app.route('/files/<name>')
+def download_file(name):
+    return send_from_directory(app.config["UPLOAD_FOLDER"], name)
 
 @app.route('/', methods=['GET', 'POST'])
 def test():
@@ -24,11 +29,20 @@ def test():
 @app.route('/gpt', methods=['POST'])
 def gpt():
     chat_messages = app.chat_messages.copy()
+    use_video = False
     json_payload = request.get_json()
     if not json_payload:
         json_payload = []
+    has_named_params = False
     if isinstance(json_payload, dict):
-        json_payload = [json_payload]
+        has_named_params = 'use_video' in json_payload or 'payload' in json_payload
+        if 'use_video' in json_payload and 'payload' in json_payload:
+            use_video = json_payload['use_video'] == "1"
+            json_payload = json_payload['payload']
+            if isinstance(json_payload, dict):
+                json_payload = [json_payload]
+        else:
+            json_payload = [json_payload]
     json_payload = json_payload[-5:]
     for message in json_payload:
         content = message['content']
@@ -47,7 +61,14 @@ def gpt():
                                                      messages=chat_messages,
                                                      max_tokens=300, temperature=0.6)
         print(json_response.choices[0].message)
-        result = json_response.choices[0].message
+        if has_named_params:
+            if use_video:
+                # TODO: to be implemented
+                result = {"url":  url_for('download_file', name="test.mp4", _external=True), "message": json_response.choices[0].message}
+            else:
+                result = {"url": "", "message": json_response.choices[0].message}
+        else:
+            result = json_response.choices[0].message
     except Exception as error_print:
         print(error_print)
         result = {}, 405

+ 1 - 2
start_docker.sh

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