|
from flask import Flask, request, jsonify, render_template |
|
import base64 |
|
import re |
|
import requests |
|
|
|
|
|
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'] |
|
|
|
|
|
image_data = re.sub('^data:image/.+;base64,', '', image_data) |
|
image_data = base64.b64decode(image_data) |
|
|
|
|
|
temp_filename = "temp_image.png" |
|
with open(temp_filename, "wb") as temp_file: |
|
temp_file.write(image_data) |
|
|
|
|
|
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 |
|
|
|
|
|
uploaded_image_url = response.json()['url'] |
|
|
|
|
|
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) |
|
|