Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ app = Flask(__name__)
|
|
| 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 =
|
| 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):
|
|
@@ -41,17 +41,24 @@ def query(prompt, negative_prompt="", steps=35, cfg_scale=7, sampler="DPM++ 2M K
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# HTML template for the index page
|
| 57 |
index_html = """
|
|
@@ -141,7 +148,7 @@ def generate_image():
|
|
| 141 |
image_filename = f"generated_image_{random.randint(1, 10000)}.png"
|
| 142 |
|
| 143 |
# Save the image to a file
|
| 144 |
-
image.save(image_filename)
|
| 145 |
|
| 146 |
# Return the index page with the generated image URL
|
| 147 |
image_url = f"/static/{image_filename}"
|
|
|
|
| 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):
|
|
|
|
| 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 = """
|
|
|
|
| 148 |
image_filename = f"generated_image_{random.randint(1, 10000)}.png"
|
| 149 |
|
| 150 |
# Save the image to a file
|
| 151 |
+
image.save(os.path.join('static', image_filename))
|
| 152 |
|
| 153 |
# Return the index page with the generated image URL
|
| 154 |
image_url = f"/static/{image_filename}"
|