File size: 2,895 Bytes
4158971
efbde5b
 
 
4158971
efbde5b
 
 
4158971
 
 
 
 
efbde5b
 
 
 
 
 
4158971
 
 
 
efbde5b
4158971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efbde5b
4158971
 
 
efbde5b
4158971
 
 
 
efbde5b
50d7bed
 
efbde5b
 
50d7bed
 
efbde5b
50d7bed
 
 
 
 
 
 
 
 
4158971
50d7bed
 
 
efbde5b
50d7bed
 
efbde5b
 
 
 
4158971
 
efbde5b
 
 
 
 
 
da6252f
efbde5b
 
4158971
 
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
from flask import Flask, request, jsonify, render_template
import base64
import re
import requests
import os

app = Flask(__name__)

# Get API keys from environment variables
api_key = os.getenv('OPENAI_API_KEY')
huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
huggingface_url = "https://huggingface.co/api/spaces/devlim/supernova/upload"

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

@app.route('/save_image', methods=['POST'])
def save_image():
    if 'image' not in request.files:
        return jsonify({'message': 'No image part in the request'}), 400

    file = request.files['image']
    
    if file.filename == '':
        return jsonify({'message': 'No selected file'}), 400

    # Save the image to a temporary file
    temp_image_path = os.path.join("temp", file.filename)
    file.save(temp_image_path)

    # Upload the image to Hugging Face Space
    headers = {
        "Authorization": f"Bearer {huggingface_api_key}"
    }
    files = {
        'file': (file.filename, open(temp_image_path, 'rb'), 'image/png')
    }
    response = requests.post(huggingface_url, headers=headers, files=files)
    
    if response.status_code != 200:
        os.remove(temp_image_path)
        return jsonify({'message': 'Error: ํ—ˆ๊น…ํŽ˜์ด์Šค์— ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.'}), 500

    # Get the uploaded image URL from Hugging Face response
    uploaded_image_url = response.json()['url']

    # Prepare the 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)

    os.remove(temp_image_path)

    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__':
    os.makedirs('temp', exist_ok=True)
    app.run(host='0.0.0.0', port=7860, debug=True)