devlim commited on
Commit
286e460
ยท
verified ยท
1 Parent(s): af374d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -47
app.py CHANGED
@@ -1,26 +1,13 @@
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
 
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
- image_data = base64.b64decode(image_data)
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
- "type": "text",
64
- "text": "์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹น๋ฅ˜๊ฐ€ ๋ช‡ g์ธ์ง€ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ํ˜•์‹๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค.\n์˜ˆ) ๋‹น๋ฅ˜ : 10g \n์ƒํ’ˆ๋ถ„์„ํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 'error'๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค."
65
- },
66
- {
67
- "type": "image_url",
68
- "image_url": {
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': '๋ถ„์„์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.', 'image_url': file_path, 'analysis_result': analysis_result})
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)