Spaces:
Runtime error
Runtime error
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 email_writing: | |
""" | |
A class for generating emails and email marketing campaigns using a language model. | |
""" | |
def email_gen(self, recipient:str, recipient_position:str, sender_name:str, position_sender:str, desc:str, language='En', model_name='Google Palm 2', creativity='Original') -> str: | |
""" | |
Generates an email based on provided details. | |
Args: | |
recipient (str): Name of the email recipient. | |
recipient_position (str): Position of the email recipient. | |
sender_name (str): Name of the email sender. | |
position_sender (str): Position of the email sender. | |
desc (str): Description or context for the email. | |
language (str): the language of the generated content (optional, default value is 'EN'). | |
creativity (str): Optional Parameter -> Controling the randomness of the model. Default value is Original | |
model_name (str): Optional Parameter -> select the LLM model. Default Value is Google Palm 2 | |
Returns: | |
email (str): Generated email content. | |
""" | |
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': | |
email_prompt = f"Write a professional and well organized Email on {desc}.\nThe name of the Recipient is {recipient} and the recipient position is {recipient_position}.\nMy Name is {sender_name} and my Position is {position_sender}." | |
email_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a professional email writer:\n{text_input}\nEmail:") | |
elif language == 'Ar': | |
email_prompt = f"اكتب بريدًا إلكترونيًا احترافيًا ومنظمًا حول {desc}.\nاسم المستلم هو {recipient} وموقع المستلم هو {recipient_position}.\nاسمي هو {sender_name} وموقعي هو {position_sender}." | |
email_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب بريد إلكتروني محترف:\n{text_input}\nالبريد الإلكتروني:") | |
email_extraction_chain = LLMChain(llm=llm, prompt=email_promptTemp) | |
email = email_extraction_chain.run(email_prompt) | |
return email | |
def email_subject_gen(self, email:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a subject for a given email. | |
Args: | |
email (str): Email content for which the subject needs to be generated. | |
language (str): the language of the generated content (optional, default value is 'EN'). | |
creativity (str): Optional Parameter -> Controling the randomness of the model. Default value is Original | |
model_name (str): Optional Parameter -> select the LLM model. Default Value is Google Palm 2 | |
Returns: | |
email_subject (str): Generated email subject. | |
""" | |
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': | |
email_subject_prompt = f"Generate a subject for the following email:\n{email}\n" | |
email_subject_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a professional email writer:\n{text_input}\nEmail Subject:") | |
elif language == 'Ar': | |
email_subject_prompt = f"أنشئ موضوعًا للبريد الإلكتروني التالي:\n{email}\n" | |
email_subject_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب بريد إلكتروني محترف:\n{text_input}\nعنوان البريد الإلكتروني:") | |
email_subject_extraction_chain = LLMChain(llm=llm, prompt=email_subject_promptTemp) | |
email_subject = email_subject_extraction_chain.run(email_subject_prompt) | |
return email_subject | |
def email_marketing_campaigns_gen(self, product_name:str, product_description:str, target_audience:str, goal:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a subject for a given email. | |
Args: | |
product_name (str): Name of the product. | |
product_description (str): Description of the product. | |
target_audience (str): Targeted audience for the campaign. | |
goal (str): Goal of the Campaign. | |
language (str): the language of the generated content (optional, default value is 'EN'). | |
Returns: | |
email (str): Generated emails marketing campaigns. | |
""" | |
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': | |
email_prompt = f"Generate a high-converting email marketing campaign for {product_name}. {product_name} is {product_description}. that is targeted at {target_audience} and has the goal of {goal}. The campaign should include a welcome email, a nurture sequence, and a promotional email." | |
email_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a Professional Email Marketing Copywriter:\n{text_input}\nEmail Marketing Campaign:") | |
elif language == 'Ar': | |
email_prompt = f"أنشئ حملة تسويق بريد إلكتروني فعّالة لـ {product_name}. {product_name} هو {product_description}. مستهدفة لـ {target_audience} ولها هدف {goal}. يجب أن تتضمن الحملة رسالة ترحيب، سلسلة تغذية، ورسالة ترويجية." | |
email_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب نصوص تسويق البريد الإلكتروني المحترف:\n{text_input}\nحملة تسويق البريد الإلكتروني:") | |
email_extraction_chain = LLMChain(llm=llm, prompt=email_promptTemp) | |
email = email_extraction_chain.run(email_prompt) | |
return email |