adgenesis-internal / apis /userdata.py
pmachal101723's picture
gracefully error handleing
c4972c9
import logging
import asyncio
from concurrent.futures import ThreadPoolExecutor
from helpercodes.image_functions import generate_image
from helpercodes.user_data import generate_gpt_prompt, _encode_image_to_base64
# Configure Logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
def _generate_prompt_and_image(pos: str, neg: str, category: str, story_instruction: str,
sentiment, image_elements, is_text, text_elements, non_compliant, emotion,
aspect_ratio, design):
try:
logging.info(f"Generating GPT prompt for category: {category}, instruction: {story_instruction}")
# Generate the GPT prompt
prompt = generate_gpt_prompt(
pos,
neg,
category,
story_instruction,
sentiment, image_elements, is_text, text_elements, non_compliant, emotion
)
logging.info(f"Generated GPT prompt: {prompt}")
# Generate the image from the prompt
image = generate_image(prompt, aspect_ratio, design)
if image:
logging.info("Successfully generated image.")
else:
logging.warning("Image generation failed.")
return image
except Exception as e:
logging.error(f"Error in _generate_prompt_and_image: {e}", exc_info=True)
return None
async def process_custom_data(pos, category, neg, sentiment, image_elements, is_text, text_elements,
non_compliant, emotion, aspect_ratio, design):
try:
logging.info("Processing custom data...")
# The two different instructions
instruction_var = (
"which should have new offers, revised text and graphics but style should remain same"
)
instruction_same = (
"which should have new offers and revised text but graphics and style should remain same"
)
# We'll use asyncio's event loop to run blocking code in a ThreadPoolExecutor
loop = asyncio.get_running_loop()
# Create the thread pool
with ThreadPoolExecutor() as executor:
# Schedule both tasks in parallel
future_var = loop.run_in_executor(
executor,
_generate_prompt_and_image,
pos,
neg,
category,
instruction_var,
sentiment, image_elements, is_text, text_elements,
non_compliant, emotion, aspect_ratio, design
)
future_same = loop.run_in_executor(
executor,
_generate_prompt_and_image,
pos,
neg,
category,
instruction_same,
sentiment, image_elements, is_text, text_elements,
non_compliant, emotion, aspect_ratio, design
)
# Wait for both images to finish
image_var, image_same = await asyncio.gather(future_var, future_same)
return image_var, image_same
except Exception as e:
logging.error(f"Unexpected error in process_custom_data: {e}", exc_info=True)
return None, None
def process_user_data_sync(pos, category, neg, sentiment, image_elements, is_text, text_elements,
non_compliant, emotion, aspect_ratio, design):
try:
logging.info("Executing process_user_data_sync...")
return asyncio.run(process_custom_data(pos, category, neg, sentiment, image_elements, is_text, text_elements,
non_compliant, emotion, aspect_ratio, design))
except RuntimeError as e:
logging.error("RuntimeError in process_user_data_sync: Async event loop issue", exc_info=True)
return None, None
except Exception as e:
logging.error(f"Unexpected error in process_user_data_sync: {e}", exc_info=True)
return None, None