Delete app.py
Browse files
app.py
DELETED
@@ -1,89 +0,0 @@
|
|
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": [
|
56 |
-
{
|
57 |
-
"role": "user",
|
58 |
-
"content": [
|
59 |
-
{
|
60 |
-
"type": "text",
|
61 |
-
"text": "์ด๋ฏธ์ง๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด ๋น๋ฅ๊ฐ ๋ช g์ธ์ง ์์์ ๊ฐ์ ํ์๋ง ์ถ๋ ฅํ์์ค.\n์) ๋น๋ฅ : 10g \n์ํ๋ถ์ํ๊ฐ ์๋๋ผ๋ฉด 'error'๋ฅผ ์ถ๋ ฅํ์์ค."
|
62 |
-
},
|
63 |
-
{
|
64 |
-
"type": "image_url",
|
65 |
-
"image_url": {
|
66 |
-
"url": uploaded_image_url
|
67 |
-
}
|
68 |
-
}
|
69 |
-
]
|
70 |
-
}
|
71 |
-
],
|
72 |
-
"max_tokens": 300
|
73 |
-
}
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|