Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ import random
|
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
from datetime import datetime
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
@@ -36,7 +38,23 @@ def save_image(image, prompt):
|
|
| 36 |
image.save(filepath, format='PNG')
|
| 37 |
return filename
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
@app.route("/generate", methods=["POST"])
|
| 41 |
def generate():
|
| 42 |
try:
|
|
|
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
from datetime import datetime
|
| 8 |
+
import string
|
| 9 |
+
import re
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
| 12 |
|
|
|
|
| 38 |
image.save(filepath, format='PNG')
|
| 39 |
return filename
|
| 40 |
|
| 41 |
+
def sanitize_filename(filename):
|
| 42 |
+
# Replace spaces with underscores
|
| 43 |
+
filename = filename.replace(" ", "_")
|
| 44 |
|
| 45 |
+
# Remove special characters
|
| 46 |
+
sanitized_filename = re.sub(r'[^\w\s-]', '', filename)
|
| 47 |
+
sanitized_filename = re.sub(r'[-\s]+', '-', sanitized_filename)
|
| 48 |
+
|
| 49 |
+
return sanitized_filename
|
| 50 |
+
|
| 51 |
+
def save_image(image, prompt):
|
| 52 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 53 |
+
filename = f"{sanitize_filename(prompt)}-{random.randint(1, 100000)}-{timestamp}.png"
|
| 54 |
+
filepath = os.path.join(TEMP_DIR, filename)
|
| 55 |
+
image.save(filepath, format='PNG')
|
| 56 |
+
return filename
|
| 57 |
+
|
| 58 |
@app.route("/generate", methods=["POST"])
|
| 59 |
def generate():
|
| 60 |
try:
|