TextGen / meta_content_creation.py
abdullah10's picture
Upload 17 files
dfc4131
from transformers import pipeline
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from load_model import call_palm
from calling_apis import google_api_key, openai_api_key
class meta_content_generation:
"""
Class meta_content_generation:
Provides methods to generate content for social media platforms using Language Models (LLMs) and prompt templates.
"""
def facebook_ads_gen(self, product_name:str, product_description:str, tone_of_voice:str, targeted_audience="", plans_promotions="", language='En', model_name='Google Palm 2', creativity='Original')->str:
"""
Generates a Facebook ad based on the provided product details and parameters.
Parameters:
- product_name (str): Name of the product for the ad.
- product_description (str): Description of the product for the ad.
- tone_of_voice (str): Tone of the ad.
- targeted_audience (str): Target audience for the ad (optional).
- plans_promotions (str): Plans and promotions for the ad (optional).
Returns:
- Generated Facebook ad text.
"""
temp = 0
if creativity == 'Original':
temp = 0
elif creativity == 'Balanced':
temp = 0.25
elif creativity == 'Creative':
temp = 0.5
elif creativity == 'Spirited':
temp = 0.75
elif creativity == 'Visionary':
temp = 1
if model_name == 'Google Palm 2':
llm = call_palm(google_api_key, temperature=temp)
elif model_name == 'GPT-3.5':
llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)
elif model_name == 'GPT-4':
llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)
if language == 'En':
if targeted_audience != "" and plans_promotions != "":
facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our Target Audience is {targeted_audience}. Our plans and promotions is {plans_promotions}. Tone of the ad should be {tone_of_voice}"
elif targeted_audience == "" and plans_promotions != "":
facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our plans and promotions is {plans_promotions}. Tone of the ad should be {tone_of_voice}"
elif targeted_audience != "" and plans_promotions == "":
facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Our Target Audience is {targeted_audience}. Tone of the ad should be{tone_of_voice}."
else:
facebook_ads_prompt = f"Generate a Facebook ad for {product_name} Product. {product_name} is {product_description}. Tone of the ad Should be {tone_of_voice}."
elif language == 'Ar':
if targeted_audience != "" and plans_promotions != "":
facebook_ads_prompt = f"ุงู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู…ู†ุชุฌ {product_name}. {product_name} ู‡ูˆ {product_description}. ุฌู…ู‡ูˆุฑู†ุง ุงู„ู…ุณุชู‡ุฏู ู‡ูˆ {targeted_audience}. ุฎุทุทู†ุง ูˆุนุฑูˆุถู†ุง ู‡ูŠ {plans_promotions}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุชูˆุฌู‡ ุงู„ุฅุนู„ุงู† {tone_of_voice}."
elif targeted_audience == "" and plans_promotions != "":
facebook_ads_prompt = f"ู‚ู… ุจุฅู†ุดุงุก ุฅุนู„ุงู† ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู€ {product_name} ุงู„ู…ู†ุชุฌ. {product_name} ู‡ูˆ {product_description}. ุฎุทุทู†ุง ูˆุนุฑูˆุถู†ุง ู‡ูŠ {plans_promotions}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุชูˆู† ุงู„ุฅุนู„ุงู† {tone_of_voice}"
elif targeted_audience != "" and plans_promotions == "":
facebook_ads_prompt = f"ุฃู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู…ู†ุชุฌ {product_name}. {product_name} ู‡ูˆ {product_description}. ุฌู…ู‡ูˆุฑู†ุง ุงู„ู…ุณุชู‡ุฏู ู‡ูˆ {targeted_audience}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุทุงุจุน ุงู„ุฅุนู„ุงู† {tone_of_voice}"
else:
facebook_ads_prompt = f"ุงู†ุดุฆ ุฅุนู„ุงู†ู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ู„ู€ {product_name} ุงู„ู…ู†ุชุฌ. {product_name} ู‡ูˆ {product_description}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ู†ุจุฑุฉ ุงู„ุฅุนู„ุงู† {tone_of_voice}."
facebook_ads_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="You are a Professional Facebook Ad Copywriter:\n{text_input}\nFacebook Ad:")
facebook_ad_extraction_chain = LLMChain(llm=llm, prompt=facebook_ads_promptTemp)
facebook_ad = facebook_ad_extraction_chain.run(facebook_ads_prompt)
return facebook_ad
def facbook_camp_gen(self, product_name:str, product_desc:str, days:int, goal:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
"""
Generates a Facebook Campign based on the provided product details and goals.
Parameters:
- product_name (str): Name of the product for the ad.
- product_desc (str): Description of the product for the ad.
- days (int): Number of campaign days.
- goal (str): Goal of the campaign.
- language (str): the language of the generated content (optional, default value is 'EN').
Returns:
- Facebook_ad (str): Generated Facebook Campaign .
"""
temp = 0
if creativity == 'Original':
temp = 0
elif creativity == 'Balanced':
temp = 0.25
elif creativity == 'Creative':
temp = 0.5
elif creativity == 'Spirited':
temp = 0.75
elif creativity == 'Visionary':
temp = 1
if model_name == 'Google Palm 2':
llm = call_palm(google_api_key, temperature=temp)
elif model_name == 'GPT-3.5':
llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)
elif model_name == 'GPT-4':
llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)
if language == 'En':
facebook_ads_prompt = f"Generate a {days} days Persuative Facebook campaign (don't mention any budget) calendar for our {product_name}. {product_name} is {product_desc}. with the goal to {goal}."
facebook_ads_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="""You are a Professional Facebook Digital Marketer:\n{text_input}\nFacebook Campaign:""")
elif language == 'Ar':
facebook_ads_prompt = f"ู‚ู… ุจุฅู†ุดุงุก ุญู…ู„ุฉ ูุงูŠุณุจูˆูƒ ู‚ุงุฆู…ุฉ ู„ู…ุฏุฉ {days} ูŠูˆู…ู‹ุง (ุฏูˆู† ุงู„ุฅุดุงุฑุฉ ุฅู„ู‰ ุฃูŠ ู…ูŠุฒุงู†ูŠุฉ) ู„ู€ {product_name} ุงู„ุฎุงุต ุจู†ุง. {product_name} ู‡ูˆ {product_desc}. ุจู‡ุฏู {goal}."
facebook_ads_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="ุฃู†ุช ู…ุณูˆู‘ู‚ ุฑู‚ู…ูŠ ู…ุญุชุฑู ุนู„ู‰ ููŠุณุจูˆูƒ:\n{text_input}\nุญู…ู„ุฉ ููŠุณุจูˆูƒ:")
facebook_ad_extraction_chain = LLMChain(llm=llm, prompt=facebook_ads_promptTemp)
facebook_ad = facebook_ad_extraction_chain.run(facebook_ads_prompt)
return facebook_ad
def facebook_post_gen(self, tone_of_voice:str, topic:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
"""
Generates a Facebook Post based on the provided topic.
Parameters:
- tone_of_voice (str): The tone of the facebook post.
- topic (str): The facebook post topic
- language (str): the language of the generated content (optional, default value is 'EN').
Returns:
- Facebook_post (str): Generated Facebook Post .
"""
temp = 0
if creativity == 'Original':
temp = 0
elif creativity == 'Balanced':
temp = 0.25
elif creativity == 'Creative':
temp = 0.5
elif creativity == 'Spirited':
temp = 0.75
elif creativity == 'Visionary':
temp = 1
if model_name == 'Google Palm 2':
llm = call_palm(google_api_key, temperature=temp)
elif model_name == 'GPT-3.5':
llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)
elif model_name == 'GPT-4':
llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)
if language == 'En':
productDesc_prompt = f"Generate an attractive and persuasive facebook post on {topic}. Tone should be {tone_of_voice}. Post dosen't include any photos or videos."
productDesc_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="You are a professional facebook content creator:\n{text_input}\n\nFacebook Post:")
elif language == 'Ar':
productDesc_prompt = f"ุงู†ุดุฑ ู…ู†ุดูˆุฑู‹ุง ุฌุฐุงุจู‹ุง ูˆู…ู‚ู†ุนู‹ุง ุนู„ู‰ ููŠุณุจูˆูƒ ุญูˆู„ {topic}. ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุงู„ู†ุจุฑุฉ {tone_of_voice}. ู„ุง ูŠุชุถู…ู† ุงู„ู…ู†ุดูˆุฑ ุฃูŠ ุตูˆุฑ ุฃูˆ ู…ู‚ุงุทุน ููŠุฏูŠูˆ."
productDesc_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="ุฃู†ุช ู…ุจุฏุน ู…ุญุชุฑู ููŠ ุฅู†ุดุงุก ู…ุญุชูˆู‰ ุนู„ู‰ ููŠุณุจูˆูƒ:\n{text_input}\n\nู…ู†ุดูˆุฑ ููŠุณุจูˆูƒ:")
productDesc_extraction_chain = LLMChain(llm=llm, prompt=productDesc_promptTemp)
product_desc = productDesc_extraction_chain.run(productDesc_prompt)
return product_desc
def img2text(self, url:str) -> str:
"""
Function to extract text from an image using a pre-trained image captioning model.
Parameters:
self: The instance of a class (if this function is part of a class).
url (str): The URL pointing to the image for which text extraction is required.
Returns:
out (str): The extracted text from the provided image.
"""
image_to_text = pipeline("image-to-text", model='Salesforce/blip-image-captioning-base')
text = image_to_text(url)
out = text[0]['generated_text']
return out
def generate_InstaCap(self, scenario:str, tone_of_voice:str, form:str, language='En', model_name='Google Palm 2', creativity='Original') -> str:
"""
Generates a Instagram Caption based on the image description.
Parameters:
- tone_of_voice (str): The tone of the facebook post.
- scenario (str): The description of the image
- form (str): the form of the caption (short - long) forms
Returns:
- instaCap (str): Generated Image Caption.
"""
temp = 0
if creativity == 'Original':
temp = 0
elif creativity == 'Balanced':
temp = 0.25
elif creativity == 'Creative':
temp = 0.5
elif creativity == 'Spirited':
temp = 0.75
elif creativity == 'Visionary':
temp = 1
if model_name == 'Google Palm 2':
llm = call_palm(google_api_key, temperature=temp)
elif model_name == 'GPT-3.5':
llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp)
elif model_name == 'GPT-4':
llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp)
if language == 'En':
instaCap_prompt = f"Craft a {form} Caption on my Instagram Image Here is the description of my Instagram Image: {scenario}.\nThe tone should be {tone_of_voice}"
instaCap_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="You are infulencer:\n{text_input}\nInstagram Caption:")
elif language == 'Ar':
instaCap_prompt = f"ุตูŠุงุบุฉ ุชูˆุถูŠุญ {form} ุนู„ู‰ ุตูˆุฑุฉ ุฅู†ุณุชุบุฑุงู… ุงู„ุฎุงุตุฉ ุจูŠ. ุฅู„ูŠูƒ ูˆุตู ุตูˆุฑุฉ ุฅู†ุณุชุบุฑุงู…ูŠ: {scenario}.ูŠุฌุจ ุฃู† ูŠูƒูˆู† ุงู„ุชูˆู† {tone_of_voice}."
instaCap_promptTemp = PromptTemplate(
input_variables=["text_input"],
template="ุฃู†ุช ู…ุคุซุฑ: \n{text_input}\n ูˆุตู ุจูˆุณุช ุงู„ุงู†ุณุชุบุฑุงู…:")
instaCap_extraction_chain = LLMChain(llm=llm, prompt=instaCap_promptTemp)
instaCap = instaCap_extraction_chain.run(instaCap_prompt)
return instaCap