2222from nc_py_api .ex_app import (
2323 AppAPIAuthMiddleware ,
2424 nc_app ,
25- persistent_storage ,
2625 run_app ,
2726 setup_nextcloud_logging ,
2827)
4342# os.environ["APP_PORT"] = "24000"
4443# os.environ["APP_ID"] = "visionatrix"
4544# os.environ["APP_SECRET"] = "12345" # noqa
45+ # os.environ["NC_DEV_SKIP_RUN"] = "1"
4646# ---------Enf of configuration values for manual deploy---------
4747
48+ SUPERUSER_NAME = "visionatrix_admin"
49+ SUPERUSER_PASSWORD = "" .join (random .choice (string .ascii_letters + string .digits ) for i in range (10 )) # noqa
50+
4851SERVICE_URL = os .environ .get ("VISIONATRIX_URL" , "http://127.0.0.1:8288" )
4952
5053logging .basicConfig (
6063current_translator .set (translation (os .getenv ("APP_ID" ), LOCALE_DIR , languages = ["en" ], fallback = True ))
6164
6265ENABLED_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+
6767INSTALLED_FLOWS = []
6868
6969PROJECT_ROOT_FOLDER = Path (__file__ ).parent .parent .parent
@@ -90,9 +90,9 @@ async def dispatch(self, request: Request, call_next):
9090async def lifespan (_app : FastAPI ):
9191 global SUPERUSER_PASSWORD
9292
93+ SUPERUSER_PASSWORD = os .environ ["ADMIN_OVERRIDE" ].split (":" )[1 ]
9394 print (_ ("Visionatrix" ), flush = True )
9495 setup_nextcloud_logging ("visionatrix" , logging_level = logging .WARNING )
95- SUPERUSER_PASSWORD = Path (SUPERUSER_PASSWORD_PATH ).read_text ()
9696 _t1 = asyncio .create_task (start_nextcloud_provider_registration ()) # noqa
9797 _t2 = asyncio .create_task (start_nextcloud_tasks_polling ()) # noqa
9898 yield
@@ -285,16 +285,20 @@ def poll_tasks(nc: NextcloudApp, basic_auth: httpx.BasicAuth, webhook_url: str,
285285 if not reply_from_nc :
286286 return False
287287 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"
288297 with httpx .Client (base_url = f"{ SERVICE_URL } /vapi" ) as client :
289298 vix_task = client .put (
290- url = f"/tasks/create/{ reply_from_nc [ 'provider' ][ 'name' ]. removeprefix ( 'v_' ) } " ,
299+ url = f"/tasks/create/{ flow_name } " ,
291300 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 ,
298302 )
299303 LOGGER .debug ("task passed to visionatrix, return code: %s" , vix_task .status_code )
300304 return True
@@ -349,36 +353,48 @@ async def start_nextcloud_provider_registration():
349353 await asyncio .to_thread (background_provider_registration )
350354
351355
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 )
366387
367- def initialize_visionatrix () -> None :
368388 while True : # Let's wait until Visionatrix opens the port.
369389 with contextlib .suppress (httpx .ReadError , httpx .ConnectError , httpx .RemoteProtocolError ):
370390 r = httpx .get (SERVICE_URL )
371391 if r .status_code in (200 , 204 , 401 , 403 ):
372392 break
373393 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 )
379394
380395
381396if __name__ == "__main__" :
382- initialize_visionatrix ()
397+ os .environ ["ADMIN_OVERRIDE" ] = f"{ SUPERUSER_NAME } :{ SUPERUSER_PASSWORD } "
398+ start_visionatrix ()
383399 os .chdir (Path (__file__ ).parent )
384400 run_app ("main:APP" , log_level = "trace" )
0 commit comments