Upload Upload-Post smolagents tool
Browse files- app.py +5 -0
- requirements.txt +2 -0
- tool.py +83 -0
app.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import UploadPostVideoTool
|
| 3 |
+
|
| 4 |
+
tool = UploadPostVideoTool()
|
| 5 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
smolagents
|
| 2 |
+
upload_post
|
tool.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import upload_post
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
class UploadPostVideoTool(Tool):
|
| 7 |
+
name = "upload_post_video"
|
| 8 |
+
description = "Publish (or schedule) a video on social networks through Upload-Post: TikTok, Instagram, YouTube, LinkedIn, Facebook, X, Threads, Pinterest, Bluesky and more. The video must be a public http(s) URL. The upload is processed asynchronously: the returned dict contains a request_id (or a job_id when scheduled) to pass to upload_post_status."
|
| 9 |
+
inputs = {'video': {'type': 'string', 'description': 'Public http(s) URL of the video. Local file paths only work if the tool was created with allow_local_files=True.'}, 'platforms': {'type': 'array', 'description': "Platforms to publish to, e.g. ['tiktok', 'instagram', 'youtube']. Allowed: tiktok, instagram, youtube, linkedin, facebook, x, threads, pinterest, bluesky, discord, telegram, google_business, mastodon, wordpress."}, 'title': {'type': 'string', 'description': 'Title / caption of the video. Required for YouTube.', 'nullable': True}, 'description': {'type': 'string', 'description': 'Longer text used for LinkedIn commentary, Facebook and YouTube descriptions and Pinterest notes.', 'nullable': True}, 'profile': {'type': 'string', 'description': 'Upload-Post profile (username) to publish from. Omit to use the default profile. Use upload_post_profiles to list profiles.', 'nullable': True}, 'scheduled_date': {'type': 'string', 'description': "ISO-8601 date/time to schedule the post instead of publishing now, e.g. '2026-12-31T18:00:00Z'. Must be in the future (max 365 days).", 'nullable': True}, 'timezone': {'type': 'string', 'description': "IANA timezone (e.g. 'Europe/Madrid') for scheduled_date. Defaults to UTC.", 'nullable': True}, 'add_to_queue': {'type': 'boolean', 'description': "Put the post in the profile's next free queue slot. Not combinable with scheduled_date.", 'nullable': True}, 'first_comment': {'type': 'string', 'description': 'First comment posted right after publishing (Instagram, Facebook, Threads, Bluesky, X, YouTube, LinkedIn, TikTok).', 'nullable': True}}
|
| 10 |
+
output_type = "object"
|
| 11 |
+
|
| 12 |
+
def __init__(self, api_key=None, profile=None, allow_local_files=False):
|
| 13 |
+
super().__init__()
|
| 14 |
+
import os
|
| 15 |
+
|
| 16 |
+
self.api_key = api_key or os.environ.get("UPLOAD_POST_API_KEY")
|
| 17 |
+
self.profile = profile or os.environ.get("UPLOAD_POST_PROFILE")
|
| 18 |
+
self.allow_local_files = allow_local_files
|
| 19 |
+
self.client = None
|
| 20 |
+
|
| 21 |
+
def setup(self):
|
| 22 |
+
from upload_post import UploadPostClient
|
| 23 |
+
|
| 24 |
+
if not self.api_key:
|
| 25 |
+
raise ValueError(
|
| 26 |
+
"Missing Upload-Post API key: pass api_key=... or set UPLOAD_POST_API_KEY."
|
| 27 |
+
)
|
| 28 |
+
self.client = UploadPostClient(self.api_key)
|
| 29 |
+
self.is_initialized = True
|
| 30 |
+
|
| 31 |
+
def forward(
|
| 32 |
+
self,
|
| 33 |
+
video: str,
|
| 34 |
+
platforms: list,
|
| 35 |
+
title: Optional[str] = None,
|
| 36 |
+
description: Optional[str] = None,
|
| 37 |
+
profile: Optional[str] = None,
|
| 38 |
+
scheduled_date: Optional[str] = None,
|
| 39 |
+
timezone: Optional[str] = None,
|
| 40 |
+
add_to_queue: Optional[bool] = None,
|
| 41 |
+
first_comment: Optional[str] = None,
|
| 42 |
+
) -> Any:
|
| 43 |
+
import os
|
| 44 |
+
|
| 45 |
+
if not self.is_initialized:
|
| 46 |
+
self.setup()
|
| 47 |
+
user = profile or self.profile
|
| 48 |
+
if not user:
|
| 49 |
+
raise ValueError(
|
| 50 |
+
"No Upload-Post profile: pass profile=... or set UPLOAD_POST_PROFILE. "
|
| 51 |
+
"Call upload_post_profiles to list the available profiles."
|
| 52 |
+
)
|
| 53 |
+
if isinstance(platforms, str):
|
| 54 |
+
platforms = [p.strip() for p in platforms.split(",") if p.strip()]
|
| 55 |
+
if not platforms:
|
| 56 |
+
raise ValueError("At least one platform is required.")
|
| 57 |
+
if scheduled_date and add_to_queue:
|
| 58 |
+
raise ValueError("scheduled_date and add_to_queue cannot be used together.")
|
| 59 |
+
if not video.lower().startswith(("http://", "https://")):
|
| 60 |
+
if not self.allow_local_files:
|
| 61 |
+
raise ValueError(
|
| 62 |
+
"Local file paths are disabled (allow_local_files=False). "
|
| 63 |
+
"Pass a public http(s) URL instead."
|
| 64 |
+
)
|
| 65 |
+
video = os.path.expanduser(video)
|
| 66 |
+
if not os.path.isfile(video):
|
| 67 |
+
raise ValueError(f"File not found: {video}")
|
| 68 |
+
options = {
|
| 69 |
+
"description": description,
|
| 70 |
+
"scheduled_date": scheduled_date,
|
| 71 |
+
"timezone": timezone,
|
| 72 |
+
"add_to_queue": add_to_queue,
|
| 73 |
+
"first_comment": first_comment,
|
| 74 |
+
}
|
| 75 |
+
options = {k: v for k, v in options.items() if v is not None and v != ""}
|
| 76 |
+
return self.client.upload_video(
|
| 77 |
+
video,
|
| 78 |
+
title=title,
|
| 79 |
+
user=user,
|
| 80 |
+
platforms=list(platforms),
|
| 81 |
+
async_upload=True,
|
| 82 |
+
**options,
|
| 83 |
+
)
|