Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="ja">
|
| 3 |
<head>
|
|
@@ -215,3 +279,37 @@
|
|
| 215 |
</script>
|
| 216 |
</body>
|
| 217 |
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_file, render_template_string
|
| 2 |
+
import requests
|
| 3 |
+
import io
|
| 4 |
+
import os
|
| 5 |
+
import random
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from deep_translator import GoogleTranslator
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
| 12 |
+
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
| 13 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 14 |
+
timeout = 300 # タイムアウトを300秒に設定
|
| 15 |
+
|
| 16 |
+
# Function to query the API and return the generated image
|
| 17 |
+
def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
|
| 18 |
+
if not prompt:
|
| 19 |
+
return None, "Prompt is required"
|
| 20 |
+
|
| 21 |
+
key = random.randint(0, 999)
|
| 22 |
+
|
| 23 |
+
# Translate the prompt from Russian to English if necessary
|
| 24 |
+
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
| 25 |
+
print(f'Generation {key} translation: {prompt}')
|
| 26 |
+
|
| 27 |
+
# Add some extra flair to the prompt
|
| 28 |
+
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 29 |
+
print(f'Generation {key}: {prompt}')
|
| 30 |
+
|
| 31 |
+
payload = {
|
| 32 |
+
"inputs": prompt,
|
| 33 |
+
"is_negative": False,
|
| 34 |
+
"steps": steps,
|
| 35 |
+
"cfg_scale": cfg_scale,
|
| 36 |
+
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
| 37 |
+
"strength": strength,
|
| 38 |
+
"parameters": {
|
| 39 |
+
"width": width,
|
| 40 |
+
"height": height
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
for attempt in range(3): # 最大3回の再試行
|
| 45 |
+
try:
|
| 46 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
| 47 |
+
if response.status_code != 200:
|
| 48 |
+
return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}"
|
| 49 |
+
|
| 50 |
+
image_bytes = response.content
|
| 51 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 52 |
+
return image, None
|
| 53 |
+
except requests.exceptions.Timeout:
|
| 54 |
+
if attempt < 2: # 最後の試行でない場合は再試行
|
| 55 |
+
print("Timeout occurred, retrying...")
|
| 56 |
+
continue
|
| 57 |
+
return None, "Error: The request timed out. Please try again."
|
| 58 |
+
except requests.exceptions.RequestException as e:
|
| 59 |
+
return None, f"Request Exception: {str(e)}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return None, f"Error when trying to open the image: {e}"
|
| 62 |
+
|
| 63 |
+
# HTML template for the index page
|
| 64 |
+
index_html = """
|
| 65 |
<!DOCTYPE html>
|
| 66 |
<html lang="ja">
|
| 67 |
<head>
|
|
|
|
| 279 |
</script>
|
| 280 |
</body>
|
| 281 |
</html>
|
| 282 |
+
|
| 283 |
+
"""
|
| 284 |
+
|
| 285 |
+
@app.route('/')
|
| 286 |
+
def index():
|
| 287 |
+
return render_template_string(index_html)
|
| 288 |
+
|
| 289 |
+
@app.route('/generate', methods=['GET'])
|
| 290 |
+
def generate_image():
|
| 291 |
+
# Retrieve query parameters
|
| 292 |
+
prompt = request.args.get("prompt", "")
|
| 293 |
+
negative_prompt = request.args.get("negative_prompt", "")
|
| 294 |
+
steps = int(request.args.get("steps", 35))
|
| 295 |
+
cfg_scale = float(request.args.get("cfgs", 7))
|
| 296 |
+
sampler = request.args.get("sampler", "DPM++ 2M Karras")
|
| 297 |
+
strength = float(request.args.get("strength", 0.7))
|
| 298 |
+
seed = int(request.args.get("seed", -1))
|
| 299 |
+
width = int(request.args.get("width", 1024))
|
| 300 |
+
height = int(request.args.get("height", 1024))
|
| 301 |
+
|
| 302 |
+
# Call the query function to generate the image
|
| 303 |
+
image, error = query(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height)
|
| 304 |
+
|
| 305 |
+
if error:
|
| 306 |
+
return jsonify({"error": error}), 400
|
| 307 |
+
|
| 308 |
+
# Save the image to a BytesIO object and return it
|
| 309 |
+
img_bytes = io.BytesIO()
|
| 310 |
+
image.save(img_bytes, format='PNG')
|
| 311 |
+
img_bytes.seek(0)
|
| 312 |
+
return send_file(img_bytes, mimetype='image/png')
|
| 313 |
+
|
| 314 |
+
if __name__ == "__main__":
|
| 315 |
+
app.run(host='0.0.0.0', port=7860)
|