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

Update post_generator.py

Browse files
Files changed (1) hide show
  1. post_generator.py +8 -39
post_generator.py CHANGED
@@ -1,10 +1,8 @@
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"
@@ -41,53 +39,24 @@ def generate_closing_line(language, tag, 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,4 +82,4 @@ def get_prompt(length, language, tag):
113
 
114
 
115
  if __name__ == "__main__":
116
- print(generate_post("Medium", "English", "Mental Health"))
 
 
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"
 
39
  return response.content.strip()
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def generate_post(length, language, tag, selected_tone=None):
43
  """
44
+ Generate a LinkedIn post dynamically with LLM including a generated closing line.
45
  """
 
46
  prompt = get_prompt(length, language, tag)
47
  response = llm.invoke(prompt)
48
+ post_content = response.content
49
 
50
+ # Generate a dynamic closing line using LLM
 
 
 
 
 
51
  if selected_tone and tag:
52
  try:
53
  closing_line = generate_closing_line(language, tag, selected_tone)
54
+ post_content += f"\n\n\n{closing_line}"
 
 
55
  except Exception as e:
56
+ # Fallback in case of LLM failure
57
+ post_content += f"\n\nThank you for reading. Your feedback is valued! πŸ™Œ"
 
 
 
 
 
 
 
58
 
59
+ return post_content
60
 
61
 
62
  def get_prompt(length, language, tag):
 
82
 
83
 
84
  if __name__ == "__main__":
85
+ print(generate_post("Medium", "English", "Mental Health"))