Spaces:
Sleeping
Sleeping
Update seo_agent/seo_agent.py
Browse files- seo_agent/seo_agent.py +40 -36
seo_agent/seo_agent.py
CHANGED
|
@@ -1,36 +1,40 @@
|
|
| 1 |
-
# seo_agent.py
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
from string import Template
|
| 5 |
-
from chat_wrapper import ChatRefiner
|
| 6 |
-
|
| 7 |
-
class SEOAgent:
|
| 8 |
-
def __init__(self, model_name="gemini-1.5-flash"):
|
| 9 |
-
base = os.path.dirname(__file__)
|
| 10 |
-
seo_path = os.path.join(base, "prompts", "seo_prompt.txt")
|
| 11 |
-
img_path = os.path.join(base, "prompts", "image_prompt.txt")
|
| 12 |
-
|
| 13 |
-
self.chat = ChatRefiner(model_name=model_name)
|
| 14 |
-
|
| 15 |
-
# load as dollar‐templates
|
| 16 |
-
with open(seo_path, encoding="utf-8") as f:
|
| 17 |
-
self.seo_template = Template(f.read())
|
| 18 |
-
with open(img_path, encoding="utf-8") as f:
|
| 19 |
-
self.img_template = Template(f.read())
|
| 20 |
-
|
| 21 |
-
def analyze_seo(self, html: str, text: str) -> str:
|
| 22 |
-
"""
|
| 23 |
-
Returns the raw SEO analysis text from the chat model.
|
| 24 |
-
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# seo_agent.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from string import Template
|
| 5 |
+
from chat_wrapper import ChatRefiner
|
| 6 |
+
|
| 7 |
+
class SEOAgent:
|
| 8 |
+
def __init__(self, model_name="gemini-1.5-flash"):
|
| 9 |
+
base = os.path.dirname(__file__)
|
| 10 |
+
seo_path = os.path.join(base, "prompts", "seo_prompt.txt")
|
| 11 |
+
img_path = os.path.join(base, "prompts", "image_prompt.txt")
|
| 12 |
+
|
| 13 |
+
self.chat = ChatRefiner(model_name=model_name)
|
| 14 |
+
|
| 15 |
+
# load as dollar‐templates
|
| 16 |
+
with open(seo_path, encoding="utf-8") as f:
|
| 17 |
+
self.seo_template = Template(f.read())
|
| 18 |
+
with open(img_path, encoding="utf-8") as f:
|
| 19 |
+
self.img_template = Template(f.read())
|
| 20 |
+
|
| 21 |
+
def analyze_seo(self, html: str, text: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Returns the raw SEO analysis text from the chat model.
|
| 24 |
+
"""
|
| 25 |
+
short_html = html[:5000]
|
| 26 |
+
short_text = text[:5000]
|
| 27 |
+
prompt = self.seo_template.safe_substitute(
|
| 28 |
+
html=short_html,
|
| 29 |
+
text=short_text,
|
| 30 |
+
)
|
| 31 |
+
return self.chat.answer(prompt)
|
| 32 |
+
|
| 33 |
+
def analyze_images(self, images: list[str], text: str) -> str:
|
| 34 |
+
limited_images = list(dict.fromkeys(images))[:10]
|
| 35 |
+
short_text = text[:5000]
|
| 36 |
+
prompt = self.img_template.safe_substitute(
|
| 37 |
+
images=limited_images, # pass the list directly
|
| 38 |
+
text=short_text,
|
| 39 |
+
)
|
| 40 |
+
return self.chat.answer(prompt)
|