Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,13 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template
|
| 2 |
import base64
|
| 3 |
import re
|
| 4 |
-
import os
|
| 5 |
-
from datetime import datetime
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
# OpenAI API Key
|
| 11 |
api_key = "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"
|
| 12 |
|
| 13 |
-
app = Flask(__name__)
|
| 14 |
-
|
| 15 |
-
# Ensure the directory exists
|
| 16 |
-
uploads_dir = 'uploads'
|
| 17 |
-
os.makedirs(uploads_dir, exist_ok=True)
|
| 18 |
-
|
| 19 |
-
# Function to encode the image
|
| 20 |
-
def encode_image(image_path):
|
| 21 |
-
with open(image_path, "rb") as image_file:
|
| 22 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 23 |
-
|
| 24 |
@app.route('/')
|
| 25 |
def index():
|
| 26 |
return render_template('index.html')
|
|
@@ -32,21 +19,7 @@ def save_image():
|
|
| 32 |
|
| 33 |
# Decode the base64 image data
|
| 34 |
image_data = re.sub('^data:image/.+;base64,', '', image_data)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Create a unique file name
|
| 38 |
-
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 39 |
-
file_path = os.path.join(uploads_dir, f'captured_image_{timestamp}.png')
|
| 40 |
-
|
| 41 |
-
# Save the image to a file
|
| 42 |
-
with open(file_path, 'wb') as f:
|
| 43 |
-
f.write(image_data)
|
| 44 |
-
|
| 45 |
-
# Path to your image
|
| 46 |
-
image_path = file_path
|
| 47 |
-
|
| 48 |
-
# Getting the base64 string
|
| 49 |
-
base64_image = encode_image(image_path)
|
| 50 |
|
| 51 |
headers = {
|
| 52 |
"Content-Type": "application/json",
|
|
@@ -58,18 +31,14 @@ def save_image():
|
|
| 58 |
"messages": [
|
| 59 |
{
|
| 60 |
"role": "user",
|
| 61 |
-
"content":
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 70 |
-
}
|
| 71 |
-
}
|
| 72 |
-
]
|
| 73 |
}
|
| 74 |
],
|
| 75 |
"max_tokens": 300
|
|
@@ -83,11 +52,7 @@ def save_image():
|
|
| 83 |
else:
|
| 84 |
analysis_result = "Error: ๋น๋ฅ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 85 |
|
| 86 |
-
return jsonify({'message': '๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.', '
|
| 87 |
-
|
| 88 |
-
@app.route('/images/<filename>')
|
| 89 |
-
def get_image(filename):
|
| 90 |
-
return send_from_directory('.', filename)
|
| 91 |
|
| 92 |
if __name__ == '__main__':
|
| 93 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template
|
| 2 |
import base64
|
| 3 |
import re
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
|
| 8 |
# OpenAI API Key
|
| 9 |
api_key = "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"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.route('/')
|
| 12 |
def index():
|
| 13 |
return render_template('index.html')
|
|
|
|
| 19 |
|
| 20 |
# Decode the base64 image data
|
| 21 |
image_data = re.sub('^data:image/.+;base64,', '', image_data)
|
| 22 |
+
base64_image = image_data # This is the base64 string of the image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
headers = {
|
| 25 |
"Content-Type": "application/json",
|
|
|
|
| 31 |
"messages": [
|
| 32 |
{
|
| 33 |
"role": "user",
|
| 34 |
+
"content": "์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ๋น๋ฅ๊ฐ ๋ช g์ธ์ง ์์์ ๊ฐ์ ํ์๋ง ์ถ๋ ฅํ์์ค.\n์) ๋น๋ฅ : 10g \n์ํ๋ถ์ํ๊ฐ ์๋๋ผ๋ฉด 'error'๋ฅผ ์ถ๋ ฅํ์์ค."
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"role": "user",
|
| 38 |
+
"content": {
|
| 39 |
+
"type": "image",
|
| 40 |
+
"image": f"data:image/png;base64,{base64_image}"
|
| 41 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
],
|
| 44 |
"max_tokens": 300
|
|
|
|
| 52 |
else:
|
| 53 |
analysis_result = "Error: ๋น๋ฅ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 54 |
|
| 55 |
+
return jsonify({'message': '๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.', 'analysis_result': analysis_result})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == '__main__':
|
| 58 |
app.run(host='0.0.0.0', port=7860, debug=True)
|