|
from flask import Flask, request, jsonify, render_template, send_from_directory |
|
import base64 |
|
import re |
|
import os |
|
from datetime import datetime |
|
import requests |
|
|
|
|
|
api_key = "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" |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@app.route('/save_image', methods=['POST']) |
|
def save_image(): |
|
data = request.get_json() |
|
image_data = data['image'] |
|
|
|
|
|
image_data = re.sub('^data:image/.+;base64,', '', image_data) |
|
image_data = base64.b64decode(image_data) |
|
|
|
|
|
base64_image = base64.b64encode(image_data).decode('utf-8') |
|
|
|
headers = { |
|
"Content-Type": "application/json", |
|
"Authorization": f"Bearer {api_key}" |
|
} |
|
|
|
payload = { |
|
"model": "gpt-4", |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{ |
|
"type": "text", |
|
"text": "์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ๋น๋ฅ๊ฐ ๋ช g์ธ์ง ์์์ ๊ฐ์ ํ์๋ง ์ถ๋ ฅํ์์ค.\n์) ๋น๋ฅ : 10g \n์ํ๋ถ์ํ๊ฐ ์๋๋ผ๋ฉด 'error'๋ฅผ ์ถ๋ ฅํ์์ค." |
|
}, |
|
{ |
|
"type": "image_url", |
|
"image_url": { |
|
"url": f"data:image/jpeg;base64,{base64_image}" |
|
} |
|
} |
|
] |
|
} |
|
], |
|
"max_tokens": 300 |
|
} |
|
|
|
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) |
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
analysis_result = result['choices'][0]['message']['content'] |
|
else: |
|
analysis_result = "Error: ๋น๋ฅ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค." |
|
|
|
print(analysis_result) |
|
return jsonify({'message': '๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.', 'analysis_result': analysis_result}) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=7860, debug=True) |