forked from C0untFloyd/roop-unleashed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
173 lines (143 loc) · 5.23 KB
/
admin.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import gradio as gr
import subprocess
import threading
import queue
import os
import zipfile
import time
from google.colab import files
# Globale Variablen
ROOP_PROCESS = None
LOG_QUEUE = queue.Queue()
STOP_THREAD = False
OUTPUT_PATH = "output"
# Hier speichern wir den roop-Link, sobald wir ihn sehen
ROOPURL_FOUND = None
def create_zip_from_output():
"""
Packt den 'output'-Ordner (ohne bereits erstelltes ZIP) in ein ZIP-Archiv
und startet den Download in Google Colab.
"""
if not os.path.exists(OUTPUT_PATH):
return "Output-Ordner existiert nicht!"
zip_path = os.path.join(OUTPUT_PATH, "output_files.zip")
try:
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files_ in os.walk(OUTPUT_PATH):
for f in files_:
if f == "output_files.zip":
continue
file_path = os.path.join(root, f)
arcname = os.path.relpath(file_path, OUTPUT_PATH)
zipf.write(file_path, arcname)
files.download(zip_path)
return "ZIP wird heruntergeladen..."
except Exception as e:
return f"Fehler beim Erstellen des ZIP: {str(e)}"
def roop_logger(proc):
"""
Läuft in einem Thread, liest stdout des Subprozesses (run.py)
und legt Zeilen in LOG_QUEUE ab, bis der Prozess oder STOP_THREAD endet.
Findet er eine Zeile "ROOPURL::<link>", speichert er diese in ROOPURL_FOUND.
"""
global STOP_THREAD, ROOPURL_FOUND
while True:
if proc.poll() is not None:
break
line = proc.stdout.readline()
if line:
# Falls wir die markierte Zeile finden:
if line.startswith("ROOPURL::"):
link = line.strip().split("ROOPURL::", 1)[-1]
ROOPURL_FOUND = link
LOG_QUEUE.put(line)
if STOP_THREAD:
break
print("Logger-Thread beendet.")
def start_stop_roop(console_str, roop_url_str):
"""
Startet oder stoppt den 'roop-unleashed'-Prozess (run.py).
- console_str: aktueller Inhalt der Konsolen-Textbox
- roop_url_str: aktueller Inhalt der roopURL-Textbox
"""
global ROOP_PROCESS, STOP_THREAD, ROOPURL_FOUND
if ROOP_PROCESS is None:
# Start
console_str = "Starte roop-unleashed...\n"
ROOPURL_FOUND = None # alten Link löschen
ROOP_PROCESS = subprocess.Popen(
["python", "run.py"], # ruft euer run.py auf
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
STOP_THREAD = False
threading.Thread(target=roop_logger, args=(ROOP_PROCESS,), daemon=True).start()
return "Stop Roop", "roop-unleashed gestartet", console_str, roop_url_str
else:
# Stop
console_str += "\nStoppe roop-unleashed...\n"
STOP_THREAD = True
ROOP_PROCESS.terminate()
time.sleep(1)
if ROOP_PROCESS.poll() is None:
ROOP_PROCESS.kill()
ROOP_PROCESS = None
return "Start Roop", "roop-unleashed gestoppt", console_str, "(noch keiner)"
def poll_logs(console_str, roop_url_str):
"""
Alle 2s aufgerufen. Liest neue Log-Zeilen aus LOG_QUEUE
und hängt sie an 'console_str'. roop_url_str wird aktualisiert,
falls in roop_logger(...) 'ROOPURL_FOUND' belegt wurde.
"""
global ROOPURL_FOUND
# 1) Logs abholen
lines = []
while not LOG_QUEUE.empty():
lines.append(LOG_QUEUE.get_nowait())
if lines:
console_str += "".join(lines)
# 2) Falls wir in roop_logger(...) "ROOPURL_FOUND" gefunden haben
if ROOPURL_FOUND:
roop_url_str = ROOPURL_FOUND
return console_str, roop_url_str
# Haupt-Gradio-Interface fürs Admin-Panel
with gr.Blocks(title="Admin Interface für roop-unleashed") as admin_interface:
gr.Markdown("# Admin Interface für roop-unleashed")
with gr.Row():
download_btn = gr.Button("Download als ZIP")
download_info = gr.Textbox(label="Download-Status", interactive=False)
with gr.Row():
start_btn = gr.Button("Start Roop", variant="primary")
status_box = gr.Textbox(label="Status", value="Roop ist gestoppt", interactive=False, lines=1)
console = gr.Textbox(label="Konsole", value="", interactive=False, lines=10)
# Textbox, in der wir den roopURL-Link anzeigen
roop_url_box = gr.Textbox(
label="roop-Unleashed Link",
value="(noch keiner)",
interactive=False,
lines=1
)
# Download-Button => ZIP erstellen
download_btn.click(
fn=create_zip_from_output,
outputs=download_info
)
# Start-/Stop-Button => Subprozess starten/stoppen
# Achtung: wir erweitern 'inputs' und 'outputs', damit roop_url_box in/out übergeben wird
start_btn.click(
fn=start_stop_roop,
inputs=[console, roop_url_box],
outputs=[start_btn, status_box, console, roop_url_box]
)
# Poll-Funktion, um Logs + roopURL zu aktualisieren
admin_interface.load(
fn=poll_logs,
inputs=[console, roop_url_box],
outputs=[console, roop_url_box],
every=2
)
if __name__ == "__main__":
# Im Colab: share=True
admin_interface.launch(share=True)