in genai-for-marketing/backend_apis/app/main.py [0:0]
def generate_content(data: ContentCreationRequest
) -> ContentCreationResponse:
"""Generate Content like Media , Ad or Emails
Body:
type: str | Email/Webpost/SocialMedia/AssetGroup
theme: str
context: str | None = None
no_of_char: int = 500
audience_age_range: str = '20-30'
audience_gender:str = 'All'
image_generate: bool = True
Returns:
text_content :str
images : list
"""
images = []
generated_content = {}
try:
if data.type == 'Email':
print("Generating Email..")
generated_content["text"]=asyncio.run(utils_prompt.async_predict_text_gemini(
EMAIL_TEXT_PROMPT.format(
data.context,
data.theme
)
)
)
elif data.type == 'Webpost':
generated_content["text"]=asyncio.run(utils_prompt.async_predict_text_gemini(
WEBSITE_PROMPT_TEMPLATE.format(
data.theme,
data.context)))
elif data.type == 'SocialMedia':
generated_content["text"]=asyncio.run(utils_prompt.async_predict_text_gemini(
AD_PROMPT_TEMPLATE.format(
BUSINESS_NAME,
data.no_of_char,
data.audience_age_range,
data.audience_gender,
data.theme,
data.context,
)))
elif data.type == 'AssetGroup':
async def generate_brief() -> tuple:
return await asyncio.gather(
utils_prompt.async_predict_text_gemini(
prompt=HEADLINE_PROMPT_TEMPLATE.format(
data.theme,
BRAND_OVERVIEW),
max_output_tokens=256
),
utils_prompt.async_predict_text_gemini(
prompt=LONG_HEADLINE_PROMPT_TEMPLATE.format(
data.theme,
BRAND_OVERVIEW)
),
utils_prompt.async_predict_text_gemini(
prompt=DESCRIPTION_PROMPT_TEMPLATE.format(
data.theme,
BRAND_OVERVIEW,
data.context)))
generated_tuple = asyncio.run(generate_brief())
generated_content["headlines"] = generated_tuple[0]
generated_content["long_headlines"] = generated_tuple[1]
generated_content["description"] = generated_tuple[2]
generated_content["scenario"] = data.theme
generated_content["business_name"] = BUSINESS_NAME
generated_content["call_to_action"] = "Shop Now"
if data.image_generate == True:
images = asyncio.run(utils_prompt.async_generate_image(
prompt=IMAGE_PROMPT_TAMPLATE.format(
data.theme,)
))
except Exception as e:
raise HTTPException(
status_code=400,
detail="Something went wrong. Please try again."+str(e)
)
return ContentCreationResponse(
generated_content=generated_content,
images = images
)