Spaces:
Sleeping
Sleeping
Update post_generator.py
Browse files- post_generator.py +12 -14
post_generator.py
CHANGED
|
@@ -1,9 +1,6 @@
|
|
| 1 |
-
import few_shot
|
| 2 |
from llm_helper import llm
|
| 3 |
from few_shot import FewShotPosts
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
def get_length_str(length):
|
| 8 |
if length == "Short":
|
| 9 |
return "1 to 5 lines"
|
|
@@ -12,15 +9,20 @@ def get_length_str(length):
|
|
| 12 |
if length == "Long":
|
| 13 |
return "11 to 15 lines"
|
| 14 |
|
| 15 |
-
|
| 16 |
def generate_post(persona_name, length, language, tag):
|
|
|
|
|
|
|
|
|
|
| 17 |
few_shot = FewShotPosts(persona_name)
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
response = llm.invoke(prompt)
|
| 20 |
return response.content
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
length_str = get_length_str(length)
|
| 25 |
|
| 26 |
prompt = f'''
|
|
@@ -31,9 +33,9 @@ def get_prompt(length, language, tag):
|
|
| 31 |
3) Language: {language}
|
| 32 |
The script for the generated post should always be English.
|
| 33 |
'''
|
| 34 |
-
# prompt = prompt.format(post_topic=tag, post_length=length_str, post_language=language)
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
if len(examples) > 0:
|
| 39 |
prompt += "4) Use the writing style as per the following examples."
|
|
@@ -42,11 +44,7 @@ def get_prompt(length, language, tag):
|
|
| 42 |
post_text = post['text_blocks']
|
| 43 |
prompt += f'\n\n Example {i+1}: \n\n {post_text}'
|
| 44 |
|
| 45 |
-
if i == 1:
|
| 46 |
break
|
| 47 |
|
| 48 |
return prompt
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if __name__ == "__main__":
|
| 52 |
-
print(generate_post("Medium", "English", "Economy"))
|
|
|
|
|
|
|
| 1 |
from llm_helper import llm
|
| 2 |
from few_shot import FewShotPosts
|
| 3 |
|
|
|
|
|
|
|
| 4 |
def get_length_str(length):
|
| 5 |
if length == "Short":
|
| 6 |
return "1 to 5 lines"
|
|
|
|
| 9 |
if length == "Long":
|
| 10 |
return "11 to 15 lines"
|
| 11 |
|
|
|
|
| 12 |
def generate_post(persona_name, length, language, tag):
|
| 13 |
+
"""Generate a LinkedIn post based on persona and given criteria."""
|
| 14 |
+
|
| 15 |
+
# Create an instance of FewShotPosts with the persona name
|
| 16 |
few_shot = FewShotPosts(persona_name)
|
| 17 |
+
|
| 18 |
+
# Pass the instance to get_prompt
|
| 19 |
+
prompt = get_prompt(few_shot, length, language, tag)
|
| 20 |
response = llm.invoke(prompt)
|
| 21 |
return response.content
|
| 22 |
|
| 23 |
+
def get_prompt(few_shot_instance, length, language, tag):
|
| 24 |
+
"""Generate the prompt for LLM based on few-shot examples."""
|
| 25 |
+
|
| 26 |
length_str = get_length_str(length)
|
| 27 |
|
| 28 |
prompt = f'''
|
|
|
|
| 33 |
3) Language: {language}
|
| 34 |
The script for the generated post should always be English.
|
| 35 |
'''
|
|
|
|
| 36 |
|
| 37 |
+
# ✅ Fix: Call get_filtered_posts() on few_shot_instance, not module `few_shot`
|
| 38 |
+
examples = few_shot_instance.get_filtered_posts(length, language, tag)
|
| 39 |
|
| 40 |
if len(examples) > 0:
|
| 41 |
prompt += "4) Use the writing style as per the following examples."
|
|
|
|
| 44 |
post_text = post['text_blocks']
|
| 45 |
prompt += f'\n\n Example {i+1}: \n\n {post_text}'
|
| 46 |
|
| 47 |
+
if i == 1: # Use max two samples
|
| 48 |
break
|
| 49 |
|
| 50 |
return prompt
|
|
|
|
|
|
|
|
|
|
|
|