Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
import io
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -11,6 +12,8 @@ API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
|
|
| 11 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 12 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def query(prompt, is_negative=False, steps=1, cfg_scale=6, seed=None):
|
| 16 |
payload = {
|
|
@@ -26,6 +29,14 @@ def query(prompt, is_negative=False, steps=1, cfg_scale=6, seed=None):
|
|
| 26 |
return image
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@app.route("/generate", methods=["POST"])
|
| 30 |
def generate():
|
| 31 |
try:
|
|
@@ -35,15 +46,11 @@ def generate():
|
|
| 35 |
is_negative = True if negative_prompt else False
|
| 36 |
|
| 37 |
image = query(prompt, is_negative=is_negative)
|
| 38 |
-
|
| 39 |
-
# Convert PIL Image to bytes
|
| 40 |
-
img_byte_array = io.BytesIO()
|
| 41 |
-
image.save(img_byte_array, format='PNG')
|
| 42 |
-
img_byte_array = img_byte_array.getvalue()
|
| 43 |
|
| 44 |
response = {
|
| 45 |
"success": True,
|
| 46 |
-
"
|
| 47 |
}
|
| 48 |
except Exception as e:
|
| 49 |
response = {
|
|
@@ -54,5 +61,13 @@ def generate():
|
|
| 54 |
return jsonify(response)
|
| 55 |
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
import requests
|
| 3 |
import io
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
from PIL import Image
|
| 7 |
+
from datetime import datetime
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
|
|
| 12 |
API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
|
| 13 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 14 |
|
| 15 |
+
TEMP_DIR = "temp"
|
| 16 |
+
|
| 17 |
|
| 18 |
def query(prompt, is_negative=False, steps=1, cfg_scale=6, seed=None):
|
| 19 |
payload = {
|
|
|
|
| 29 |
return image
|
| 30 |
|
| 31 |
|
| 32 |
+
def save_image(image, prompt):
|
| 33 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 34 |
+
filename = f"{prompt}-{random.randint(1, 100000)}-{timestamp}.png"
|
| 35 |
+
filepath = os.path.join(TEMP_DIR, filename)
|
| 36 |
+
image.save(filepath, format='PNG')
|
| 37 |
+
return filename
|
| 38 |
+
|
| 39 |
+
|
| 40 |
@app.route("/generate", methods=["POST"])
|
| 41 |
def generate():
|
| 42 |
try:
|
|
|
|
| 46 |
is_negative = True if negative_prompt else False
|
| 47 |
|
| 48 |
image = query(prompt, is_negative=is_negative)
|
| 49 |
+
filename = save_image(image, prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
response = {
|
| 52 |
"success": True,
|
| 53 |
+
"image_filename": filename
|
| 54 |
}
|
| 55 |
except Exception as e:
|
| 56 |
response = {
|
|
|
|
| 61 |
return jsonify(response)
|
| 62 |
|
| 63 |
|
| 64 |
+
@app.route("/temp/<filename>")
|
| 65 |
+
def show_image(filename):
|
| 66 |
+
return send_from_directory(TEMP_DIR, filename)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
if not os.path.exists(TEMP_DIR):
|
| 71 |
+
os.makedirs(TEMP_DIR)
|
| 72 |
+
|
| 73 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|