mirror of https://github.com/vladmandic/automatic
genai exception handling and lint all
Signed-off-by: vladmandic <mandic00@live.com>pull/4493/head
parent
4c35d3887e
commit
dde91321b9
|
|
@ -72,6 +72,7 @@ select = [
|
|||
ignore = [
|
||||
"B006", # Do not use mutable data structures for argument defaults
|
||||
"B008", # Do not perform function call in argument defaults
|
||||
"B905", # Strict zip() usage
|
||||
"C420", # Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
|
||||
"C408", # Unnecessary `dict` call
|
||||
"I001", # Import block is un-sorted or un-formatted
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ const generateForever = (genbuttonid) => {
|
|||
busy = outerButton?.classList.contains('generate') && outerButton?.classList.contains('active');
|
||||
}
|
||||
return busy;
|
||||
}
|
||||
};
|
||||
log('generateForever: start');
|
||||
if (!isBusy()) genbutton.click();
|
||||
window.generateOnRepeatInterval = setInterval(() => {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ function controlInputMode(inputMode, ...args) {
|
|||
if (!tab) return ['Image', ...args];
|
||||
// let inputTab = tab.innerText;
|
||||
const tabs = Array.from(gradioApp().querySelectorAll('#control-tab-input button'));
|
||||
const tabIdx = tabs.findIndex((btn) => btn.classList.contains('selected'))
|
||||
const tabIdx = tabs.findIndex((btn) => btn.classList.contains('selected'));
|
||||
const tabNames = ['Image', 'Video', 'Batch', 'Folder'];
|
||||
let inputTab = tabNames[tabIdx] || 'Image';
|
||||
log('controlInputMode', { mode: inputMode, tab: inputTab, kanvas: typeof Kanvas });
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class NetworkOnDisk:
|
|||
if self.filename is not None:
|
||||
fn = os.path.splitext(self.filename)[0] + '.txt'
|
||||
if os.path.exists(fn):
|
||||
with open(fn, "r") as file:
|
||||
with open(fn, "r", encoding="utf-8") as file:
|
||||
return file.read()
|
||||
return None
|
||||
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ class YoloRestorer(Detailer):
|
|||
try:
|
||||
seg = (255 * seg).astype(np.uint8)
|
||||
seg = Image.fromarray(seg).resize(image.size).convert('L')
|
||||
except:
|
||||
except Exception:
|
||||
seg = None
|
||||
cls = int(cls)
|
||||
label = prediction.names[cls] if cls < len(prediction.names) else f'cls{cls}'
|
||||
|
|
|
|||
|
|
@ -136,11 +136,12 @@ class GoogleVeoVideoPipeline():
|
|||
log.error(f'Cloud video: model="{self.model}" {operation} {e}')
|
||||
return None
|
||||
|
||||
if operation is None or operation.response is None or operation.response.generated_videos is None or len(operation.response.generated_videos) == 0:
|
||||
try:
|
||||
response: genai.types.GeneratedVideo = operation.response.generated_videos[0]
|
||||
except Exception:
|
||||
log.error(f'Cloud video: model="{self.model}" no response {operation}')
|
||||
return None
|
||||
try:
|
||||
response: genai.types.GeneratedVideo = operation.response.generated_videos[0]
|
||||
self.client.files.download(file=response.video)
|
||||
video_bytes = response.video.video_bytes
|
||||
return { 'bytes': video_bytes, 'images': [] }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import io
|
||||
import os
|
||||
import time
|
||||
from PIL import Image
|
||||
from installer import install, reload, log
|
||||
|
||||
|
|
@ -110,10 +111,17 @@ class GoogleNanoBananaPipeline():
|
|||
# log.debug(f'Cloud: config={self.config}')
|
||||
|
||||
try:
|
||||
t0 = time.time()
|
||||
if image is not None:
|
||||
response = self.img2img(prompt, image)
|
||||
else:
|
||||
response = self.txt2img(prompt)
|
||||
t1 = time.time()
|
||||
try:
|
||||
tokens = response.usage_metadata.total_token_count
|
||||
except Exception:
|
||||
tokens = 0
|
||||
log.debug(f'Cloud: model="{self.model}" tokens={tokens} time={(t1 - t0):.2f}')
|
||||
except Exception as e:
|
||||
log.error(f'Cloud: model="{self.model}" {e}')
|
||||
return None
|
||||
|
|
@ -121,10 +129,16 @@ class GoogleNanoBananaPipeline():
|
|||
image = None
|
||||
if getattr(response, 'prompt_feedback', None) is not None:
|
||||
log.error(f'Cloud: model="{self.model}" {response.prompt_feedback}')
|
||||
if not hasattr(response, 'candidates') or (response.candidates is None) or (len(response.candidates) == 0) or (response.candidates[0].content is None) or (len(response.candidates[0].content.parts) == 0):
|
||||
|
||||
parts = []
|
||||
try:
|
||||
for candidate in response.candidates:
|
||||
parts.extend(candidate.content.parts)
|
||||
except Exception:
|
||||
log.error(f'Cloud: model="{self.model}" no images received')
|
||||
return None
|
||||
for part in response.candidates[0].content.parts:
|
||||
|
||||
for part in parts:
|
||||
if part.inline_data is not None:
|
||||
image = Image.open(io.BytesIO(part.inline_data.data))
|
||||
return image
|
||||
|
|
|
|||
Loading…
Reference in New Issue