linkedin-post-generator / post_generator.py
DrishtiSharma's picture
Update post_generator.py
77e9507 verified
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_closing_line(language, tag, tone):
"""
Generate a closing line using the LLM based on language, tag, and tone.
"""
closing_prompt = f"""
You are writing a LinkedIn post. Create a concise and engaging closing line.
- The closing line should reflect the topic: "{tag}".
- Use the tone/style: "{tone}".
- Add hashtags only at the end, i.e., after the closing line and not before it. Ensure that hashtags are placed one line below the closing line, leaving a decent space for better readability and structure.
- The closing line must encourage engagement or provide a call to action, relevant to the topic.
- Use the language: "{language}" (Hinglish means Hindi phrases written in English script).
Examples:
- Topic: "Job Search", Tone: "Motivational", Language: "English"
Closing Line: "Your dream job is closer than you think. Stay determined! πŸš€ #DreamJob #StayMotivated"
- Topic: "Mental Health", Tone: "Professional", Language: "English"
Closing Line: "Your mental well-being is essential. Let’s discuss ways to manage stress. πŸ’‘ #MentalHealth #StressManagement"
- Topic: "Dating", Tone: "Informal", Language: "Hinglish"
Closing Line: "Apka perfect date idea kya hai? Neeche share karein! πŸ˜„ #DatingTips #FunDates"
Now, write a relevant closing line for the following inputs:
Topic: "{tag}"
Tone: "{tone}"
Language: "{language}"
"""
response = llm.invoke(closing_prompt)
return response.content.strip()
def generate_post(length, language, tag, selected_tone=None):
"""
Generate a LinkedIn post dynamically with LLM including a generated closing line.
"""
prompt = get_prompt(length, language, tag)
response = llm.invoke(prompt)
post_content = response.content.strip()
# Generate a dynamic closing line using LLM
if selected_tone and tag:
try:
closing_line = generate_closing_line(language, tag, selected_tone)
# Append the closing line with hashtags
post_content += f"\n\n{closing_line}\n\n"
except Exception as e:
# Fallback in case of LLM failure
post_content += f"\n\nThank you for reading. Your feedback is valued! πŸ™Œ"
return post_content
def get_prompt(length, language, tag):
length_str = get_length_str(length)
prompt = f'''
Write a professional, engaging LinkedIn post.
1. Topic: "{tag}"
2. Post Length: "{length_str}"
3. Language: "{language}" (Hinglish means Hindi phrases written in English script).
4. Incorporate creativity, enthusiasm, emotional appeal, and actionable advice.
5. Do not include hashtags in the main content.
'''
examples = few_shot.get_filtered_posts(length, language, tag)
if examples:
prompt += "\nExamples of great posts:\n"
for i, post in enumerate(examples[:2]): # Limit to 2 examples
post_text = post['text']
# Remove hashtags from examples
cleaned_post_text = " ".join(word for word in post_text.split() if not word.startswith("#"))
prompt += f"Example {i + 1}: {cleaned_post_text}\n"
prompt += "\nNow write the post."
return prompt
if __name__ == "__main__":
print(generate_post("Medium", "English", "Mental Health"))