Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,26 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify, render_template
|
| 2 |
import base64
|
| 3 |
import re
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
|
| 6 |
# OpenAI API Key
|
| 7 |
-
api_key_1 = "sk-"
|
| 8 |
-
api_key_2 = "Ts4M29N6u2rPPzsrCy2qT3BlbkFJu1z6otKVXaJAbaIvIesj"
|
| 9 |
-
huggingface_api_key_1 = "hf_"
|
| 10 |
-
huggingface_api_key_2 = "RIYBqKlSJQSvLeQuWuTXuohTuhzVMMWBZR"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
huggingface_url = "https://huggingface.co/spaces/devlim/supernova/upload"
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@app.route('/')
|
| 20 |
def index():
|
| 21 |
return render_template('index.html')
|
|
@@ -29,31 +34,25 @@ def save_image():
|
|
| 29 |
image_data = re.sub('^data:image/.+;base64,', '', image_data)
|
| 30 |
image_data = base64.b64decode(image_data)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
temp_file.write(image_data)
|
| 36 |
-
|
| 37 |
-
# Upload image to Hugging Face
|
| 38 |
-
headers = {
|
| 39 |
-
"Authorization": f"Bearer {huggingface_api_key}"
|
| 40 |
-
}
|
| 41 |
-
files = {
|
| 42 |
-
'file': (temp_filename, open(temp_filename, 'rb'), 'image/png')
|
| 43 |
-
}
|
| 44 |
-
response = requests.post(huggingface_url, headers=headers, files=files)
|
| 45 |
-
|
| 46 |
-
if response.status_code != 200:
|
| 47 |
-
return jsonify({'message': 'Error: νκΉ
νμ΄μ€μ μ΄λ―Έμ§λ₯Ό μ
λ‘λν μ μμ΅λλ€.'}), 500
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
headers = {
|
| 54 |
"Content-Type": "application/json",
|
| 55 |
"Authorization": f"Bearer {api_key}"
|
| 56 |
}
|
|
|
|
| 57 |
payload = {
|
| 58 |
"model": "gpt-4",
|
| 59 |
"messages": [
|
|
@@ -67,7 +66,7 @@ def save_image():
|
|
| 67 |
{
|
| 68 |
"type": "image_url",
|
| 69 |
"image_url": {
|
| 70 |
-
"url":
|
| 71 |
}
|
| 72 |
}
|
| 73 |
]
|
|
@@ -83,8 +82,12 @@ def save_image():
|
|
| 83 |
analysis_result = result['choices'][0]['message']['content']
|
| 84 |
else:
|
| 85 |
analysis_result = "Error: λΉλ₯λ₯Ό μ°Ύμ μ μμ΅λλ€."
|
| 86 |
-
|
| 87 |
-
return jsonify({'message': 'λΆμμ΄ μλ£λμμ΅λλ€.', 'analysis_result': analysis_result})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
if __name__ == '__main__':
|
| 90 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 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')
|
|
|
|
| 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",
|
| 53 |
"Authorization": f"Bearer {api_key}"
|
| 54 |
}
|
| 55 |
+
|
| 56 |
payload = {
|
| 57 |
"model": "gpt-4",
|
| 58 |
"messages": [
|
|
|
|
| 66 |
{
|
| 67 |
"type": "image_url",
|
| 68 |
"image_url": {
|
| 69 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 70 |
}
|
| 71 |
}
|
| 72 |
]
|
|
|
|
| 82 |
analysis_result = result['choices'][0]['message']['content']
|
| 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)
|