23984
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,43 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
try:
|
14 |
-
m = gr.load(f'models/{model}')
|
15 |
-
except Exception as error:
|
16 |
-
m = gr.Interface(lambda txt: None, ['text'], ['image'])
|
17 |
-
models_load.update({model: m})
|
18 |
-
|
19 |
-
|
20 |
-
load_fn(models)
|
21 |
-
|
22 |
-
|
23 |
-
num_models = 6
|
24 |
-
default_models = models[:num_models]
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
def extend_choices(choices):
|
29 |
-
return choices + (num_models - len(choices)) * ['NA']
|
30 |
-
|
31 |
-
|
32 |
-
def update_imgbox(choices):
|
33 |
-
choices_plus = extend_choices(choices)
|
34 |
-
return [gr.Image(None, label = m, visible = (m != 'NA')) for m in choices_plus]
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
txt_input = gr.Textbox(label = 'Your prompt:', lines=4).style(container=False,min_width=1200)
|
48 |
-
gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
|
49 |
-
stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
|
50 |
-
gen_button.click(lambda s: gr.update(interactive = True), None, stop_button)
|
51 |
-
gr.HTML(
|
52 |
-
"""
|
53 |
-
<div style="text-align: center; max-width: 1200px; margin: 0 auto;">
|
54 |
-
<div>
|
55 |
-
<body>
|
56 |
-
<div class="center"><p style="margin-bottom: 10px; color: #000000;">Scroll down to see more images and select models.</p>
|
57 |
-
</div>
|
58 |
-
</body>
|
59 |
-
</div>
|
60 |
-
</div>
|
61 |
-
"""
|
62 |
-
)
|
63 |
-
with gr.Row():
|
64 |
-
output = [gr.Image(label = m, min_width=480) for m in default_models]
|
65 |
-
current_models = [gr.Textbox(m, visible = False) for m in default_models]
|
66 |
-
|
67 |
-
for m, o in zip(current_models, output):
|
68 |
-
gen_event = gen_button.click(gen_fn, [m, txt_input], o)
|
69 |
-
stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
|
70 |
-
with gr.Accordion('Model selection'):
|
71 |
-
model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, multiselect = True, max_choices = num_models, interactive = True, filterable = False)
|
72 |
-
model_choice.change(update_imgbox, model_choice, output)
|
73 |
-
model_choice.change(extend_choices, model_choice, current_models)
|
74 |
-
with gr.Row():
|
75 |
-
gr.HTML(
|
76 |
-
"""
|
77 |
-
<div class="footer">
|
78 |
-
<p> Based on the <a href="https://huggingface.co/spaces/derwahnsinn/TestGen">TestGen</a> Space by derwahnsinn, the <a href="https://huggingface.co/spaces/RdnUser77/SpacIO_v1">SpacIO</a> Space by RdnUser77 and Omnibus's Maximum Multiplier!
|
79 |
-
</p>
|
80 |
-
"""
|
81 |
-
)
|
82 |
|
83 |
-
|
84 |
-
|
|
|
1 |
+
import os
|
2 |
+
os.system("pip install flask")
|
3 |
+
|
4 |
+
from flask import Flask, request, send_file, abort
|
5 |
+
from io import BytesIO
|
6 |
+
from PIL import Image
|
7 |
+
import random
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# モデルのロードをシミュレートするためのスタブ
|
12 |
+
def load_model(model_name):
|
13 |
+
# モデルをロードするロジックをここに追加
|
14 |
+
# ここでは、PILで空の画像を生成する簡易的な例を示します
|
15 |
+
def model(prompt):
|
16 |
+
img = Image.new('RGB', (256, 256), color = (73, 109, 137))
|
17 |
+
return img
|
18 |
+
return model
|
19 |
+
|
20 |
+
# モデルのロード
|
21 |
+
models_load = {f'model_{i}': load_model(f'model_{i}') for i in range(1, 7)}
|
22 |
+
|
23 |
+
@app.route('/generate_image', methods=['GET'])
|
24 |
+
def generate_image():
|
25 |
+
prompt = request.args.get('prompt')
|
26 |
+
model_str = request.args.get('model')
|
27 |
|
28 |
+
if model_str not in models_load:
|
29 |
+
abort(404, description="Model not found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
model = models_load[model_str]
|
32 |
+
noise = str(random.randint(0, 99999999999))
|
33 |
+
image = model(f'{prompt} {noise}')
|
34 |
+
|
35 |
+
# 画像をバイナリで保存し、レスポンスとして返す
|
36 |
+
img_bytes = BytesIO()
|
37 |
+
image.save(img_bytes, format='PNG')
|
38 |
+
img_bytes.seek(0)
|
39 |
+
|
40 |
+
return send_file(img_bytes, mimetype='image/png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
if __name__ == '__main__':
|
43 |
+
app.run(host='0.0.0.0', port=5000)
|