DrishtiSharma commited on
Commit
6f28dc4
Β·
verified Β·
1 Parent(s): 6ca5a11

Update post_generator.py

Browse files
Files changed (1) hide show
  1. post_generator.py +39 -12
post_generator.py CHANGED
@@ -1,8 +1,10 @@
 
1
  from llm_helper import llm
2
  from few_shot import FewShotPosts
3
 
4
  few_shot = FewShotPosts()
5
 
 
6
  def get_length_str(length):
7
  if length == "Short":
8
  return "1 to 5 lines"
@@ -23,17 +25,13 @@ def generate_closing_line(language, tag, tone):
23
  - Add hashtags only at the end, i.e., after the closing line and not before it.
24
  - The closing line must encourage engagement or provide a call to action, relevant to the topic.
25
  - Use the language: "{language}" (Hinglish means Hindi phrases written in English script).
26
-
27
  Examples:
28
  - Topic: "Job Search", Tone: "Motivational", Language: "English"
29
  Closing Line: "Your dream job is closer than you think. Stay determined! πŸš€"
30
-
31
  - Topic: "Mental Health", Tone: "Professional", Language: "English"
32
  Closing Line: "Your mental well-being is essential. Let’s discuss ways to manage stress. πŸ’‘"
33
-
34
  - Topic: "Dating", Tone: "Informal", Language: "Hinglish"
35
  Closing Line: "Apka perfect date idea kya hai? Neeche share karein! πŸ˜„"
36
-
37
  Now, write a relevant closing line for the following inputs:
38
  Topic: "{tag}"
39
  Tone: "{tone}"
@@ -43,24 +41,53 @@ def generate_closing_line(language, tag, tone):
43
  return response.content.strip()
44
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  def generate_post(length, language, tag, selected_tone=None):
47
  """
48
- Generate a LinkedIn post dynamically with LLM including a generated closing line.
49
  """
 
50
  prompt = get_prompt(length, language, tag)
51
  response = llm.invoke(prompt)
52
- post_content = response.content
 
 
 
 
53
 
54
- # Generate a dynamic closing line using LLM
 
55
  if selected_tone and tag:
56
  try:
57
  closing_line = generate_closing_line(language, tag, selected_tone)
58
- post_content += f"\n\n\n{closing_line}"
 
 
59
  except Exception as e:
60
- # Fallback in case of LLM failure
61
- post_content += f"\n\nThank you for reading. Your feedback is valued! πŸ™Œ"
 
 
 
 
 
 
 
62
 
63
- return post_content
64
 
65
 
66
  def get_prompt(length, language, tag):
@@ -86,4 +113,4 @@ def get_prompt(length, language, tag):
86
 
87
 
88
  if __name__ == "__main__":
89
- print(generate_post("Medium", "English", "Mental Health"))
 
1
+ import re # For handling hashtags
2
  from llm_helper import llm
3
  from few_shot import FewShotPosts
4
 
5
  few_shot = FewShotPosts()
6
 
7
+
8
  def get_length_str(length):
9
  if length == "Short":
10
  return "1 to 5 lines"
 
25
  - Add hashtags only at the end, i.e., after the closing line and not before it.
26
  - The closing line must encourage engagement or provide a call to action, relevant to the topic.
27
  - Use the language: "{language}" (Hinglish means Hindi phrases written in English script).
 
28
  Examples:
29
  - Topic: "Job Search", Tone: "Motivational", Language: "English"
30
  Closing Line: "Your dream job is closer than you think. Stay determined! πŸš€"
 
31
  - Topic: "Mental Health", Tone: "Professional", Language: "English"
32
  Closing Line: "Your mental well-being is essential. Let’s discuss ways to manage stress. πŸ’‘"
 
33
  - Topic: "Dating", Tone: "Informal", Language: "Hinglish"
34
  Closing Line: "Apka perfect date idea kya hai? Neeche share karein! πŸ˜„"
 
35
  Now, write a relevant closing line for the following inputs:
36
  Topic: "{tag}"
37
  Tone: "{tone}"
 
41
  return response.content.strip()
42
 
43
 
44
+ def extract_hashtags(content):
45
+ """
46
+ Extract all hashtags from the given content.
47
+ """
48
+ return re.findall(r"#\w+", content)
49
+
50
+
51
+ def remove_hashtags(content):
52
+ """
53
+ Remove all hashtags from the given content.
54
+ """
55
+ return re.sub(r"#\w+", "", content).strip()
56
+
57
+
58
  def generate_post(length, language, tag, selected_tone=None):
59
  """
60
+ Generate a LinkedIn post dynamically with LLM, ensuring hashtags are added only at the end.
61
  """
62
+ # Generate the main content
63
  prompt = get_prompt(length, language, tag)
64
  response = llm.invoke(prompt)
65
+ post_content = response.content.strip() # Clean up whitespace
66
+
67
+ # Extract and remove hashtags from the main content
68
+ hashtags = extract_hashtags(post_content)
69
+ post_content = remove_hashtags(post_content)
70
 
71
+ # Generate the closing line
72
+ closing_line = ""
73
  if selected_tone and tag:
74
  try:
75
  closing_line = generate_closing_line(language, tag, selected_tone)
76
+ # Extract and remove hashtags from the closing line
77
+ hashtags += extract_hashtags(closing_line)
78
+ closing_line = remove_hashtags(closing_line).strip()
79
  except Exception as e:
80
+ closing_line = "Thank you for reading. Your feedback is valued! πŸ™Œ"
81
+
82
+ # Combine the cleaned main content and closing line
83
+ full_post = f"{post_content}\n\n{closing_line}"
84
+
85
+ # Add unique hashtags at the end of the post
86
+ if hashtags:
87
+ unique_hashtags = " ".join(set(hashtags)) # Remove duplicates
88
+ full_post += f"\n\n{unique_hashtags}"
89
 
90
+ return full_post
91
 
92
 
93
  def get_prompt(length, language, tag):
 
113
 
114
 
115
  if __name__ == "__main__":
116
+ print(generate_post("Medium", "English", "Mental Health"))