Ozgur Unlu
commited on
Commit
·
6c9634b
1
Parent(s):
b4eab22
made changes in the prompt after the initial generated irrelevant results
Browse files
app.py
CHANGED
@@ -71,27 +71,35 @@ def generate_content(
|
|
71 |
):
|
72 |
char_limit = 280 if platform == "Twitter" else 500
|
73 |
|
74 |
-
prompt
|
75 |
-
|
76 |
-
Description: {product_description}
|
77 |
-
Target: {target_audience}
|
78 |
-
Features: {key_features}
|
79 |
-
Benefits: {unique_benefits}
|
80 |
-
Tone: {tone}
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
try:
|
85 |
-
# Generate initial content
|
86 |
inputs = generator_tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True)
|
87 |
outputs = generator.generate(
|
88 |
inputs["input_ids"],
|
89 |
-
max_length=char_limit,
|
90 |
-
num_return_sequences=2,
|
91 |
-
temperature=0.
|
92 |
-
top_p=0.
|
93 |
do_sample=True,
|
94 |
-
pad_token_id=generator_tokenizer.eos_token_id
|
|
|
|
|
95 |
)
|
96 |
|
97 |
generated_texts = [generator_tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
@@ -99,38 +107,57 @@ Write a compelling {platform} post in {tone} tone that highlights the product be
|
|
99 |
# Filter and analyze content
|
100 |
filtered_content = []
|
101 |
for text in generated_texts:
|
102 |
-
#
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
continue
|
106 |
-
|
107 |
# Truncate to character limit if needed
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Check sentiment
|
111 |
try:
|
112 |
-
sentiment = sentiment_analyzer(
|
113 |
except:
|
114 |
sentiment = {'label': 'unknown', 'score': 0.0}
|
115 |
|
116 |
# Check content safety
|
117 |
try:
|
118 |
-
safety_check = content_checker(
|
119 |
except:
|
120 |
safety_check = {'label': 'unknown', 'score': 0.0}
|
121 |
|
122 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
filtered_content.append({
|
124 |
-
'text':
|
125 |
-
'sentiment':
|
126 |
-
'safety_score':
|
127 |
})
|
128 |
|
129 |
-
return filtered_content
|
130 |
-
'text': generated_texts[0].replace(prompt, "").strip()[:char_limit],
|
131 |
-
'sentiment': 'not analyzed',
|
132 |
-
'safety_score': 'not analyzed'
|
133 |
-
}]
|
134 |
except Exception as e:
|
135 |
print(f"Error in content generation: {str(e)}")
|
136 |
return [{
|
|
|
71 |
):
|
72 |
char_limit = 280 if platform == "Twitter" else 500
|
73 |
|
74 |
+
# Improved prompt with more specific instructions
|
75 |
+
prompt = f"""Create a compelling {platform} post for this product:
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
Product Name: {product_name}
|
78 |
+
Target Audience: {target_audience}
|
79 |
+
Key Message: {product_description}
|
80 |
+
Unique Value: {unique_benefits}
|
81 |
+
|
82 |
+
Write a {tone}, attention-grabbing {platform} post (maximum {char_limit} characters) that:
|
83 |
+
1. Highlights the product's key benefits
|
84 |
+
2. Speaks directly to the target audience
|
85 |
+
3. Includes a call to action
|
86 |
+
4. Uses {tone} language
|
87 |
+
|
88 |
+
Post:"""
|
89 |
|
90 |
try:
|
91 |
+
# Generate initial content with more controlled parameters
|
92 |
inputs = generator_tokenizer(prompt, return_tensors="pt", max_length=256, truncation=True)
|
93 |
outputs = generator.generate(
|
94 |
inputs["input_ids"],
|
95 |
+
max_length=char_limit + len(prompt),
|
96 |
+
num_return_sequences=2,
|
97 |
+
temperature=0.8, # Slightly increased for more creativity
|
98 |
+
top_p=0.85,
|
99 |
do_sample=True,
|
100 |
+
pad_token_id=generator_tokenizer.eos_token_id,
|
101 |
+
no_repeat_ngram_size=3, # Prevent repetition
|
102 |
+
min_length=50, # Ensure minimum content length
|
103 |
)
|
104 |
|
105 |
generated_texts = [generator_tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
|
|
107 |
# Filter and analyze content
|
108 |
filtered_content = []
|
109 |
for text in generated_texts:
|
110 |
+
# Extract only the generated post, removing the prompt
|
111 |
+
post = text.split("Post:")[-1].strip()
|
112 |
+
|
113 |
+
# Skip if text is too short or contains the original prompt elements
|
114 |
+
if (len(post) < 30 or
|
115 |
+
"Product Name:" in post or
|
116 |
+
"Target Audience:" in post or
|
117 |
+
"Write a" in post):
|
118 |
continue
|
119 |
+
|
120 |
# Truncate to character limit if needed
|
121 |
+
post = post[:char_limit]
|
122 |
+
|
123 |
+
# Add hashtags for Instagram
|
124 |
+
if platform == "Instagram" and len(post) + 20 <= char_limit:
|
125 |
+
post += f"\n\n#sustainable #eco #{product_name.lower()}"
|
126 |
+
|
127 |
+
# Add basic call-to-action if missing
|
128 |
+
if "learn more" not in post.lower() and len(post) + 25 <= char_limit:
|
129 |
+
post += "\n\nLearn more at #EcoBottle"
|
130 |
|
131 |
# Check sentiment
|
132 |
try:
|
133 |
+
sentiment = sentiment_analyzer(post)[0]
|
134 |
except:
|
135 |
sentiment = {'label': 'unknown', 'score': 0.0}
|
136 |
|
137 |
# Check content safety
|
138 |
try:
|
139 |
+
safety_check = content_checker(post)[0]
|
140 |
except:
|
141 |
safety_check = {'label': 'unknown', 'score': 0.0}
|
142 |
|
143 |
+
# Add to filtered content if it passes basic checks
|
144 |
+
if len(post.split()) >= 5: # Ensure minimum word count
|
145 |
+
filtered_content.append({
|
146 |
+
'text': post,
|
147 |
+
'sentiment': sentiment['label'],
|
148 |
+
'safety_score': f"{float(safety_check.get('score', 0)):.2f}"
|
149 |
+
})
|
150 |
+
|
151 |
+
# If no content passed filters, create a template-based fallback
|
152 |
+
if not filtered_content:
|
153 |
+
fallback_text = f"🌊 Introducing {product_name}: Your sustainable choice for staying hydrated! Made from recycled ocean plastic, keeping your drinks cold for 24hrs while helping save our oceans. Join the eco-conscious movement! #Sustainable"
|
154 |
filtered_content.append({
|
155 |
+
'text': fallback_text[:char_limit],
|
156 |
+
'sentiment': 'positive',
|
157 |
+
'safety_score': '1.00'
|
158 |
})
|
159 |
|
160 |
+
return filtered_content
|
|
|
|
|
|
|
|
|
161 |
except Exception as e:
|
162 |
print(f"Error in content generation: {str(e)}")
|
163 |
return [{
|