Update app.py
Browse files
app.py
CHANGED
@@ -10,10 +10,15 @@ api_key = "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"
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
# Function to encode the image
|
14 |
def encode_image(image_path):
|
15 |
-
|
16 |
-
|
17 |
|
18 |
@app.route('/')
|
19 |
def index():
|
@@ -30,13 +35,11 @@ def save_image():
|
|
30 |
|
31 |
# Create a unique file name
|
32 |
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
33 |
-
file_path = os.path.join(
|
34 |
|
35 |
# Save the image to a file
|
36 |
with open(file_path, 'wb') as f:
|
37 |
f.write(image_data)
|
38 |
-
|
39 |
-
|
40 |
|
41 |
# Path to your image
|
42 |
image_path = file_path
|
@@ -45,30 +48,30 @@ def save_image():
|
|
45 |
base64_image = encode_image(image_path)
|
46 |
|
47 |
headers = {
|
48 |
-
|
49 |
-
|
50 |
}
|
51 |
|
52 |
payload = {
|
53 |
-
|
54 |
-
|
55 |
-
{
|
56 |
-
"role": "user",
|
57 |
-
"content": [
|
58 |
-
{
|
59 |
-
"type": "text",
|
60 |
-
"text": "์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ๋น๋ฅ๊ฐ ๋ช g์ธ์ง ์์์ ๊ฐ์ ํ์๋ง ์ถ๋ ฅํ์์ค.\n์) ๋น๋ฅ : 10g \n์ํ๋ถ์ํ๊ฐ ์๋๋ผ๋ฉด 'error'๋ฅผ ์ถ๋ ฅํ์์ค."
|
61 |
-
},
|
62 |
{
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
}
|
67 |
-
|
68 |
-
|
69 |
-
}
|
70 |
-
],
|
71 |
-
"max_tokens": 300
|
72 |
}
|
73 |
|
74 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
@@ -81,8 +84,6 @@ def save_image():
|
|
81 |
|
82 |
return jsonify({'message': '๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.', 'image_url': file_path, 'analysis_result': analysis_result})
|
83 |
|
84 |
-
|
85 |
-
|
86 |
@app.route('/images/<filename>')
|
87 |
def get_image(filename):
|
88 |
return send_from_directory('.', filename)
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
13 |
+
# Ensure the directory exists and set appropriate permissions
|
14 |
+
uploads_dir = 'uploads'
|
15 |
+
os.makedirs(uploads_dir, exist_ok=True)
|
16 |
+
os.chmod(uploads_dir, 0o777)
|
17 |
+
|
18 |
# Function to encode the image
|
19 |
def encode_image(image_path):
|
20 |
+
with open(image_path, "rb") as image_file:
|
21 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
22 |
|
23 |
@app.route('/')
|
24 |
def index():
|
|
|
35 |
|
36 |
# Create a unique file name
|
37 |
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
38 |
+
file_path = os.path.join(uploads_dir, f'captured_image_{timestamp}.png')
|
39 |
|
40 |
# Save the image to a file
|
41 |
with open(file_path, 'wb') as f:
|
42 |
f.write(image_data)
|
|
|
|
|
43 |
|
44 |
# Path to your image
|
45 |
image_path = file_path
|
|
|
48 |
base64_image = encode_image(image_path)
|
49 |
|
50 |
headers = {
|
51 |
+
"Content-Type": "application/json",
|
52 |
+
"Authorization": f"Bearer {api_key}"
|
53 |
}
|
54 |
|
55 |
payload = {
|
56 |
+
"model": "gpt-4",
|
57 |
+
"messages": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
{
|
59 |
+
"role": "user",
|
60 |
+
"content": [
|
61 |
+
{
|
62 |
+
"type": "text",
|
63 |
+
"text": "์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ๋น๋ฅ๊ฐ ๋ช g์ธ์ง ์์์ ๊ฐ์ ํ์๋ง ์ถ๋ ฅํ์์ค.\n์) ๋น๋ฅ : 10g \n์ํ๋ถ์ํ๊ฐ ์๋๋ผ๋ฉด 'error'๋ฅผ ์ถ๋ ฅํ์์ค."
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"type": "image_url",
|
67 |
+
"image_url": {
|
68 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
69 |
+
}
|
70 |
+
}
|
71 |
+
]
|
72 |
}
|
73 |
+
],
|
74 |
+
"max_tokens": 300
|
|
|
|
|
|
|
75 |
}
|
76 |
|
77 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
|
|
84 |
|
85 |
return jsonify({'message': '๋ถ์์ด ์๋ฃ๋์์ต๋๋ค.', 'image_url': file_path, 'analysis_result': analysis_result})
|
86 |
|
|
|
|
|
87 |
@app.route('/images/<filename>')
|
88 |
def get_image(filename):
|
89 |
return send_from_directory('.', filename)
|