imaginer: format

This commit is contained in:
0xMRTT 2023-05-02 00:44:07 +02:00
parent da7b824284
commit a7605f6199
Signed by: 0xMRTT
GPG Key ID: 19C1449A774028BD
2 changed files with 78 additions and 57 deletions

View File

@ -20,8 +20,8 @@
import sys import sys
import gi import gi
gi.require_version('Gtk', '4.0') gi.require_version("Gtk", "4.0")
gi.require_version('Adw', '1') gi.require_version("Adw", "1")
from gi.repository import Gtk, Gio, Adw, GLib, Pango from gi.repository import Gtk, Gio, Adw, GLib, Pango
from .window import ImaginerWindow from .window import ImaginerWindow
@ -41,24 +41,27 @@ import re
import unicodedata import unicodedata
from time import gmtime, strftime from time import gmtime, strftime
class ImaginerApplication(Adw.Application): class ImaginerApplication(Adw.Application):
"""The main application singleton class.""" """The main application singleton class."""
def __init__(self): def __init__(self):
super().__init__(application_id='io.github.ImaginerApp.Imaginer', super().__init__(
flags=Gio.ApplicationFlags.DEFAULT_FLAGS) application_id="io.github.ImaginerApp.Imaginer",
self.create_action('quit', self.on_quit_action, ['<primary>q']) flags=Gio.ApplicationFlags.DEFAULT_FLAGS,
self.create_action('about', self.on_about_action) )
self.create_action('preferences', self.on_preferences_action) self.create_action("quit", self.on_quit_action, ["<primary>q"])
self.create_action('get_started', self.on_get_started_action) self.create_action("about", self.on_about_action)
self.create_action('imagine', self.on_imagine_action, ['<primary>Return']) self.create_action("preferences", self.on_preferences_action)
self.create_action('choose_output', self.on_file_chooser, ['<primary>s']) self.create_action("get_started", self.on_get_started_action)
self.create_action('new_window', self.on_new_window_action, ['<primary>n']) self.create_action("imagine", self.on_imagine_action, ["<primary>Return"])
self.create_action("choose_output", self.on_file_chooser, ["<primary>s"])
self.create_action("new_window", self.on_new_window_action, ["<primary>n"])
def on_quit_action(self, action, _): def on_quit_action(self, action, _):
"""Callback for the app.quit action.""" """Callback for the app.quit action."""
self.quit() self.quit()
def do_activate(self): def do_activate(self):
"""Called when the application is activated. """Called when the application is activated.
@ -75,9 +78,7 @@ class ImaginerApplication(Adw.Application):
self.file_chooser.set_transient_for(self.win) self.file_chooser.set_transient_for(self.win)
self.file_chooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER) self.file_chooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER)
self.file_chooser.set_modal(True) self.file_chooser.set_modal(True)
self.file_chooser.connect( self.file_chooser.connect("response", self.on_file_chooser_response)
"response", self.on_file_chooser_response
)
self.token = "" self.token = ""
def on_new_window_action(self, action, _): def on_new_window_action(self, action, _):
@ -110,13 +111,15 @@ class ImaginerApplication(Adw.Application):
def on_about_action(self, widget, _): def on_about_action(self, widget, _):
"""Callback for the app.about action.""" """Callback for the app.about action."""
about = Adw.AboutWindow(transient_for=self.props.active_window, about = Adw.AboutWindow(
application_name='imaginer', transient_for=self.props.active_window,
application_icon='io.github.ImaginerApp.Imaginer', application_name="imaginer",
developer_name='Me', application_icon="io.github.ImaginerApp.Imaginer",
version='0.1.0', developer_name="Me",
developers=['Me'], version="0.1.0",
copyright='© 2023 Me') developers=["Me"],
copyright="© 2023 Me",
)
about.present() about.present()
def on_get_started_action(self, widget, _): def on_get_started_action(self, widget, _):
@ -124,9 +127,13 @@ class ImaginerApplication(Adw.Application):
self.win.stack_imaginer.set_visible_child_name("stack_imagine") self.win.stack_imaginer.set_visible_child_name("stack_imagine")
def slugify(self, value): def slugify(self, value):
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = (
value = re.sub('[^\w\s-]', '', value).strip().lower() unicodedata.normalize("NFKD", value)
return re.sub('[-\s]+', '-', value) .encode("ascii", "ignore")
.decode("ascii")
)
value = re.sub("[^\w\s-]", "", value).strip().lower()
return re.sub("[-\s]+", "-", value)
def on_imagine_action(self, widget, _): def on_imagine_action(self, widget, _):
"""Callback for the app.imagine action.""" """Callback for the app.imagine action."""
@ -144,8 +151,6 @@ class ImaginerApplication(Adw.Application):
NITRO_DIFFUSION = 4 NITRO_DIFFUSION = 4
ANALOG_DIFFUSION = 5 ANALOG_DIFFUSION = 5
PORTRAIT_PLUS = 6 PORTRAIT_PLUS = 6
prompt = self.win.prompt.get_text() prompt = self.win.prompt.get_text()
self.token = self.win.token.get_text() self.token = self.win.token.get_text()
@ -163,11 +168,9 @@ class ImaginerApplication(Adw.Application):
case ProvidersEnum.OPENAI.value: case ProvidersEnum.OPENAI.value:
try: try:
response = openai.Image.create( response = openai.Image.create(
prompt=prompt, prompt=prompt, n=1, size="1024x1024"
n=1,
size="1024x1024"
) )
image_url = response['data'][0]['url'] image_url = response["data"][0]["url"]
image_bytes = requests.get(image_url).content image_bytes = requests.get(image_url).content
except openai.error.AuthenticationError: except openai.error.AuthenticationError:
self.win.banner.set_title("Invalid API Key") self.win.banner.set_title("Invalid API Key")
@ -175,40 +178,58 @@ class ImaginerApplication(Adw.Application):
image_bytes = None image_bytes = None
case ProvidersEnum.STABLE_DIFFUSION.value: case ProvidersEnum.STABLE_DIFFUSION.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
)
case ProvidersEnum.WAIFU_DIFFUSION.value: case ProvidersEnum.WAIFU_DIFFUSION.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/hakurei/waifu-diffusion") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/hakurei/waifu-diffusion",
)
case ProvidersEnum.OPENJOURNEY.value: case ProvidersEnum.OPENJOURNEY.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/prompthero/openjourney") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/prompthero/openjourney",
)
case ProvidersEnum.NITRO_DIFFUSION.value: case ProvidersEnum.NITRO_DIFFUSION.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/nitrosocke/Nitro-Diffusion") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/nitrosocke/Nitro-Diffusion",
)
case ProvidersEnum.ANALOG_DIFFUSION.value: case ProvidersEnum.ANALOG_DIFFUSION.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/wavymulder/Analog-Diffusion") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/wavymulder/Analog-Diffusion",
)
case ProvidersEnum.PORTRAIT_PLUS.value: case ProvidersEnum.PORTRAIT_PLUS.value:
image_bytes = self.query({ image_bytes = self.query(
"inputs": prompt, {
}, "https://api-inference.huggingface.co/models/wavymulder/portraitplus") "inputs": prompt,
},
"https://api-inference.huggingface.co/models/wavymulder/portraitplus",
)
if image_bytes: if image_bytes:
try: try:
image = Image.open(io.BytesIO(image_bytes)) image = Image.open(io.BytesIO(image_bytes))
except UnidentifiedImageError: except UnidentifiedImageError:
error = json.loads(image_bytes)["error"] error = json.loads(image_bytes)["error"]
self.win.banner.set_title(error) self.win.banner.set_title(error)
self.win.banner.set_revealed(True) self.win.banner.set_revealed(True)
image = None image = None
else: else:
image = None image = None
GLib.idle_add(cleanup, image) GLib.idle_add(cleanup, image)
def cleanup(image): def cleanup(image):
@ -218,14 +239,13 @@ class ImaginerApplication(Adw.Application):
if image: if image:
image.save(path) image.save(path)
self.win.image.set_file(Gio.File.new_for_path(path)) self.win.image.set_file(Gio.File.new_for_path(path))
t = threading.Thread(target=thread_run) t = threading.Thread(target=thread_run)
t.start() t.start()
def on_preferences_action(self, widget, _): def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action.""" """Callback for the app.preferences action."""
print('app.preferences action activated') print("app.preferences action activated")
def create_action(self, name, callback, shortcuts=None): def create_action(self, name, callback, shortcuts=None):
"""Add an application action. """Add an application action.

View File

@ -20,9 +20,10 @@
from gi.repository import Adw from gi.repository import Adw
from gi.repository import Gtk from gi.repository import Gtk
@Gtk.Template(resource_path='/io/github/ImaginerApp/Imaginer/window.ui')
@Gtk.Template(resource_path="/io/github/ImaginerApp/Imaginer/window.ui")
class ImaginerWindow(Adw.ApplicationWindow): class ImaginerWindow(Adw.ApplicationWindow):
__gtype_name__ = 'ImaginerWindow' __gtype_name__ = "ImaginerWindow"
toast = Gtk.Template.Child() toast = Gtk.Template.Child()
stack_imaginer = Gtk.Template.Child() stack_imaginer = Gtk.Template.Child()