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