linkedin-post-generator / post_generator.py
DrishtiSharma's picture
Update post_generator.py
49c324c verified
raw
history blame
4.63 kB
from llm_helper import llm
from few_shot import FewShotPosts
few_shot = FewShotPosts()
def get_length_str(length):
if length == "Short":
return "1 to 5 lines"
if length == "Medium":
return "6 to 10 lines"
if length == "Long":
return "11 to 15 lines"
def generate_post(length, language, tag, selected_tone=None):
prompt = get_prompt(length, language, tag)
response = llm.invoke(prompt)
post_content = response.content
# Add closing statements dynamically based on tone and language
closing_lines = {
"Motivational": {
"English": [
"Stay inspired and keep moving forward! πŸ’ͺ",
"Your journey to greatness begins today. 🌟",
"Remember, small steps lead to big achievements. πŸš€",
"Keep pushing your boundaries. You’ve got this! πŸ”₯"
],
"Hinglish": [
"Dhire dhire aage badho aur safalta pao! πŸ’ͺ",
"Sapne bade rakho, unhe pura karo. 🌟",
"Chhoti chhoti koshis se bade lakshya hasil hote hain! πŸš€",
"Apne boundaries todho, aap kar sakte ho! πŸ”₯"
]
},
"Professional": {
"English": [
"Looking forward to hearing your thoughts! πŸ‘”",
"Let’s collaborate and make an impact together. 🌐",
"Feel free to share your perspective below. πŸ“’",
"Insights and feedback are always welcome. πŸ’‘"
],
"Hinglish": [
"Aapke vichar sunne ka intezaar hai! πŸ‘”",
"Chaliye milkar kuch asar karte hain. 🌐",
"Neeche apne vichar zaroor share karein. πŸ“’",
"Aapka feedback hamare liye mahatvapurn hai. πŸ’‘"
]
},
"Informal": {
"English": [
"What do you think? Let's chat in the comments! πŸ˜„",
"Drop your thoughts below, would love to hear from you! πŸ—¨οΈ",
"Let’s keep the conversation rolling. Share your views! πŸš€",
"Got something to add? Don’t hold back! 😎"
],
"Hinglish": [
"Kya sochte ho? Comments mein baat karte hain! πŸ˜„",
"Apne vichar neeche likho, sunne ke liye excited hoon! πŸ—¨οΈ",
"Charcha ko jaari rakhein, apne vichar share karein! πŸš€",
"Kuch add karna hai? Sharmana mat, batao! 😎"
]
},
"Neutral": {
"English": [
"Thank you for reading. Your feedback is valued! πŸ™Œ",
"Let’s connect and share ideas. 🌟",
"Looking forward to engaging with you in the comments. πŸ’¬",
"Your thoughts are always appreciated. 😊"
],
"Hinglish": [
"Padhne ke liye dhanyavaad. Aapka feedback mahatvapurn hai! πŸ™Œ",
"Chaliye judein aur naye ideas share karein. 🌟",
"Aapke vichar comments mein padhne ka intezaar hai. πŸ’¬",
"Aapke sujhav hamesha sarankrit hote hain. 😊"
]
}
}
if selected_tone and selected_tone in closing_lines:
if language == "Hinglish" and "Hinglish" in closing_lines[selected_tone]:
closing_line = closing_lines[selected_tone]["Hinglish"]
else:
closing_line = closing_lines[selected_tone]["English"]
# Randomly select a closing line
import random
post_content += f"\n\n{random.choice(closing_line)}"
return post_content
def get_prompt(length, language, tag):
length_str = get_length_str(length)
prompt = f'''
Generate a LinkedIn post using the below information. No preamble.
1) Topic: {tag}
2) Length: {length_str}
3) Language: {language}
If Language is Hinglish then it means it is a mix of Hindi and English.
The script for the generated post should always be English.
'''
# prompt = prompt.format(post_topic=tag, post_length=length_str, post_language=language)
examples = few_shot.get_filtered_posts(length, language, tag)
if len(examples) > 0:
prompt += "4) Use the writing style as per the following examples."
for i, post in enumerate(examples):
post_text = post['text']
prompt += f'\n\n Example {i+1}: \n\n {post_text}'
if i == 1: # Use max two samples
break
return prompt
if __name__ == "__main__":
print(generate_post("Medium", "English", "Mental Health"))