Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,55 @@
|
|
1 |
-
from flask import Flask, request, jsonify, render_template
|
2 |
import base64
|
3 |
import re
|
4 |
-
import os
|
5 |
-
from datetime import datetime
|
6 |
import requests
|
7 |
-
|
8 |
-
# OpenAI API Key
|
9 |
-
api_key = "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"
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
13 |
@app.route('/')
|
14 |
def index():
|
15 |
return render_template('index.html')
|
16 |
|
17 |
@app.route('/save_image', methods=['POST'])
|
18 |
def save_image():
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
28 |
|
|
|
|
|
|
|
|
|
29 |
headers = {
|
30 |
"Content-Type": "application/json",
|
31 |
"Authorization": f"Bearer {api_key}"
|
32 |
}
|
33 |
-
|
34 |
payload = {
|
35 |
"model": "gpt-4",
|
36 |
"messages": [
|
@@ -44,7 +63,7 @@ def save_image():
|
|
44 |
{
|
45 |
"type": "image_url",
|
46 |
"image_url": {
|
47 |
-
"url":
|
48 |
}
|
49 |
}
|
50 |
]
|
@@ -55,14 +74,16 @@ def save_image():
|
|
55 |
|
56 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
57 |
|
|
|
|
|
58 |
if response.status_code == 200:
|
59 |
result = response.json()
|
60 |
analysis_result = result['choices'][0]['message']['content']
|
61 |
else:
|
62 |
analysis_result = "Error: λΉλ₯λ₯Ό μ°Ύμ μ μμ΅λλ€."
|
63 |
|
64 |
-
print(analysis_result)
|
65 |
return jsonify({'message': 'λΆμμ΄ μλ£λμμ΅λλ€.', 'analysis_result': analysis_result})
|
66 |
|
67 |
if __name__ == '__main__':
|
68 |
-
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template
|
2 |
import base64
|
3 |
import re
|
|
|
|
|
4 |
import requests
|
5 |
+
import os
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Get API keys from environment variables
|
10 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
11 |
+
huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
|
12 |
+
huggingface_url = "https://huggingface.co/api/spaces/devlim/supernova/upload"
|
13 |
+
|
14 |
@app.route('/')
|
15 |
def index():
|
16 |
return render_template('index.html')
|
17 |
|
18 |
@app.route('/save_image', methods=['POST'])
|
19 |
def save_image():
|
20 |
+
if 'image' not in request.files:
|
21 |
+
return jsonify({'message': 'No image part in the request'}), 400
|
22 |
+
|
23 |
+
file = request.files['image']
|
24 |
|
25 |
+
if file.filename == '':
|
26 |
+
return jsonify({'message': 'No selected file'}), 400
|
27 |
+
|
28 |
+
# Save the image to a temporary file
|
29 |
+
temp_image_path = os.path.join("temp", file.filename)
|
30 |
+
file.save(temp_image_path)
|
31 |
+
|
32 |
+
# Upload the image to Hugging Face Space
|
33 |
+
headers = {
|
34 |
+
"Authorization": f"Bearer {huggingface_api_key}"
|
35 |
+
}
|
36 |
+
files = {
|
37 |
+
'file': (file.filename, open(temp_image_path, 'rb'), 'image/png')
|
38 |
+
}
|
39 |
+
response = requests.post(huggingface_url, headers=headers, files=files)
|
40 |
|
41 |
+
if response.status_code != 200:
|
42 |
+
os.remove(temp_image_path)
|
43 |
+
return jsonify({'message': 'Error: νκΉ
νμ΄μ€μ μ΄λ―Έμ§λ₯Ό μ
λ‘λν μ μμ΅λλ€.'}), 500
|
44 |
|
45 |
+
# Get the uploaded image URL from Hugging Face response
|
46 |
+
uploaded_image_url = response.json()['url']
|
47 |
+
|
48 |
+
# Prepare the payload for OpenAI API
|
49 |
headers = {
|
50 |
"Content-Type": "application/json",
|
51 |
"Authorization": f"Bearer {api_key}"
|
52 |
}
|
|
|
53 |
payload = {
|
54 |
"model": "gpt-4",
|
55 |
"messages": [
|
|
|
63 |
{
|
64 |
"type": "image_url",
|
65 |
"image_url": {
|
66 |
+
"url": uploaded_image_url
|
67 |
}
|
68 |
}
|
69 |
]
|
|
|
74 |
|
75 |
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
76 |
|
77 |
+
os.remove(temp_image_path)
|
78 |
+
|
79 |
if response.status_code == 200:
|
80 |
result = response.json()
|
81 |
analysis_result = result['choices'][0]['message']['content']
|
82 |
else:
|
83 |
analysis_result = "Error: λΉλ₯λ₯Ό μ°Ύμ μ μμ΅λλ€."
|
84 |
|
|
|
85 |
return jsonify({'message': 'λΆμμ΄ μλ£λμμ΅λλ€.', 'analysis_result': analysis_result})
|
86 |
|
87 |
if __name__ == '__main__':
|
88 |
+
os.makedirs('temp', exist_ok=True)
|
89 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|