Spaces:
Running
Running
File size: 4,622 Bytes
872c6d4 1eae35f 6a3ef3a 1eae35f 435f67e 1eae35f 435f67e 1eae35f 435f67e 1eae35f 435f67e 1eae35f 1fb1361 1eae35f 435f67e 1eae35f 435f67e a2384b5 e58623d ef72775 e58623d ef72775 1eae35f e58623d ef72775 1eae35f ef72775 a2384b5 872c6d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
from flask import Flask, request, jsonify, send_file, render_template_string
import requests
import io
import random
from PIL import Image
from deep_translator import GoogleTranslator
app = Flask(__name__)
API_URL = "https://api-inference.huggingface.co/models/Ojimi/anime-kawai-diffusion"
timeout = 3000 # Set timeout to 300 seconds
def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024):
if not prompt:
return None, "Prompt is required"
# Translate prompt from Russian to English if needed
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
print(f'Translated prompt: {prompt}')
# Enhance the prompt
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
print(f'Final prompt: {prompt}')
payload = {
"inputs": prompt,
"is_negative": False,
"steps": steps,
"cfg_scale": cfg_scale,
"seed": seed if seed != -1 else random.randint(1, 1000000000),
"strength": strength,
"parameters": {
"width": width,
"height": height
}
}
for attempt in range(3):
try:
response = requests.post(API_URL, json=payload, timeout=timeout)
if response.status_code != 200:
return None, f"Error: Failed to get image. Status code: {response.status_code}, Details: {response.text}"
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image, None
except requests.exceptions.Timeout:
if attempt < 2:
print("Timeout occurred, retrying...")
continue
return None, "Error: The request timed out. Please try again."
except requests.exceptions.RequestException as e:
return None, f"Request Exception: {str(e)}"
except Exception as e:
return None, f"Error when trying to open the image: {e}"
@app.route('/')
def index():
return render_template_string("""
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Image Generator</title>
</head>
<body>
<h1>Kawaii Diffusion Image Generator</h1>
<form action="/generate" method="get">
<label for="prompt">Prompt:</label><br>
<input type="text" id="prompt" name="prompt" required><br>
<label for="negative_prompt">Negative Prompt:</label><br>
<input type="text" id="negative_prompt" name="negative_prompt"><br>
<label for="steps">Steps:</label><br>
<input type="number" id="steps" name="steps" value="35"><br>
<label for="cfg_scale">CFG Scale:</label><br>
<input type="number" step="0.1" id="cfg_scale" name="cfg_scale" value="7"><br>
<label for="strength">Strength:</label><br>
<input type="number" step="0.1" id="strength" name="strength" value="0.7"><br>
<label for="seed">Seed:</label><br>
<input type="number" id="seed" name="seed" value="-1"><br>
<label for="width">Width:</label><br>
<input type="number" id="width" name="width" value="1024"><br>
<label for="height">Height:</label><br>
<input type="number" id="height" name="height" value="1024"><br><br>
<input type="submit" value="Generate">
</form>
</body>
</html>
""")
@app.route('/generate', methods=['GET'])
def generate_image():
prompt = request.args.get("prompt", "")
negative_prompt = request.args.get("negative_prompt", "bad hands, low quality, low quality, unnatural, dirty eyes, distorted eyes, low quality eyes, distorted arms, distorted legs, distorted face, 3 legs, 3 hands, anatomically Bad, 6 fingers, 7 fingers, less fingers, more fingers,")
steps = int(request.args.get("steps", 35))
cfg_scale = float(request.args.get("cfg_scale", 7))
strength = float(request.args.get("strength", 0.7))
seed = int(request.args.get("seed", -1))
width = int(request.args.get("width", 1024))
height = int(request.args.get("height", 1024))
image, error = query(prompt, negative_prompt, steps, cfg_scale, seed=seed, strength=strength, width=width, height=height)
if error:
return jsonify({"error": error}), 400
img_bytes = io.BytesIO()
image.save(img_bytes, format='PNG')
img_bytes.seek(0)
return send_file(img_bytes, mimetype='image/png')
if __name__ == "__main__":
app.run(host='0.0.0.0', port=7860)
|