File size: 2,524 Bytes
efbde5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efb95c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from flask import Flask, request, jsonify, render_template, send_from_directory
import base64
import re
import os
from datetime import datetime
import requests

# OpenAI API Key
api_key = "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"

app = Flask(__name__)

# Function to encode the image
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/save_image', methods=['POST'])
def save_image():
    data = request.get_json()
    image_data = data['image']
    
    # Decode the base64 image data
    image_data = re.sub('^data:image/.+;base64,', '', image_data)
    image_data = base64.b64decode(image_data)
    
    # Create a unique file name
    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    file_path = os.path.join('uploads', f'captured_image_{timestamp}.png')
    
    # Save the image to a file
    with open(file_path, 'wb') as f:
        f.write(image_data)
    
    

    # Path to your image
    image_path = file_path

    # Getting the base64 string
    base64_image = encode_image(image_path)

    headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
    }

    payload = {
    "model": "gpt-4o",
    "messages": [
        {
        "role": "user",
        "content": [
            {
            "type": "text",
            "text": "์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹น๋ฅ˜๊ฐ€ ๋ช‡ g์ธ์ง€ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ํ˜•์‹๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค.\n์˜ˆ) ๋‹น๋ฅ˜ : 10g \n์ƒํ’ˆ๋ถ„์„ํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 'error'๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค."
            },
            {
            "type": "image_url",
            "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}"
            }
            }
        ]
        }
    ],
    "max_tokens": 300
    }

    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

    if response.status_code == 200:
        result = response.json()
        analysis_result = result['choices'][0]['message']['content']
    else:
        analysis_result = "Error: ๋‹น๋ฅ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."

    return jsonify({'message': '๋ถ„์„์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.', 'image_url': file_path, 'analysis_result': analysis_result})



@app.route('/images/<filename>')
def get_image(filename):
    return send_from_directory('.', filename)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=True)