22
22
from nc_py_api .ex_app import (
23
23
AppAPIAuthMiddleware ,
24
24
nc_app ,
25
- persistent_storage ,
26
25
run_app ,
27
26
setup_nextcloud_logging ,
28
27
)
43
42
# os.environ["APP_PORT"] = "24000"
44
43
# os.environ["APP_ID"] = "visionatrix"
45
44
# os.environ["APP_SECRET"] = "12345" # noqa
45
+ # os.environ["NC_DEV_SKIP_RUN"] = "1"
46
46
# ---------Enf of configuration values for manual deploy---------
47
47
48
+ SUPERUSER_NAME = "visionatrix_admin"
49
+ SUPERUSER_PASSWORD = "" .join (random .choice (string .ascii_letters + string .digits ) for i in range (10 )) # noqa
50
+
48
51
SERVICE_URL = os .environ .get ("VISIONATRIX_URL" , "http://127.0.0.1:8288" )
49
52
50
53
logging .basicConfig (
60
63
current_translator .set (translation (os .getenv ("APP_ID" ), LOCALE_DIR , languages = ["en" ], fallback = True ))
61
64
62
65
ENABLED_FLAG = NextcloudApp ().enabled_state
63
- SUPERUSER_PASSWORD_PATH = Path (persistent_storage ()).joinpath ("superuser.txt" )
64
- SUPERUSER_NAME = "visionatrix_admin"
65
- SUPERUSER_PASSWORD : str = ""
66
- print (str (SUPERUSER_PASSWORD_PATH ), flush = True ) # for development only
66
+
67
67
INSTALLED_FLOWS = []
68
68
69
69
PROJECT_ROOT_FOLDER = Path (__file__ ).parent .parent .parent
@@ -90,9 +90,9 @@ async def dispatch(self, request: Request, call_next):
90
90
async def lifespan (_app : FastAPI ):
91
91
global SUPERUSER_PASSWORD
92
92
93
+ SUPERUSER_PASSWORD = os .environ ["ADMIN_OVERRIDE" ].split (":" )[1 ]
93
94
print (_ ("Visionatrix" ), flush = True )
94
95
setup_nextcloud_logging ("visionatrix" , logging_level = logging .WARNING )
95
- SUPERUSER_PASSWORD = Path (SUPERUSER_PASSWORD_PATH ).read_text ()
96
96
_t1 = asyncio .create_task (start_nextcloud_provider_registration ()) # noqa
97
97
_t2 = asyncio .create_task (start_nextcloud_tasks_polling ()) # noqa
98
98
yield
@@ -285,16 +285,20 @@ def poll_tasks(nc: NextcloudApp, basic_auth: httpx.BasicAuth, webhook_url: str,
285
285
if not reply_from_nc :
286
286
return False
287
287
task_info = reply_from_nc ["task" ]
288
+ data = {
289
+ "prompt" : task_info ["input" ]["input" ],
290
+ "batch_size" : min (task_info ["input" ]["numberOfImages" ], 4 ),
291
+ "webhook_url" : webhook_url + f"/{ task_info ['id' ]} " ,
292
+ "webhook_headers" : webhook_headers ,
293
+ }
294
+ flow_name = reply_from_nc ["provider" ]["name" ].removeprefix ("v_" )
295
+ if flow_name in ("flux1_dev" , "flux1_schnell" ):
296
+ data ["diffusion_precision" ] = "fp8_e4m3fn"
288
297
with httpx .Client (base_url = f"{ SERVICE_URL } /vapi" ) as client :
289
298
vix_task = client .put (
290
- url = f"/tasks/create/{ reply_from_nc [ 'provider' ][ 'name' ]. removeprefix ( 'v_' ) } " ,
299
+ url = f"/tasks/create/{ flow_name } " ,
291
300
auth = basic_auth ,
292
- data = {
293
- "prompt" : task_info ["input" ]["input" ],
294
- "batch_size" : min (task_info ["input" ]["numberOfImages" ], 4 ),
295
- "webhook_url" : webhook_url + f"/{ task_info ['id' ]} " ,
296
- "webhook_headers" : webhook_headers ,
297
- },
301
+ data = data ,
298
302
)
299
303
LOGGER .debug ("task passed to visionatrix, return code: %s" , vix_task .status_code )
300
304
return True
@@ -349,36 +353,48 @@ async def start_nextcloud_provider_registration():
349
353
await asyncio .to_thread (background_provider_registration )
350
354
351
355
352
- def generate_random_string (length = 10 ):
353
- letters = string .ascii_letters + string .digits # You can include other characters if needed
354
- return "" .join (random .choice (letters ) for i in range (length )) # noqa
355
-
356
-
357
- def venv_run (command : str ) -> None :
358
- command = f". /Visionatrix/venv/bin/activate && { command } "
359
- try :
360
- print (f"executing(pwf={ os .getcwd ()} ): { command } " , flush = True )
361
- subprocess .check_call (command , shell = True )
362
- except subprocess .CalledProcessError as e :
363
- print ("An error occurred while executing command in venv:" , str (e ), flush = True )
364
- raise
365
-
356
+ def start_visionatrix () -> None :
357
+ if os .environ .get ("NC_DEV_SKIP_RUN" ) != "1" :
358
+ visionatrix_python = "/Visionatrix/venv/bin/python"
359
+ if os .environ .get ("DISABLE_WORKER" ) != "1" :
360
+ # Run server in background and redirect output to server.log
361
+ server_log = open ("server.log" , "wb" )
362
+ subprocess .Popen (
363
+ [visionatrix_python , "-m" , "visionatrix" , "run" , "--mode=SERVER" ],
364
+ stdout = server_log ,
365
+ stderr = subprocess .STDOUT ,
366
+ )
367
+ print ("[DEBUG]: Launched Visionatrix server in background" , flush = True )
368
+ # Wait a bit to let the server start up
369
+ sleep (15 )
370
+ # Run worker in background and redirect output to worker.log
371
+ worker_log = open ("worker.log" , "wb" )
372
+ subprocess .Popen (
373
+ [visionatrix_python , "-m" , "visionatrix" , "run" , "--mode=WORKER" , "--disable-smart-memory" ],
374
+ stdout = worker_log ,
375
+ stderr = subprocess .STDOUT ,
376
+ )
377
+ print ("[DEBUG]: Launched Visionatrix worker in background" , flush = True )
378
+ else :
379
+ # Only run server when worker is disabled
380
+ server_log = open ("server.log" , "wb" )
381
+ subprocess .Popen (
382
+ [visionatrix_python , "-m" , "visionatrix" , "run" , "--mode=SERVER" ],
383
+ stdout = server_log ,
384
+ stderr = subprocess .STDOUT ,
385
+ )
386
+ print ("[DEBUG]: Launched Visionatrix server (worker disabled)" , flush = True )
366
387
367
- def initialize_visionatrix () -> None :
368
388
while True : # Let's wait until Visionatrix opens the port.
369
389
with contextlib .suppress (httpx .ReadError , httpx .ConnectError , httpx .RemoteProtocolError ):
370
390
r = httpx .get (SERVICE_URL )
371
391
if r .status_code in (200 , 204 , 401 , 403 ):
372
392
break
373
393
sleep (5 )
374
- if not SUPERUSER_PASSWORD_PATH .exists ():
375
- password = generate_random_string ()
376
- # password = "12345" # uncomment this line and comment next for the developing with local Visionatrix version.
377
- venv_run (f"python3 -m visionatrix create-user --name { SUPERUSER_NAME } --password { password } " )
378
- Path (SUPERUSER_PASSWORD_PATH ).write_text (password )
379
394
380
395
381
396
if __name__ == "__main__" :
382
- initialize_visionatrix ()
397
+ os .environ ["ADMIN_OVERRIDE" ] = f"{ SUPERUSER_NAME } :{ SUPERUSER_PASSWORD } "
398
+ start_visionatrix ()
383
399
os .chdir (Path (__file__ ).parent )
384
400
run_app ("main:APP" , log_level = "trace" )
0 commit comments