File size: 4,064 Bytes
6f28dc4
765a4ee
 
 
 
 
6f28dc4
765a4ee
 
 
 
 
 
 
 
 
45fc613
 
 
 
 
 
 
 
6ca5a11
45fc613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f28dc4
 
 
 
 
 
 
 
 
 
 
 
 
 
f7a52fb
7692c8e
6f28dc4
7692c8e
6f28dc4
765a4ee
 
6f28dc4
 
 
 
 
f7a52fb
6f28dc4
 
45fc613
 
 
6f28dc4
 
 
45fc613
6f28dc4
 
 
 
 
 
 
 
 
f7a52fb
6f28dc4
f7a52fb
765a4ee
0c02355
765a4ee
 
0c02355
e1a012a
0c02355
 
 
 
 
765a4ee
 
e1a012a
 
 
 
0c02355
 
 
765a4ee
 
 
 
6f28dc4
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
import re  # For handling hashtags
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.
    """
    # Generate the main content
    prompt = get_prompt(length, language, tag)
    response = llm.invoke(prompt)
    post_content = response.content.strip()  # Clean up whitespace

    # Extract and remove hashtags from the main content
    hashtags = extract_hashtags(post_content)
    post_content = remove_hashtags(post_content)

    # Generate the closing line
    closing_line = ""
    if selected_tone and tag:
        try:
            closing_line = generate_closing_line(language, tag, selected_tone)
            # Extract and remove hashtags from the closing line
            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! πŸ™Œ"

    # Combine the cleaned main content and closing line
    full_post = f"{post_content}\n\n{closing_line}"

    # Add unique hashtags at the end of the post
    if hashtags:
        unique_hashtags = " ".join(set(hashtags))  # Remove duplicates
        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]):  # Limit to 2 examples
            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"))