fix: change UI logging
Browse files
app.py
CHANGED
|
@@ -1,14 +1,24 @@
|
|
| 1 |
import asyncio
|
| 2 |
import base64
|
| 3 |
import io
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import httpx
|
| 8 |
from PIL import Image
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
async def generate_async(images, prompt, variation, size):
|
|
|
|
| 12 |
api_key = os.getenv("BYTEPLUS_API_KEY")
|
| 13 |
base_url = os.getenv("BYTEPLUS_URL", "").rstrip("/")
|
| 14 |
response_format = "b64_json"
|
|
@@ -26,7 +36,7 @@ async def generate_async(images, prompt, variation, size):
|
|
| 26 |
prefixed_b64 = f"data:image/png;base64,{encoded}"
|
| 27 |
images_input.append(prefixed_b64)
|
| 28 |
except Exception as e:
|
| 29 |
-
|
| 30 |
|
| 31 |
try:
|
| 32 |
model_name = "seedream-4-0-250828"
|
|
@@ -46,7 +56,7 @@ async def generate_async(images, prompt, variation, size):
|
|
| 46 |
"Content-Type": "application/json",
|
| 47 |
"Authorization": f"Bearer {api_key}",
|
| 48 |
}
|
| 49 |
-
|
| 50 |
async with httpx.AsyncClient(timeout=120) as client:
|
| 51 |
for _ in range(int(variation)):
|
| 52 |
response = await client.post(
|
|
@@ -66,22 +76,22 @@ async def generate_async(images, prompt, variation, size):
|
|
| 66 |
image = Image.open(io.BytesIO(image_data))
|
| 67 |
images_output.append(image)
|
| 68 |
except Exception as e:
|
| 69 |
-
|
| 70 |
|
| 71 |
return images_output
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
-
|
| 75 |
return []
|
| 76 |
|
| 77 |
|
| 78 |
# Wrapper because Gradio doesn't await async functions
|
| 79 |
-
def generate(images, prompt, variation, size):
|
| 80 |
-
return
|
| 81 |
|
| 82 |
|
| 83 |
# ------------------ Gradio UI ------------------ #
|
| 84 |
-
with gr.Blocks(theme=gr.themes.
|
| 85 |
gr.Markdown("## 🔥 Multi-API Image-to-Image Generator")
|
| 86 |
|
| 87 |
with gr.Row():
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import base64
|
| 3 |
import io
|
| 4 |
+
import logging
|
| 5 |
import os
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import httpx
|
| 9 |
from PIL import Image
|
| 10 |
|
| 11 |
+
# Configure once at startup
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
level=logging.INFO,
|
| 14 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
|
| 20 |
async def generate_async(images, prompt, variation, size):
|
| 21 |
+
logger.info("giving it to BytePlus...")
|
| 22 |
api_key = os.getenv("BYTEPLUS_API_KEY")
|
| 23 |
base_url = os.getenv("BYTEPLUS_URL", "").rstrip("/")
|
| 24 |
response_format = "b64_json"
|
|
|
|
| 36 |
prefixed_b64 = f"data:image/png;base64,{encoded}"
|
| 37 |
images_input.append(prefixed_b64)
|
| 38 |
except Exception as e:
|
| 39 |
+
logger.error(f"⚠️ Failed to process image {img.name}: {e}")
|
| 40 |
|
| 41 |
try:
|
| 42 |
model_name = "seedream-4-0-250828"
|
|
|
|
| 56 |
"Content-Type": "application/json",
|
| 57 |
"Authorization": f"Bearer {api_key}",
|
| 58 |
}
|
| 59 |
+
logger.info("Sending request to BytePlus...")
|
| 60 |
async with httpx.AsyncClient(timeout=120) as client:
|
| 61 |
for _ in range(int(variation)):
|
| 62 |
response = await client.post(
|
|
|
|
| 76 |
image = Image.open(io.BytesIO(image_data))
|
| 77 |
images_output.append(image)
|
| 78 |
except Exception as e:
|
| 79 |
+
logger.warning(f"⚠️ Failed to decode base64 image: {e}")
|
| 80 |
|
| 81 |
return images_output
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
logger.error(f"⚠️ Failed to process everything: {e}")
|
| 85 |
return []
|
| 86 |
|
| 87 |
|
| 88 |
# Wrapper because Gradio doesn't await async functions
|
| 89 |
+
async def generate(images, prompt, variation, size):
|
| 90 |
+
return await generate_async(images, prompt, variation, size)
|
| 91 |
|
| 92 |
|
| 93 |
# ------------------ Gradio UI ------------------ #
|
| 94 |
+
with gr.Blocks(theme=gr.themes.Glass()) as demo:
|
| 95 |
gr.Markdown("## 🔥 Multi-API Image-to-Image Generator")
|
| 96 |
|
| 97 |
with gr.Row():
|