@@ -72,9 +72,8 @@ def __init__(self, account_uuid: str, account_nickname: str, fp_profile_path: st
7272 if get_headless ():
7373 self .options .add_argument ("--headless" )
7474
75- # Set the profile path
76- self .options .add_argument ("-profile" )
77- self .options .add_argument (fp_profile_path )
75+ profile = webdriver .FirefoxProfile (self ._fp_profile_path )
76+ self .options .profile = profile
7877
7978 # Set the service
8079 self .service : Service = Service (GeckoDriverManager ().install ())
@@ -226,7 +225,22 @@ def generate_prompts(self) -> List[str]:
226225 Returns:
227226 image_prompts (List[str]): Generated List of image prompts.
228227 """
229- n_prompts = len (self .script ) / 3
228+ # Check if using G4F for image generation
229+ cached_accounts = get_accounts ("youtube" )
230+ account_config = None
231+ for account in cached_accounts :
232+ if account ["id" ] == self ._account_uuid :
233+ account_config = account
234+ break
235+
236+ # Calculate number of prompts based on script length
237+ base_n_prompts = len (self .script ) / 3
238+
239+ # If using G4F, limit to 25 prompts
240+ if account_config and account_config .get ("use_g4f" , False ):
241+ n_prompts = min (base_n_prompts , 25 )
242+ else :
243+ n_prompts = base_n_prompts
230244
231245 prompt = f"""
232246 Generate { n_prompts } Image Prompts for AI Image Generation,
@@ -279,60 +293,135 @@ def generate_prompts(self) -> List[str]:
279293 warning ("Failed to generate Image Prompts. Retrying..." )
280294 return self .generate_prompts ()
281295
282- self .image_prompts = image_prompts
296+ # Limit prompts to max allowed amount
297+ if account_config and account_config .get ("use_g4f" , False ):
298+ image_prompts = image_prompts [:25 ]
299+ elif len (image_prompts ) > n_prompts :
300+ image_prompts = image_prompts [:int (n_prompts )]
283301
284- # Check the amount of image prompts
285- # and remove if it's more than needed
286- if len (image_prompts ) > n_prompts :
287- image_prompts = image_prompts [:n_prompts ]
302+ self .image_prompts = image_prompts
288303
289304 success (f"Generated { len (image_prompts )} Image Prompts." )
290305
291306 return image_prompts
292307
293- def generate_image (self , prompt : str ) -> str :
308+ def generate_image_g4f (self , prompt : str ) -> str :
294309 """
295- Generates an AI Image based on the given prompt .
310+ Generates an AI Image using G4F with SDXL Turbo .
296311
297312 Args:
298313 prompt (str): Reference for image generation
299314
300315 Returns:
301316 path (str): The path to the generated image.
302317 """
303- ok = False
304- while ok == False :
305- url = f"https://hercai.onrender.com/{ get_image_model ()} /text2image?prompt={ prompt } "
318+ print (f"Generating Image using G4F: { prompt } " )
319+
320+ try :
321+ from g4f .client import Client
322+
323+ client = Client ()
324+ response = client .images .generate (
325+ model = "sdxl-turbo" ,
326+ prompt = prompt ,
327+ response_format = "url"
328+ )
329+
330+ if response and response .data and len (response .data ) > 0 :
331+ # Download image from URL
332+ image_url = response .data [0 ].url
333+ image_response = requests .get (image_url )
334+
335+ if image_response .status_code == 200 :
336+ image_path = os .path .join (ROOT_DIR , ".mp" , str (uuid4 ()) + ".png" )
337+
338+ with open (image_path , "wb" ) as image_file :
339+ image_file .write (image_response .content )
340+
341+ if get_verbose ():
342+ info (f" => Downloaded Image from { image_url } to \" { image_path } \" \n " )
343+
344+ self .images .append (image_path )
345+ return image_path
346+ else :
347+ if get_verbose ():
348+ warning (f"Failed to download image from URL: { image_url } " )
349+ return None
350+ else :
351+ if get_verbose ():
352+ warning ("Failed to generate image using G4F - no data in response" )
353+ return None
354+
355+ except Exception as e :
356+ if get_verbose ():
357+ warning (f"Failed to generate image using G4F: { str (e )} " )
358+ return None
306359
307- r = requests .get (url )
308- parsed = r .json ()
360+ def generate_image_cloudflare (self , prompt : str , worker_url : str ) -> str :
361+ """
362+ Generates an AI Image using Cloudflare worker.
309363
310- if "url" not in parsed or not parsed .get ("url" ):
311- # Retry
312- if get_verbose ():
313- info (f" => Failed to generate Image for Prompt: { prompt } . Retrying..." )
314- ok = False
315- else :
316- ok = True
317- image_url = parsed ["url" ]
364+ Args:
365+ prompt (str): Reference for image generation
366+ worker_url (str): The Cloudflare worker URL
318367
319- if get_verbose ():
320- info (f" => Generated Image: { image_url } " )
368+ Returns:
369+ path (str): The path to the generated image.
370+ """
371+ print (f"Generating Image using Cloudflare: { prompt } " )
321372
322- image_path = os .path .join (ROOT_DIR , ".mp" , str (uuid4 ()) + ".png" )
323-
324- with open (image_path , "wb" ) as image_file :
325- # Write bytes to file
326- image_r = requests .get (image_url )
373+ url = f"{ worker_url } ?prompt={ prompt } &model=sdxl"
374+
375+ response = requests .get (url )
376+
377+ if response .headers .get ('content-type' ) == 'image/png' :
378+ image_path = os .path .join (ROOT_DIR , ".mp" , str (uuid4 ()) + ".png" )
379+
380+ with open (image_path , "wb" ) as image_file :
381+ image_file .write (response .content )
382+
383+ if get_verbose ():
384+ info (f" => Wrote Image to \" { image_path } \" \n " )
385+
386+ self .images .append (image_path )
387+
388+ return image_path
389+ else :
390+ if get_verbose ():
391+ warning ("Failed to generate image. The response was not a PNG image." )
392+ return None
327393
328- image_file .write (image_r .content )
394+ def generate_image (self , prompt : str ) -> str :
395+ """
396+ Generates an AI Image based on the given prompt.
329397
330- if get_verbose () :
331- info ( f" => Wrote Image to \" { image_path } \" \n " )
398+ Args :
399+ prompt (str): Reference for image generation
332400
333- self .images .append (image_path )
334-
335- return image_path
401+ Returns:
402+ path (str): The path to the generated image.
403+ """
404+ # Get account config from cache
405+ cached_accounts = get_accounts ("youtube" )
406+ account_config = None
407+ for account in cached_accounts :
408+ if account ["id" ] == self ._account_uuid :
409+ account_config = account
410+ break
411+
412+ if not account_config :
413+ error ("Account configuration not found" )
414+ return None
415+
416+ # Check if using G4F or Cloudflare
417+ if account_config .get ("use_g4f" , False ):
418+ return self .generate_image_g4f (prompt )
419+ else :
420+ worker_url = account_config .get ("worker_url" )
421+ if not worker_url :
422+ error ("Cloudflare worker URL not configured for this account" )
423+ return None
424+ return self .generate_image_cloudflare (prompt , worker_url )
336425
337426 def generate_script_to_speech (self , tts_instance : TTS ) -> str :
338427 """
0 commit comments