File size: 2,905 Bytes
a0eab63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import re
import requests

# OpenAI API Key
api_key_1 = "sk-"
api_key_2 = "Ts4M29N6u2rPPzsrCy2qT3BlbkFJu1z6otKVXaJAbaIvIesj"
huggingface_api_key_1 = "hf_"
huggingface_api_key_2 = "RIYBqKlSJQSvLeQuWuTXuohTuhzVMMWBZR"

api_key = api_key_1 + api_key_2
huggingface_api_key = huggingface_api_key_1 + huggingface_api_key_2

huggingface_url = "https://huggingface.co/spaces/devlim/supernova/upload"

app = Flask(__name__)

@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)
    
    # Save image to temporary file
    temp_filename = "temp_image.png"
    with open(temp_filename, "wb") as temp_file:
        temp_file.write(image_data)
    
    # Upload image to Hugging Face
    headers = {
        "Authorization": f"Bearer {huggingface_api_key}"
    }
    files = {
        'file': (temp_filename, open(temp_filename, 'rb'), 'image/png')
    }
    response = requests.post(huggingface_url, headers=headers, files=files)
    
    if response.status_code != 200:
        return jsonify({'message': 'Error: ν—ˆκΉ…νŽ˜μ΄μŠ€μ— 이미지λ₯Ό μ—…λ‘œλ“œν•  수 μ—†μŠ΅λ‹ˆλ‹€.'}), 500
    
    # Get uploaded image URL
    uploaded_image_url = response.json()['url']
    
    # Prepare payload for OpenAI API
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    payload = {
        "model": "gpt-4",
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "이미지λ₯Ό μž…λ ₯λ°›μœΌλ©΄ λ‹Ήλ₯˜κ°€ λͺ‡ g인지 μ˜ˆμ‹œμ™€ 같은 ν˜•μ‹λ§Œ 좜λ ₯ν•˜μ‹œμ˜€.\n예) λ‹Ήλ₯˜ : 10g \nμƒν’ˆλΆ„μ„ν‘œκ°€ μ•„λ‹ˆλΌλ©΄ 'error'λ₯Ό 좜λ ₯ν•˜μ‹œμ˜€."
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": uploaded_image_url
                        }
                    }
                ]
            }
        ],
        "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': '뢄석이 μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.', 'analysis_result': analysis_result})

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