devlim commited on
Commit
50d7bed
ยท
verified ยท
1 Parent(s): 60773d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
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
- 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():
@@ -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('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
@@ -45,30 +48,30 @@ def save_image():
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)
@@ -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)