devlim commited on
Commit
efbde5b
โ€ข
1 Parent(s): ca60311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -90
app.py CHANGED
@@ -1,91 +1,91 @@
1
- from flask import Flask, request, jsonify, render_template, send_from_directory
2
- import base64
3
- import re
4
- import os
5
- from datetime import datetime
6
- import requests
7
-
8
- # OpenAI API Key
9
- 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
- with open(image_path, "rb") as image_file:
16
- return base64.b64encode(image_file.read()).decode('utf-8')
17
-
18
- @app.route('/')
19
- def index():
20
- return render_template('index.html')
21
-
22
- @app.route('/save_image', methods=['POST'])
23
- def save_image():
24
- data = request.get_json()
25
- image_data = data['image']
26
-
27
- # Decode the base64 image data
28
- image_data = re.sub('^data:image/.+;base64,', '', image_data)
29
- image_data = base64.b64decode(image_data)
30
-
31
- # Create a unique file name
32
- timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
33
- file_path = f'captured_image_{timestamp}.png'
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
43
-
44
- # Getting the base64 string
45
- base64_image = encode_image(image_path)
46
-
47
- headers = {
48
- "Content-Type": "application/json",
49
- "Authorization": f"Bearer {api_key}"
50
- }
51
-
52
- payload = {
53
- "model": "gpt-4o",
54
- "messages": [
55
- {
56
- "role": "user",
57
- "content": [
58
- {
59
- "type": "text",
60
- "text": "์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹น๋ฅ˜๊ฐ€ ๋ช‡ g์ธ์ง€ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ํ˜•์‹๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค.\n์˜ˆ) ๋‹น๋ฅ˜ : 10g \n์ƒํ’ˆ๋ถ„์„ํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 'error'๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค."
61
- },
62
- {
63
- "type": "image_url",
64
- "image_url": {
65
- "url": f"data:image/jpeg;base64,{base64_image}"
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)
75
-
76
- if response.status_code == 200:
77
- result = response.json()
78
- analysis_result = result['choices'][0]['message']['content']
79
- else:
80
- analysis_result = "Error: ๋‹น๋ฅ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
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)
89
-
90
- if __name__ == '__main__':
91
  app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ from flask import Flask, request, jsonify, render_template, send_from_directory
2
+ import base64
3
+ import re
4
+ import os
5
+ from datetime import datetime
6
+ import requests
7
+
8
+ # OpenAI API Key
9
+ 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
+ with open(image_path, "rb") as image_file:
16
+ return base64.b64encode(image_file.read()).decode('utf-8')
17
+
18
+ @app.route('/')
19
+ def index():
20
+ return render_template('index.html')
21
+
22
+ @app.route('/save_image', methods=['POST'])
23
+ def save_image():
24
+ data = request.get_json()
25
+ image_data = data['image']
26
+
27
+ # Decode the base64 image data
28
+ image_data = re.sub('^data:image/.+;base64,', '', image_data)
29
+ image_data = base64.b64decode(image_data)
30
+
31
+ # Create a unique file name
32
+ timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
33
+ file_path = os.path.join('uploads', f'captured_image_{timestamp}.png')
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
43
+
44
+ # Getting the base64 string
45
+ base64_image = encode_image(image_path)
46
+
47
+ headers = {
48
+ "Content-Type": "application/json",
49
+ "Authorization": f"Bearer {api_key}"
50
+ }
51
+
52
+ payload = {
53
+ "model": "gpt-4o",
54
+ "messages": [
55
+ {
56
+ "role": "user",
57
+ "content": [
58
+ {
59
+ "type": "text",
60
+ "text": "์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹น๋ฅ˜๊ฐ€ ๋ช‡ g์ธ์ง€ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ํ˜•์‹๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค.\n์˜ˆ) ๋‹น๋ฅ˜ : 10g \n์ƒํ’ˆ๋ถ„์„ํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 'error'๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค."
61
+ },
62
+ {
63
+ "type": "image_url",
64
+ "image_url": {
65
+ "url": f"data:image/jpeg;base64,{base64_image}"
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)
75
+
76
+ if response.status_code == 200:
77
+ result = response.json()
78
+ analysis_result = result['choices'][0]['message']['content']
79
+ else:
80
+ analysis_result = "Error: ๋‹น๋ฅ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
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)
89
+
90
+ if __name__ == '__main__':
91
  app.run(host='0.0.0.0', port=7860, debug=True)