|
import re |
|
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. |
|
- 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! π" |
|
- Topic: "Mental Health", Tone: "Professional", Language: "English" |
|
Closing Line: "Your mental well-being is essential. Letβs discuss ways to manage stress. π‘" |
|
- Topic: "Dating", Tone: "Informal", Language: "Hinglish" |
|
Closing Line: "Apka perfect date idea kya hai? Neeche share karein! π" |
|
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 extract_hashtags(content): |
|
""" |
|
Extract all hashtags from the given content. |
|
""" |
|
return re.findall(r"#\w+", content) |
|
|
|
|
|
def remove_hashtags(content): |
|
""" |
|
Remove all hashtags from the given content. |
|
""" |
|
return re.sub(r"#\w+", "", content).strip() |
|
|
|
|
|
def generate_post(length, language, tag, selected_tone=None): |
|
""" |
|
Generate a LinkedIn post dynamically with LLM, ensuring hashtags are added only at the end. |
|
""" |
|
|
|
prompt = get_prompt(length, language, tag) |
|
response = llm.invoke(prompt) |
|
post_content = response.content.strip() |
|
|
|
|
|
hashtags = extract_hashtags(post_content) |
|
post_content = remove_hashtags(post_content) |
|
|
|
|
|
closing_line = "" |
|
if selected_tone and tag: |
|
try: |
|
closing_line = generate_closing_line(language, tag, selected_tone) |
|
|
|
hashtags += extract_hashtags(closing_line) |
|
closing_line = remove_hashtags(closing_line).strip() |
|
except Exception as e: |
|
closing_line = "Thank you for reading. Your feedback is valued! π" |
|
|
|
|
|
full_post = f"{post_content}\n\n{closing_line}" |
|
|
|
|
|
if hashtags: |
|
unique_hashtags = " ".join(set(hashtags)) |
|
full_post += f"\n\n{unique_hashtags}" |
|
|
|
return full_post |
|
|
|
|
|
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. |
|
''' |
|
|
|
examples = few_shot.get_filtered_posts(length, language, tag) |
|
if examples: |
|
prompt += "\nExamples of great posts:\n" |
|
for i, post in enumerate(examples[:2]): |
|
post_text = post['text'] |
|
prompt += f"Example {i + 1}: {post_text}\n" |
|
|
|
prompt += "\nNow write the post." |
|
return prompt |
|
|
|
|
|
if __name__ == "__main__": |
|
print(generate_post("Medium", "English", "Mental Health")) |
|
|