Spaces:
Paused
Paused
Kastg
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,40 +97,6 @@ def status():
|
|
| 97 |
def page_not_found(e):
|
| 98 |
return send_from_directory(static_dir, '404.html'), 404
|
| 99 |
|
| 100 |
-
|
| 101 |
-
def generate_random_text(length=10):
|
| 102 |
-
letters = string.ascii_lowercase
|
| 103 |
-
return ''.join(random.choice(letters) for _ in range(length))
|
| 104 |
-
|
| 105 |
-
@app.route('/upload', methods=['GET'])
|
| 106 |
-
def upload_image():
|
| 107 |
-
try:
|
| 108 |
-
# Get the URL parameter
|
| 109 |
-
url = request.args.get('url')
|
| 110 |
-
|
| 111 |
-
if not url:
|
| 112 |
-
return jsonify({'error': 'URL parameter is missing'}), 400
|
| 113 |
-
|
| 114 |
-
# Download the image
|
| 115 |
-
response = requests.get(url)
|
| 116 |
-
if response.status_code != 200:
|
| 117 |
-
return jsonify({'error': 'Failed to download image'}), 400
|
| 118 |
-
|
| 119 |
-
# Generate random text for the image name
|
| 120 |
-
random_text = generate_random_text()
|
| 121 |
-
image_name = f"{random_text}.png"
|
| 122 |
-
|
| 123 |
-
# Save the image
|
| 124 |
-
img = Image.open(BytesIO(response.content))
|
| 125 |
-
img.save(image_name, "PNG")
|
| 126 |
-
|
| 127 |
-
return jsonify({'success': f'Image uploaded as {image_name}'}), 200
|
| 128 |
-
except Exception as e:
|
| 129 |
-
return jsonify({'error': str(e)}), 500
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
apiKeys = [
|
| 135 |
"2021e94a-1385-4ddc-905b-c050cfb5af32",
|
| 136 |
"0bfe0e6d-6bf9-4984-ab07-3a9410a551ad",
|
|
@@ -163,6 +129,47 @@ def get_styles():
|
|
| 163 |
|
| 164 |
return jsonify({"styles": [style["name"] for style in styles]})
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
@app.route('/generate', methods=['POST'])
|
| 167 |
async def generate_image():
|
| 168 |
try:
|
|
@@ -219,7 +226,7 @@ async def generate_image():
|
|
| 219 |
|
| 220 |
# Return the success response with the generated image URL
|
| 221 |
return jsonify({
|
| 222 |
-
'
|
| 223 |
'url': discord_cdn_url
|
| 224 |
}), 200
|
| 225 |
else:
|
|
|
|
| 97 |
def page_not_found(e):
|
| 98 |
return send_from_directory(static_dir, '404.html'), 404
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
apiKeys = [
|
| 101 |
"2021e94a-1385-4ddc-905b-c050cfb5af32",
|
| 102 |
"0bfe0e6d-6bf9-4984-ab07-3a9410a551ad",
|
|
|
|
| 129 |
|
| 130 |
return jsonify({"styles": [style["name"] for style in styles]})
|
| 131 |
|
| 132 |
+
|
| 133 |
+
@app.route('/upload/image', methods=['GET'])
|
| 134 |
+
def upload_image():
|
| 135 |
+
try:
|
| 136 |
+
# Get the URL parameter
|
| 137 |
+
url = request.args.get('url')
|
| 138 |
+
|
| 139 |
+
if not url:
|
| 140 |
+
return jsonify({'error': 'URL parameter is missing'}), 400
|
| 141 |
+
|
| 142 |
+
# Download the image
|
| 143 |
+
response = requests.get(url)
|
| 144 |
+
if response.status_code != 200:
|
| 145 |
+
return jsonify({'error': 'Failed to download image'}), 400
|
| 146 |
+
|
| 147 |
+
image_name = f"image.png"
|
| 148 |
+
|
| 149 |
+
# Save the image
|
| 150 |
+
img = Image.open(BytesIO(response.content))
|
| 151 |
+
img.save(image_name, "PNG")
|
| 152 |
+
|
| 153 |
+
# Send the image to Discord
|
| 154 |
+
discord_webhook_url = "https://discord.com/api/webhooks/1217109788656406588/sh0LG9VH5wmxSWP8OBwfHxfbbMHleUX6eQ8-xULIEo5m4IASfNm7jCNrZFZZweKaNGTM"
|
| 155 |
+
files = {'file': open(image_name, 'rb')}
|
| 156 |
+
webhook_response = requests.post(discord_webhook_url, files=files)
|
| 157 |
+
|
| 158 |
+
# Get the uploaded image URL from Discord CDN
|
| 159 |
+
discord_cdn_url = webhook_response.json().get('attachments', [{}])[0].get('url')
|
| 160 |
+
|
| 161 |
+
# Delete the temporary image file
|
| 162 |
+
os.remove(image_name)
|
| 163 |
+
|
| 164 |
+
return jsonify({
|
| 165 |
+
'success': f'Image uploaded and sent to Discord',
|
| 166 |
+
'discord_cdn_url': discord_cdn_url
|
| 167 |
+
}), 200
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return jsonify({'error': str(e)}), 500
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
|
| 173 |
@app.route('/generate', methods=['POST'])
|
| 174 |
async def generate_image():
|
| 175 |
try:
|
|
|
|
| 226 |
|
| 227 |
# Return the success response with the generated image URL
|
| 228 |
return jsonify({
|
| 229 |
+
'status': 'success',
|
| 230 |
'url': discord_cdn_url
|
| 231 |
}), 200
|
| 232 |
else:
|