-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.py
132 lines (119 loc) · 4.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import sys
import pathlib
from tools.config import Config
from PyQt5.QtCore import Qt, pyqtSlot, QThread, QObject, pyqtSignal
from PyQt5.QtGui import QPalette, QColor, QIcon, QTextCursor
from PyQt5.QtWidgets import QMainWindow, QApplication, QTabWidget, QSplitter, QPlainTextEdit
from queue import Queue
from ui.gallery_tab import GalleryTab
from ui.generator_tab import GeneratorTab
from ui.editor_tab import EditorTab
from ui.prompts_tab import PromptsTab
class LogStream(object):
save_stdout = sys.stdout
save_stderr = sys.stderr
def __init__(self, queue):
self.queue = queue
def write(self, text):
self.queue.put(text)
self.save_stdout.write(text)
def flush(self):
pass
class LogReceiver(QObject):
signal = pyqtSignal(str)
def __init__(self, queue, *args, **kwargs):
QObject.__init__(self, *args, **kwargs)
self.queue = queue
@pyqtSlot()
def run(self):
while True:
text = self.queue.get()
self.signal.emit(text)
class Window(QMainWindow):
prev_line = ''
def __init__(self):
super(Window, self).__init__()
self.cfg = Config()
# System color changes
palette = QPalette()
palette.setColor(QPalette.Highlight, QColor.fromRgb(2, 113, 177))
QApplication.setPalette(palette)
# UI elements
self.setWindowTitle("Stable Diffusion")
self.asset_path = pathlib.Path(__file__).parent / 'assets'
self.move(0, 0)
# self.setGeometry(0, 0, 1020, 1150)
self.resize(1150, 1150)
self.setup()
self.show()
def setup(self):
# Main splitter
main_split = QSplitter(Qt.Vertical)
# Tabs
self.tabs = QTabWidget()
self.tabs.setContentsMargins(0, 0, 0, 0)
self.tabGen = GeneratorTab(self.cfg)
self.tabGen.setContentsMargins(0, 0, 0, 0)
self.tabs.addTab(self.tabGen, "Generate Images")
self.tabEditor = EditorTab(self.cfg)
self.tabEditor.setContentsMargins(0, 0, 0, 0)
self.tabs.addTab(self.tabEditor, "Edit Images")
self.tabPrompts = PromptsTab(self.cfg)
self.tabPrompts.setContentsMargins(0, 0, 0, 0)
self.tabs.addTab(self.tabPrompts, "Prompts")
self.tabGallery = GalleryTab(self.cfg)
self.tabGallery.setContentsMargins(0, 0, 0, 0)
self.tabs.addTab(self.tabGallery, "Gallery")
main_split.addWidget(self.tabs)
# Log area
self.logs = QPlainTextEdit()
self.logs.setContentsMargins(0, 0, 0, 0)
self.logs.setReadOnly(True)
main_split.addWidget(self.logs)
main_split.setSizes([1150, 50])
main_split.setStretchFactor(23, 1)
self.setCentralWidget(main_split)
# Pass editor to other tabs which need it
self.tabGen.editor_tab = self.tabEditor
self.tabGallery.editor_tab = self.tabEditor
@pyqtSlot(str)
def log_text(self, line):
self.logs.moveCursor(QTextCursor.End)
if self.prev_line.endswith('it/s]') or self.prev_line.endswith('s/it]'):
if line.endswith('it/s]') or line.endswith('s/it]'):
# Replace last line and move on
cursor = self.logs.textCursor()
cursor.select(QTextCursor.LineUnderCursor)
cursor.removeSelectedText()
cursor.deletePreviousChar()
# Insert new line
self.logs.insertPlainText(line)
self.prev_line = line
if __name__ == '__main__':
# Create Queue and redirect system output to this queue
queue = Queue()
# sys.stdout = sys.stderr = LogStream(queue)
# Windows specific changes to make it display correctly
if os.name == 'nt':
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
# The app
app = QApplication(sys.argv)
# Windows specific changes to make it display correctly
if os.name == 'nt':
font = app.font()
font.setPointSize(12)
app.setFont(font)
# Global configurations
app.setWindowIcon(QIcon(str('assets/icon.png')))
win = Window()
# Create thread that will listen on the other end of the queue, and send the text to log console
thread = QThread()
receiver = LogReceiver(queue)
receiver.signal.connect(win.log_text)
receiver.moveToThread(thread)
thread.started.connect(receiver.run)
thread.start()
# Start app
app.exec()