Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
-
import
|
2 |
import requests
|
3 |
import io
|
4 |
-
import random
|
5 |
import os
|
6 |
-
import
|
7 |
from PIL import Image
|
8 |
from deep_translator import GoogleTranslator
|
9 |
-
import json
|
10 |
|
11 |
-
|
12 |
|
13 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
14 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
@@ -16,101 +14,127 @@ headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
16 |
timeout = 100
|
17 |
|
18 |
# Function to query the API and return the generated image
|
19 |
-
def query(prompt,
|
20 |
-
if
|
21 |
-
return None
|
22 |
|
23 |
key = random.randint(0, 999)
|
24 |
|
25 |
-
API_TOKEN = random.choice([os.getenv("HF_READ_TOKEN")])
|
26 |
-
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
27 |
-
|
28 |
# Translate the prompt from Russian to English if necessary
|
29 |
prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
|
30 |
-
print(f'
|
31 |
|
32 |
# Add some extra flair to the prompt
|
33 |
prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
34 |
-
print(f'
|
35 |
|
36 |
-
# Prepare the payload for the API call, including width and height
|
37 |
payload = {
|
38 |
"inputs": prompt,
|
39 |
-
"is_negative":
|
40 |
"steps": steps,
|
41 |
"cfg_scale": cfg_scale,
|
42 |
"seed": seed if seed != -1 else random.randint(1, 1000000000),
|
43 |
"strength": strength,
|
44 |
"parameters": {
|
45 |
-
"width": width,
|
46 |
-
"height": height
|
47 |
}
|
48 |
}
|
49 |
|
50 |
-
# Send the request to the API
|
51 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=timeout)
|
52 |
-
if response.status_code != 200:
|
53 |
-
print(f"Error: Failed to get image. Response status: {response.status_code}")
|
54 |
-
print(f"Response content: {response.text}")
|
55 |
-
if response.status_code == 503:
|
56 |
-
raise gr.Error(f"{response.status_code} : The model is being loaded")
|
57 |
-
raise gr.Error(f"{response.status_code}")
|
58 |
-
|
59 |
try:
|
60 |
-
|
|
|
|
|
|
|
61 |
image_bytes = response.content
|
62 |
image = Image.open(io.BytesIO(image_bytes))
|
63 |
-
|
64 |
-
return image
|
65 |
except Exception as e:
|
66 |
-
|
67 |
-
return None
|
68 |
-
|
69 |
-
# CSS to style the app
|
70 |
-
css = """
|
71 |
-
#app-container {
|
72 |
-
max-width: 800px;
|
73 |
-
margin-left: auto;
|
74 |
-
margin-right: auto;
|
75 |
-
}
|
76 |
-
"""
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
with gr.Row():
|
93 |
-
with gr.Accordion("Advanced Settings", open=False):
|
94 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
|
95 |
-
with gr.Row():
|
96 |
-
width = gr.Slider(label="Width", value=1024, minimum=64, maximum=1216, step=32)
|
97 |
-
height = gr.Slider(label="Height", value=1024, minimum=64, maximum=1216, step=32)
|
98 |
-
steps = gr.Slider(label="Sampling steps", value=35, minimum=1, maximum=100, step=1)
|
99 |
-
cfg = gr.Slider(label="CFG Scale", value=7, minimum=1, maximum=20, step=1)
|
100 |
-
strength = gr.Slider(label="Strength", value=0.7, minimum=0, maximum=1, step=0.001)
|
101 |
-
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1) # Setting the seed to -1 will make it random
|
102 |
-
method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
|
103 |
-
|
104 |
-
# Add a button to trigger the image generation
|
105 |
-
with gr.Row():
|
106 |
-
text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
|
107 |
-
|
108 |
-
# Image output area to display the generated image
|
109 |
-
with gr.Row():
|
110 |
-
image_output = gr.Image(type="pil", label="Image Output", elem_id="gallery")
|
111 |
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
app.
|
|
|
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")
|
|
|
14 |
timeout = 100
|
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 |
+
# Send the request to the API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Exception as e:
|
54 |
+
return None, f"Error when trying to open the image: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
# HTML template for the index page
|
57 |
+
index_html = """
|
58 |
+
<!DOCTYPE html>
|
59 |
+
<html lang="ja">
|
60 |
+
<head>
|
61 |
+
<meta charset="UTF-8">
|
62 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
63 |
+
<title>FLUX.1-Dev Image Generator</title>
|
64 |
+
</head>
|
65 |
+
<body>
|
66 |
+
<h1>FLUX.1-Dev Image Generator</h1>
|
67 |
+
<form action="/generate" method="get">
|
68 |
+
<label for="prompt">Prompt:</label>
|
69 |
+
<input type="text" id="prompt" name="prompt" placeholder="Enter your prompt" required><br><br>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
<label for="negative_prompt">Negative Prompt:</label>
|
72 |
+
<input type="text" id="negative_prompt" name="negative_prompt" value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos"><br><br>
|
73 |
+
|
74 |
+
<label for="width">Width:</label>
|
75 |
+
<input type="number" id="width" name="width" value="1024"><br><br>
|
76 |
+
|
77 |
+
<label for="height">Height:</label>
|
78 |
+
<input type="number" id="height" name="height" value="1024"><br><br>
|
79 |
+
|
80 |
+
<label for="steps">Sampling Steps:</label>
|
81 |
+
<input type="number" id="steps" name="steps" value="35"><br><br>
|
82 |
+
|
83 |
+
<label for="cfgs">CFG Scale:</label>
|
84 |
+
<input type="number" id="cfgs" name="cfgs" value="7"><br><br>
|
85 |
+
|
86 |
+
<label for="sampler">Sampling Method:</label>
|
87 |
+
<select id="sampler" name="sampler">
|
88 |
+
<option value="DPM++ 2M Karras">DPM++ 2M Karras</option>
|
89 |
+
<option value="DPM++ SDE Karras">DPM++ SDE Karras</option>
|
90 |
+
<option value="Euler">Euler</option>
|
91 |
+
<option value="Euler a">Euler a</option>
|
92 |
+
<option value="Heun">Heun</option>
|
93 |
+
<option value="DDIM">DDIM</option>
|
94 |
+
</select><br><br>
|
95 |
+
|
96 |
+
<label for="strength">Strength:</label>
|
97 |
+
<input type="number" id="strength" name="strength" value="0.7" step="0.01" min="0" max="1"><br><br>
|
98 |
+
|
99 |
+
<label for="seed">Seed:</label>
|
100 |
+
<input type="number" id="seed" name="seed" value="-1" step="1"><br><br>
|
101 |
+
|
102 |
+
<button type="submit">Generate Image</button>
|
103 |
+
</form>
|
104 |
+
</body>
|
105 |
+
</html>
|
106 |
+
"""
|
107 |
+
|
108 |
+
@app.route('/')
|
109 |
+
def index():
|
110 |
+
return render_template_string(index_html)
|
111 |
+
|
112 |
+
@app.route('/generate', methods=['GET'])
|
113 |
+
def generate_image():
|
114 |
+
# Retrieve query parameters
|
115 |
+
prompt = request.args.get("prompt", "")
|
116 |
+
negative_prompt = request.args.get("negative_prompt", "")
|
117 |
+
steps = int(request.args.get("steps", 35))
|
118 |
+
cfg_scale = float(request.args.get("cfgs", 7))
|
119 |
+
sampler = request.args.get("sampler", "DPM++ 2M Karras")
|
120 |
+
seed = int(request.args.get("seed", -1))
|
121 |
+
strength = float(request.args.get("strength", 0.7))
|
122 |
+
width = int(request.args.get("width", 1024))
|
123 |
+
height = int(request.args.get("height", 1024))
|
124 |
+
|
125 |
+
# Call the query function to generate the image
|
126 |
+
image, error = query(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height)
|
127 |
+
|
128 |
+
if error:
|
129 |
+
return jsonify({"error": error}), 500
|
130 |
+
|
131 |
+
# Save the image to a buffer
|
132 |
+
img_io = io.BytesIO()
|
133 |
+
image.save(img_io, 'PNG')
|
134 |
+
img_io.seek(0)
|
135 |
+
|
136 |
+
# Return the image
|
137 |
+
return send_file(img_io, mimetype='image/png')
|
138 |
|
139 |
+
if __name__ == '__main__':
|
140 |
+
app.run(host='0.0.0.0', port=7860)
|