Spaces:
Paused
Paused
Kastg
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import time
|
|
| 5 |
import datetime
|
| 6 |
import json
|
| 7 |
import subprocess
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
@@ -89,6 +91,37 @@ def status():
|
|
| 89 |
@app.errorhandler(404)
|
| 90 |
def page_not_found(e):
|
| 91 |
return send_from_directory(static_dir, '404.html'), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 5 |
import datetime
|
| 6 |
import json
|
| 7 |
import subprocess
|
| 8 |
+
import string
|
| 9 |
+
import random
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
|
|
|
| 91 |
@app.errorhandler(404)
|
| 92 |
def page_not_found(e):
|
| 93 |
return send_from_directory(static_dir, '404.html'), 404
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def generate_random_text(length=10):
|
| 97 |
+
letters = string.ascii_lowercase
|
| 98 |
+
return ''.join(random.choice(letters) for _ in range(length))
|
| 99 |
+
|
| 100 |
+
@app.route('/upload', methods=['GET'])
|
| 101 |
+
def upload_image():
|
| 102 |
+
try:
|
| 103 |
+
# Get the URL parameter
|
| 104 |
+
url = request.args.get('url')
|
| 105 |
+
|
| 106 |
+
if not url:
|
| 107 |
+
return jsonify({'error': 'URL parameter is missing'}), 400
|
| 108 |
+
|
| 109 |
+
# Download the image
|
| 110 |
+
response = requests.get(url)
|
| 111 |
+
if response.status_code != 200:
|
| 112 |
+
return jsonify({'error': 'Failed to download image'}), 400
|
| 113 |
+
|
| 114 |
+
# Generate random text for the image name
|
| 115 |
+
random_text = generate_random_text()
|
| 116 |
+
image_name = f"{random_text}.png"
|
| 117 |
+
|
| 118 |
+
# Save the image
|
| 119 |
+
img = Image.open(BytesIO(response.content))
|
| 120 |
+
img.save(image_name, "PNG")
|
| 121 |
+
|
| 122 |
+
return jsonify({'success': f'Image uploaded as {image_name}'}), 200
|
| 123 |
+
except Exception as e:
|
| 124 |
+
return jsonify({'error': str(e)}), 500
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
app.run(host="0.0.0.0", port=7860, debug=True)
|