Upload content_optimizer.py
Browse files- agents/content_optimizer.py +13 -5
agents/content_optimizer.py
CHANGED
|
@@ -41,14 +41,22 @@ class ContentOptimizationAgent:
|
|
| 41 |
num_beams=4,
|
| 42 |
early_stopping=True
|
| 43 |
)
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def optimize_title(self, content, keyword):
|
| 47 |
if not hasattr(self, 'title_model'): return ""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def analyze_page(self, url):
|
| 54 |
logger.info(f"Optimizing content for: {url}")
|
|
|
|
| 41 |
num_beams=4,
|
| 42 |
early_stopping=True
|
| 43 |
)
|
| 44 |
+
try:
|
| 45 |
+
return self.meta_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.error(f"Meta gen failed: {e}")
|
| 48 |
+
return "Could not generate meta description."
|
| 49 |
|
| 50 |
def optimize_title(self, content, keyword):
|
| 51 |
if not hasattr(self, 'title_model'): return ""
|
| 52 |
+
try:
|
| 53 |
+
prompt = f"Write an SEO-optimized page title under 60 characters for content about '{keyword}': {content[:500]}"
|
| 54 |
+
inputs = self.title_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 55 |
+
outputs = self.title_model.generate(inputs["input_ids"], max_length=60, num_beams=5)
|
| 56 |
+
return self.title_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
+
except Exception as e:
|
| 58 |
+
logger.error(f"Title gen failed: {e}")
|
| 59 |
+
return "Could not generate title."
|
| 60 |
|
| 61 |
def analyze_page(self, url):
|
| 62 |
logger.info(f"Optimizing content for: {url}")
|