Spaces:
Runtime error
Runtime error
File size: 8,400 Bytes
8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 dfc4131 8bc7dc5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
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 |