|
@@ -3,6 +3,8 @@ import os
|
|
import openai
|
|
import openai
|
|
from flask import Flask, request, jsonify, send_from_directory, url_for
|
|
from flask import Flask, request, jsonify, send_from_directory, url_for
|
|
|
|
|
|
|
|
+import json
|
|
|
|
+
|
|
app = Flask(__name__)
|
|
app = Flask(__name__)
|
|
ssl = None
|
|
ssl = None
|
|
# ssl =('/etc/ssl/sample.crt', '/etc/ssl/sample.pem')
|
|
# ssl =('/etc/ssl/sample.crt', '/etc/ssl/sample.pem')
|
|
@@ -22,6 +24,11 @@ app.suggest_messages = [
|
|
{"role": "system",
|
|
{"role": "system",
|
|
"content": "Please suggest reply messages based on the previous conversations and the user's request."}
|
|
"content": "Please suggest reply messages based on the previous conversations and the user's request."}
|
|
]
|
|
]
|
|
|
|
+app.recommend_messages = [
|
|
|
|
+ {"role": "system",
|
|
|
|
+ "content": "Give normalized total weight of each category in json based on headlines"
|
|
|
|
+ }
|
|
|
|
+]
|
|
UPLOAD_FOLDER = 'files'
|
|
UPLOAD_FOLDER = 'files'
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
|
|
@@ -33,11 +40,70 @@ def download_file(name):
|
|
def test():
|
|
def test():
|
|
return jsonify({"status": "0"})
|
|
return jsonify({"status": "0"})
|
|
|
|
|
|
|
|
+def recommend(headlines, category):
|
|
|
|
+ chat_messages = app.recommend_messages.copy()
|
|
|
|
+ try:
|
|
|
|
+ json_payload = {
|
|
|
|
+ "role": "user",
|
|
|
|
+ "content": f"""{headlines}
|
|
|
|
+ Berikan nilai berat masing-masing kategori, jumlahkan dan normalisasikan:
|
|
|
|
+ {category}
|
|
|
|
+ Berikan dalam bentuk json
|
|
|
|
+ """
|
|
|
|
+ }
|
|
|
|
+ chat_messages.append(json_payload)
|
|
|
|
+ print(chat_messages)
|
|
|
|
+ json_response = openai.ChatCompletion.create(model="gpt-3.5-turbo-1106",
|
|
|
|
+ messages=chat_messages,
|
|
|
|
+ response_format={ "type": "json_object" }
|
|
|
|
+ )
|
|
|
|
+ print(json_response.choices[0]["message"]["content"])
|
|
|
|
+ return json.loads(json_response.choices[0]["message"]["content"])
|
|
|
|
+ except Exception as error_print:
|
|
|
|
+ app.logger.error(error_print)
|
|
|
|
+ result = {}, 405
|
|
|
|
+
|
|
|
|
+def vision(message, image_url=None, image_b64=None):
|
|
|
|
+ chat_messages = app.chat_messages.copy()
|
|
|
|
+ url = ""
|
|
|
|
+ if image_url:
|
|
|
|
+ url = f"{image_url}"
|
|
|
|
+ elif image_b64:
|
|
|
|
+ url = f"data:image/jpeg;base64,{image_b64}"
|
|
|
|
+ try:
|
|
|
|
+ json_payload = {
|
|
|
|
+ "role": "user",
|
|
|
|
+ "content": [
|
|
|
|
+ {"type": "text", "text": message},
|
|
|
|
+ {
|
|
|
|
+ "type": "image_url",
|
|
|
|
+ "image_url": {
|
|
|
|
+ "url": url,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ }
|
|
|
|
+ chat_messages.append(json_payload)
|
|
|
|
+ print(chat_messages)
|
|
|
|
+ json_response = openai.ChatCompletion.create(
|
|
|
|
+ model="gpt-4-vision-preview",
|
|
|
|
+ messages=chat_messages,
|
|
|
|
+ max_tokens=300
|
|
|
|
+ )
|
|
|
|
+ return json_response.choices[0]["message"]
|
|
|
|
+ except Exception as error_print:
|
|
|
|
+ app.logger.error(error_print)
|
|
|
|
+ result = {}, 405
|
|
|
|
+
|
|
|
|
+
|
|
@app.route('/gpt', methods=['POST'])
|
|
@app.route('/gpt', methods=['POST'])
|
|
def gpt():
|
|
def gpt():
|
|
chat_messages = app.chat_messages.copy()
|
|
chat_messages = app.chat_messages.copy()
|
|
use_video = False
|
|
use_video = False
|
|
suggest = False
|
|
suggest = False
|
|
|
|
+ category = []
|
|
|
|
+ headlines = []
|
|
|
|
+ image_url = ""
|
|
num_choices = 1
|
|
num_choices = 1
|
|
json_payload = request.get_json()
|
|
json_payload = request.get_json()
|
|
if not json_payload:
|
|
if not json_payload:
|
|
@@ -88,6 +154,18 @@ def gpt():
|
|
"content": greeting_message
|
|
"content": greeting_message
|
|
}
|
|
}
|
|
]
|
|
]
|
|
|
|
+ elif 'recommend' in json_payload:
|
|
|
|
+ headlines = json_payload['recommend']['headlines']
|
|
|
|
+ category = json_payload['recommend']['category']
|
|
|
|
+ return recommend(headlines, category)
|
|
|
|
+ elif 'image_url' in json_payload:
|
|
|
|
+ image = json_payload['image_url']
|
|
|
|
+ message = json_payload["message"] if 'message' in json_payload else "Ini gambar apa?"
|
|
|
|
+ return vision(message,image_url=image)
|
|
|
|
+ elif 'image_b64' in json_payload:
|
|
|
|
+ image = json_payload['image_b64']
|
|
|
|
+ message = json_payload["message"] if 'message' in json_payload else "Ini gambar apa?"
|
|
|
|
+ return vision(message,image_b64=image_url)
|
|
else:
|
|
else:
|
|
chat_messages = app.chat_messages.copy()
|
|
chat_messages = app.chat_messages.copy()
|
|
json_payload = [json_payload]
|
|
json_payload = [json_payload]
|