devlim commited on
Commit
a0eab63
ยท
verified ยท
1 Parent(s): 5b7691d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ api_key = api_key_1 + api_key_2
13
+ huggingface_api_key = huggingface_api_key_1 + huggingface_api_key_2
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')
22
+
23
+ @app.route('/save_image', methods=['POST'])
24
+ def save_image():
25
+ data = request.get_json()
26
+ image_data = data['image']
27
+
28
+ # Decode the base64 image data
29
+ image_data = re.sub('^data:image/.+;base64,', '', image_data)
30
+ image_data = base64.b64decode(image_data)
31
+
32
+ # Save image to temporary file
33
+ temp_filename = "temp_image.png"
34
+ with open(temp_filename, "wb") as temp_file:
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
+ # Get uploaded image URL
50
+ uploaded_image_url = response.json()['url']
51
+
52
+ # Prepare payload for OpenAI API
53
+ headers = {
54
+ "Content-Type": "application/json",
55
+ "Authorization": f"Bearer {api_key}"
56
+ }
57
+ payload = {
58
+ "model": "gpt-4",
59
+ "messages": [
60
+ {
61
+ "role": "user",
62
+ "content": [
63
+ {
64
+ "type": "text",
65
+ "text": "์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋‹น๋ฅ˜๊ฐ€ ๋ช‡ g์ธ์ง€ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ํ˜•์‹๋งŒ ์ถœ๋ ฅํ•˜์‹œ์˜ค.\n์˜ˆ) ๋‹น๋ฅ˜ : 10g \n์ƒํ’ˆ๋ถ„์„ํ‘œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด 'error'๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค."
66
+ },
67
+ {
68
+ "type": "image_url",
69
+ "image_url": {
70
+ "url": uploaded_image_url
71
+ }
72
+ }
73
+ ]
74
+ }
75
+ ],
76
+ "max_tokens": 300
77
+ }
78
+
79
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
80
+
81
+ if response.status_code == 200:
82
+ result = response.json()
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)