Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,28 +5,24 @@ from PIL import Image
|
|
5 |
from deep_translator import GoogleTranslator
|
6 |
import aiohttp
|
7 |
from quart import Quart, request, jsonify, send_file, render_template_string
|
8 |
-
from
|
9 |
|
10 |
# アプリケーションの設定
|
11 |
app = Quart(__name__)
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
15 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
16 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
17 |
timeout = 50000 # タイムアウトを300秒に設定
|
18 |
|
19 |
-
# キャッシュの設定
|
20 |
-
def get_cached_image(prompt):
|
21 |
-
cached = cache.get(prompt)
|
22 |
-
if cached is not None:
|
23 |
-
return cached
|
24 |
-
return None
|
25 |
-
|
26 |
-
def set_cache_image(prompt, image):
|
27 |
-
cache.set(prompt, image, timeout=60*60*24) # キャッシュを24時間保存
|
28 |
-
|
29 |
-
|
30 |
# 非同期APIリクエストの実行関数
|
31 |
async def query_async(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024, num_inference_steps=30, guidance_scale=7.5, top_k=50, top_p=0.9, eta=0.1):
|
32 |
if not prompt:
|
@@ -127,9 +123,9 @@ async def generate_image():
|
|
127 |
eta = float(request.args.get("eta", 0.1))
|
128 |
|
129 |
# キャッシュを確認
|
130 |
-
cached_image =
|
131 |
if cached_image:
|
132 |
-
return await send_file(cached_image, mimetype='image/png')
|
133 |
|
134 |
image, error = await query_async(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height, num_inference_steps, guidance_scale, top_k, top_p, eta)
|
135 |
|
@@ -141,7 +137,7 @@ async def generate_image():
|
|
141 |
img_bytes.seek(0)
|
142 |
|
143 |
# 画像をキャッシュに保存
|
144 |
-
|
145 |
|
146 |
return await send_file(img_bytes, mimetype='image/png')
|
147 |
|
|
|
5 |
from deep_translator import GoogleTranslator
|
6 |
import aiohttp
|
7 |
from quart import Quart, request, jsonify, send_file, render_template_string
|
8 |
+
from flask_caching import Cache # flask-cachingを使用
|
9 |
|
10 |
# アプリケーションの設定
|
11 |
app = Quart(__name__)
|
12 |
+
|
13 |
+
# キャッシュの設定
|
14 |
+
cache_config = {
|
15 |
+
"CACHE_TYPE": "SimpleCache", # メモリベースのシンプルなキャッシュ
|
16 |
+
"CACHE_DEFAULT_TIMEOUT": 60 * 60 * 24 # 24時間
|
17 |
+
}
|
18 |
+
app.config.from_mapping(cache_config)
|
19 |
+
cache = Cache(app)
|
20 |
|
21 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
22 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
23 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
24 |
timeout = 50000 # タイムアウトを300秒に設定
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# 非同期APIリクエストの実行関数
|
27 |
async def query_async(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M Karras", seed=-1, strength=0.7, width=1024, height=1024, num_inference_steps=30, guidance_scale=7.5, top_k=50, top_p=0.9, eta=0.1):
|
28 |
if not prompt:
|
|
|
123 |
eta = float(request.args.get("eta", 0.1))
|
124 |
|
125 |
# キャッシュを確認
|
126 |
+
cached_image = cache.get(prompt)
|
127 |
if cached_image:
|
128 |
+
return await send_file(io.BytesIO(cached_image), mimetype='image/png')
|
129 |
|
130 |
image, error = await query_async(prompt, negative_prompt, steps, cfg_scale, sampler, seed, strength, width, height, num_inference_steps, guidance_scale, top_k, top_p, eta)
|
131 |
|
|
|
137 |
img_bytes.seek(0)
|
138 |
|
139 |
# 画像をキャッシュに保存
|
140 |
+
cache.set(prompt, img_bytes.getvalue())
|
141 |
|
142 |
return await send_file(img_bytes, mimetype='image/png')
|
143 |
|