Spaces:
Sleeping
Sleeping
Commit ·
c92a951
1
Parent(s): cd969b0
removed wordpress_auth
Browse files- app.py +1 -8
- tools/wordpress_auth.py +0 -46
- tools/wordpress_post.py +11 -27
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import requests
|
|
| 6 |
import pytz
|
| 7 |
import yaml
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
| 9 |
-
from tools.wordpress_auth import WordPressAuthTool
|
| 10 |
from tools.wordpress_post import WordPressPostTool
|
| 11 |
from tools.blog_generator import BlogGeneratorTool
|
| 12 |
from tools.visit_webpage import VisitWebpageTool
|
|
@@ -148,14 +147,9 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 148 |
prompt_templates = yaml.safe_load(stream)
|
| 149 |
|
| 150 |
# Initialize WordPress tools
|
| 151 |
-
|
| 152 |
-
wordpress_post = WordPressPostTool(auth_tool=wordpress_auth)
|
| 153 |
blog_generator = BlogGeneratorTool(model=model)
|
| 154 |
|
| 155 |
-
# Set WordPress credentials
|
| 156 |
-
wordpress_auth.forward(action="set_credentials",
|
| 157 |
-
credentials=wordpress_credentials)
|
| 158 |
-
|
| 159 |
agent = CodeAgent(
|
| 160 |
model=model,
|
| 161 |
tools=[
|
|
@@ -164,7 +158,6 @@ agent = CodeAgent(
|
|
| 164 |
image_generation_tool,
|
| 165 |
quick_research,
|
| 166 |
get_weather,
|
| 167 |
-
wordpress_auth,
|
| 168 |
wordpress_post,
|
| 169 |
blog_generator,
|
| 170 |
visit_webpage,
|
|
|
|
| 6 |
import pytz
|
| 7 |
import yaml
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 9 |
from tools.wordpress_post import WordPressPostTool
|
| 10 |
from tools.blog_generator import BlogGeneratorTool
|
| 11 |
from tools.visit_webpage import VisitWebpageTool
|
|
|
|
| 147 |
prompt_templates = yaml.safe_load(stream)
|
| 148 |
|
| 149 |
# Initialize WordPress tools
|
| 150 |
+
wordpress_post = WordPressPostTool(wordpress_credentials)
|
|
|
|
| 151 |
blog_generator = BlogGeneratorTool(model=model)
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
agent = CodeAgent(
|
| 154 |
model=model,
|
| 155 |
tools=[
|
|
|
|
| 158 |
image_generation_tool,
|
| 159 |
quick_research,
|
| 160 |
get_weather,
|
|
|
|
| 161 |
wordpress_post,
|
| 162 |
blog_generator,
|
| 163 |
visit_webpage,
|
tools/wordpress_auth.py
DELETED
|
@@ -1,46 +0,0 @@
|
|
| 1 |
-
from typing import Dict, Optional
|
| 2 |
-
from smolagents.tools import Tool
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
class WordPressAuthTool(Tool):
|
| 6 |
-
name = "wordpress_auth"
|
| 7 |
-
description = "Manages WordPress authentication credentials"
|
| 8 |
-
inputs = {
|
| 9 |
-
'action': {'type': 'string', 'description': 'Action to perform: set_credentials or get_credentials'},
|
| 10 |
-
'credentials': {
|
| 11 |
-
'type': 'object',
|
| 12 |
-
'description': 'WordPress credentials dictionary containing site_url, username, and app_password',
|
| 13 |
-
'nullable': True
|
| 14 |
-
}
|
| 15 |
-
}
|
| 16 |
-
output_type = "object"
|
| 17 |
-
|
| 18 |
-
def __init__(self):
|
| 19 |
-
super().__init__()
|
| 20 |
-
self._credentials: Optional[Dict] = None
|
| 21 |
-
|
| 22 |
-
def forward(self, action: str, credentials: Optional[Dict] = None) -> Dict:
|
| 23 |
-
"""Manages WordPress credentials
|
| 24 |
-
Args:
|
| 25 |
-
action: Either 'set_credentials' or 'get_credentials'
|
| 26 |
-
credentials: Dict containing site_url, username, and app_password (only for set_credentials)
|
| 27 |
-
Returns:
|
| 28 |
-
Dict containing the current credentials or status message
|
| 29 |
-
"""
|
| 30 |
-
if action == "set_credentials":
|
| 31 |
-
if not credentials:
|
| 32 |
-
return {"error": "Credentials required for set_credentials action"}
|
| 33 |
-
|
| 34 |
-
required_fields = ["site_url", "username", "app_password"]
|
| 35 |
-
if not all(field in credentials for field in required_fields):
|
| 36 |
-
return {"error": f"Missing required fields. Need: {required_fields}"}
|
| 37 |
-
|
| 38 |
-
self._credentials = credentials
|
| 39 |
-
return {"status": "Credentials stored successfully"}
|
| 40 |
-
|
| 41 |
-
elif action == "get_credentials":
|
| 42 |
-
if not self._credentials:
|
| 43 |
-
return {"error": "No credentials set. Please set credentials first."}
|
| 44 |
-
return self._credentials
|
| 45 |
-
|
| 46 |
-
return {"error": "Invalid action. Use 'set_credentials' or 'get_credentials'"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/wordpress_post.py
CHANGED
|
@@ -18,9 +18,9 @@ class WordPressPostTool(Tool):
|
|
| 18 |
}
|
| 19 |
output_type = "object"
|
| 20 |
|
| 21 |
-
def __init__(self,
|
| 22 |
super().__init__()
|
| 23 |
-
self.
|
| 24 |
|
| 25 |
def forward(self, title: str, content: str, status: str = "publish") -> Dict:
|
| 26 |
"""Publishes a post to WordPress
|
|
@@ -29,18 +29,14 @@ class WordPressPostTool(Tool):
|
|
| 29 |
content: The content in HTML format
|
| 30 |
status: Post status (publish, draft, private)
|
| 31 |
Returns:
|
| 32 |
-
Dict containing
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if "error" in creds:
|
| 37 |
-
return creds
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"Content-Type": "application/json"
|
| 43 |
-
}
|
| 44 |
|
| 45 |
data = {
|
| 46 |
"title": title,
|
|
@@ -49,20 +45,8 @@ class WordPressPostTool(Tool):
|
|
| 49 |
}
|
| 50 |
|
| 51 |
try:
|
| 52 |
-
response = requests.post(
|
| 53 |
-
url,
|
| 54 |
-
json=data,
|
| 55 |
-
headers=headers,
|
| 56 |
-
auth=HTTPBasicAuth(creds['username'], creds['app_password'])
|
| 57 |
-
)
|
| 58 |
response.raise_for_status()
|
| 59 |
-
return {
|
| 60 |
-
"status": "success",
|
| 61 |
-
"message": "Post published successfully!",
|
| 62 |
-
"url": response.json().get('link')
|
| 63 |
-
}
|
| 64 |
except requests.exceptions.RequestException as e:
|
| 65 |
-
return {
|
| 66 |
-
"status": "error",
|
| 67 |
-
"message": f"Error publishing post: {str(e)}"
|
| 68 |
-
}
|
|
|
|
| 18 |
}
|
| 19 |
output_type = "object"
|
| 20 |
|
| 21 |
+
def __init__(self, wordpress_credentials):
|
| 22 |
super().__init__()
|
| 23 |
+
self.credentials = wordpress_credentials
|
| 24 |
|
| 25 |
def forward(self, title: str, content: str, status: str = "publish") -> Dict:
|
| 26 |
"""Publishes a post to WordPress
|
|
|
|
| 29 |
content: The content in HTML format
|
| 30 |
status: Post status (publish, draft, private)
|
| 31 |
Returns:
|
| 32 |
+
Dict containing the response from WordPress
|
| 33 |
"""
|
| 34 |
+
if not self.credentials:
|
| 35 |
+
return {"error": "WordPress credentials not configured"}
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
api_url = f"{self.credentials['site_url']}/wp-json/wp/v2/posts"
|
| 38 |
+
auth = HTTPBasicAuth(
|
| 39 |
+
self.credentials['username'], self.credentials['app_password'])
|
|
|
|
|
|
|
| 40 |
|
| 41 |
data = {
|
| 42 |
"title": title,
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
response = requests.post(api_url, json=data, auth=auth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
response.raise_for_status()
|
| 50 |
+
return {"status": "success", "post_id": response.json().get("id")}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
+
return {"error": f"Failed to publish post: {str(e)}"}
|
|
|
|
|
|
|
|
|