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 X_Generator: | |
""" | |
X_Generator: A class designed to generate various Twitter-related content using language models. | |
This class contains methods that leverage language models to generate diverse content | |
for Twitter campaigns, retweet comments, Twitter threads, bios, and converting articles | |
into coherent Twitter threads. | |
""" | |
def x_camp_gen(self, product_name:str, product_desc:str, goal:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a Twitter campaign prompt based on product information. | |
Parameters: | |
- product_name (str): Name of the product. | |
- product_desc (str): Description of the product. | |
- goal (str): Goal or objective of the campaign. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- x_camp (str): Generated content for the Twitter 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': | |
x_camp_prompt = f"Design a potent Twitter campaign for my product '{product_name}'. {product_name} is {product_desc} aiming to accomplish {goal} through the meticulous adoption of planning and content creation best practices.\n\nCampaign will include: Campaign Teaser, Educational Content Series, Customer Testimonials and Case Studies, Interactive Content, Limited-Time Offer Announcement, Call-to-Action for Consultation and Recap and Thank You." | |
x_camp_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a Specialist in Twitter Copywriting\n\n{text_input}\n\nMake the tweets engaging, creative and coherent") | |
elif language == 'Ar': | |
x_camp_prompt = f"قم بتصميم حملة تويتر قوية لمنتجي '{product_name}'. {product_name} هو {product_desc} يهدف إلى تحقيق {goal} من خلال اعتماد دقيق على أفضل الممارسات في التخطيط وإنشاء المحتوى.\n\nستتضمن الحملة: العرض التشويقي للحملة، سلسلة محتوى تعليمي، شهادات العملاء ودراسات الحالة، محتوى تفاعلي، إعلان عن عرض لفترة محدودة، دعوة للعمل والاستشارة، وإعادة النظر والشكر." | |
x_camp_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت متخصص في كتابة التغريدات على تويتر\n\n{text_input}\n\nاجعل التغريدات جذابة وإبداعية ومترابطة") | |
x_camp_extraction_chain = LLMChain(llm=llm, prompt=x_camp_promptTemp) | |
x_camp = x_camp_extraction_chain.run(x_camp_prompt) | |
return x_camp | |
def x_retweet_commenting_gen(self, tweet:str, tone_of_voice:str, language = 'En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a X Retweet Commenting based on the quote or commenting tweet. | |
Parameters: | |
- tweet (str): The Quoted or commented tweet. | |
- tone_of_voice (str): tone of voice of the generated content. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- x_retweet_comment (str): Generated content for the Twitter 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': | |
x_retweet_comment_prompt = f"I'm planning to retweet the following tweet:\n“{tweet}”\nConstruct 5 varied comments I could append to this retweet. tone should be {tone_of_voice}" | |
x_retweet_comment_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are Specilaized Twitter Copywriter\n{text_input}\n\nRetweet Comment:") | |
elif language == 'Ar': | |
x_retweet_comment_prompt = f"أنا أخطط لإعادة نشر التغريدة التالية:\n“{tweet}”\nقم بصياغة 5 تعليقات متنوعة يمكنني إلحاقها بهذا الإعادة. يجب أن يكون النبرة {tone_of_voice}." | |
x_retweet_comment_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب نصوص تويتر متخصص\n{text_input}\n\nتعليق إعادة التغريد:") | |
x_retweet_comment_extraction_chain = LLMChain(llm=llm, prompt=x_retweet_comment_promptTemp) | |
x_retweet_comment = x_retweet_comment_extraction_chain.run(x_retweet_comment_prompt) | |
return x_retweet_comment | |
def x_thread_gen_intro(self, topic:str, thread:str, tone_of_voice:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a X Retweet Commenting based on the quote or commenting tweet. | |
Parameters: | |
- tweet (str): The Quoted or commented tweet. | |
- tone_of_voice (str): tone of voice of the generated content. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- x_retweet_comment (str): Generated content for the Twitter 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': | |
x_thread_intro_prompt = f"Write a an engagging and attractive Introduction head tweet for my twitter thread on {topic}. here is my twitter thread:\n{thread}\nTone should be {tone_of_voice}" | |
x_thread_intro_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a Twitter Content Creator:\n{text_input}\n\nIntroduction Tweet:") | |
elif language == 'Ar': | |
x_thread_intro_prompt = f"اكتب تغريدة تعريفية جذابة وجذابة للثريد الخاص بي على تويتر {topic}. إليك الثريد الخاص بي على تويتر:\n{thread}\nيجب أن يكون الأسلوب {tone_of_voice}" | |
x_thread_intro_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت مُنشئ محتوى على تويتر:\n{text_input}\n\n التغريدة التعريفية:") | |
x_thread_intro_extraction_chain = LLMChain(llm=llm, prompt=x_thread_intro_promptTemp) | |
x_thread_intro = x_thread_intro_extraction_chain.run(x_thread_intro_prompt) | |
return x_thread_intro | |
def x_thread_gen(self, topic:str, num_tweets:int, tone_of_voice:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Generates a X Retweet Commenting based on the quote or commenting tweet. | |
Parameters: | |
- tweet (str): The Quoted or commented tweet. | |
- tone_of_voice (str): tone of voice of the generated content. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- x_retweet_comment (str): Generated content for the Twitter 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': | |
x_thread_prompt = f"Write a an engagging Twitter Thread on '{topic}' consists of {num_tweets} tweets. Tone should be {tone_of_voice}" | |
x_thread_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a Twitter Content Creator:\n{text_input}\n\nTwitter Thread:") | |
elif language == 'Ar': | |
x_thread_prompt = f"اكتب سلسلة تغريدات جذابة (ثريد) عن '{topic}' تتكون من {num_tweets} الثريد. يجب أن يكون اللهجة {tone_of_voice}." | |
x_thread_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت مُنشئ محتوى على تويتر:\n{text_input}\n\n الثريد:") | |
x_thread_extraction_chain = LLMChain(llm=llm, prompt=x_thread_promptTemp) | |
x_thread = x_thread_extraction_chain.run(x_thread_prompt) | |
return x_thread | |
def x_bio_gen(self, info:str, tone_of_voice:str, language='En', model_name = 'Google Palm 2', creativity = 'Original')->str: | |
""" | |
Generates an X Bio based on given Profile. | |
Parameters: | |
- info (str): Information about the X user. | |
- tone_of_voice (str): tone of voice of the generated X Bio. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- x_bio (str): Generated X Bio for The X user. | |
""" | |
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': | |
x_bio_prompt = f"Craft a personalized Twitter bio for me, based on the content I create, related to {info}. tone should be {tone_of_voice}, Produce 10 tailor-made Twitter bios for me." | |
x_bio_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a copywriter\n{text_input}\n\nTwitter Bios:") | |
elif language == 'Ar': | |
x_bio_prompt = f"قم بكتابة لي سيرة ذاتية شخصية على تويتر، مستندة إلى المحتوى الذي أنشئه، ذات صلة بـ {info}. يجب أن يكون النبرة {tone_of_voice}. أنتج ١٠ سير ذاتية مُصمَّمة خصيصًا لتويتر بالنسبة لي." | |
x_bio_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب نسخ\n{text_input}\n\n السير الذاتية لتويتر:") | |
x_bio_extraction_chain = LLMChain(llm=llm, prompt=x_bio_promptTemp) | |
x_bio = x_bio_extraction_chain.run(x_bio_prompt) | |
return x_bio | |
def article_to_x_thread_gen(self, article:str, language='En', model_name='Google Palm 2', creativity='Original')->str: | |
""" | |
Converts an article into a coherent Twitter thread. | |
Parameters: | |
- article (str): The article content to be transformed. | |
- language (str): Opitonal Parameter -> The language of the model. | |
- 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: | |
- article_to_x_thread (str): Generated coherent Twitter thread based on the article. | |
""" | |
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': | |
article_to_x_thread_prompt = f"Transform the ensuing article into a sequence of Twitter posts, creating a coherent Twitter thread between 5 to 20 tweets\nArticle: “{article}.“" | |
article_to_x_thread_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a twitter copywriter\n{text_input}\n\nCoherent Twitter Thread:") | |
elif language == 'Ar': | |
article_to_x_thread_prompt = f"حوّل المقال الآتي إلى سلسلة من تغريدات تويتر، مكوّناً سلسلة تويتر مترابطة تتكون من 5 إلى 20 تغريدة. المقال : {article}" | |
article_to_x_thread_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت كاتب نصوص تويتر\n{text_input}\n\nسلسلة تويتر متناسقة:") | |
article_to_x_thread_extraction_chain = LLMChain(llm=llm, prompt=article_to_x_thread_promptTemp) | |
article_to_x_thread = article_to_x_thread_extraction_chain.run(article_to_x_thread_prompt) | |
return article_to_x_thread |