Spaces:
Sleeping
Sleeping
rsm-roguchi commited on
Commit ·
d465b34
1
Parent(s): d24e71f
update
Browse files- server/meta.py +35 -23
- ui/meta.py +7 -9
server/meta.py
CHANGED
|
@@ -11,13 +11,21 @@ ACCESS_TOKEN = os.getenv("FB_PAGE_ACCESS_TOKEN")
|
|
| 11 |
|
| 12 |
generated_fb_post = reactive.Value("")
|
| 13 |
|
| 14 |
-
def generate_facebook_post(topic: str, max_len: int = 500) -> str:
|
| 15 |
-
# Step 1:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
post = get_response(
|
| 23 |
input=gen_prompt,
|
|
@@ -28,26 +36,30 @@ def generate_facebook_post(topic: str, max_len: int = 500) -> str:
|
|
| 28 |
max_tokens=250
|
| 29 |
).strip()
|
| 30 |
|
| 31 |
-
# Step 2: If too long, ask Gemini to shorten it
|
| 32 |
if len(post) <= max_len:
|
| 33 |
return post
|
| 34 |
-
else:
|
| 35 |
-
shorten_prompt = (
|
| 36 |
-
f"Shorten this Facebook post to {max_len} characters or fewer, but keep the friendly tone, emojis, and all original hashtags:\n\n"
|
| 37 |
-
f"{post}\n\n"
|
| 38 |
-
f"Only return the shortened post. Do not remove hashtags or change tone."
|
| 39 |
-
)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
return shortened[:max_len] # Hard cap fallback
|
| 51 |
|
| 52 |
def post_to_facebook(message: str) -> str:
|
| 53 |
url = f"https://graph.facebook.com/{PAGE_ID}/feed"
|
|
|
|
| 11 |
|
| 12 |
generated_fb_post = reactive.Value("")
|
| 13 |
|
| 14 |
+
def generate_facebook_post(topic: str, url: str = "", max_len: int = 500) -> str:
|
| 15 |
+
# Step 1: Generate with/without URL context
|
| 16 |
+
if url:
|
| 17 |
+
gen_prompt = (
|
| 18 |
+
f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
|
| 19 |
+
f"Write a short, engaging Facebook post (max {max_len} characters) about: '{topic}'.\n"
|
| 20 |
+
f"The post MUST include this URL: {url}\n"
|
| 21 |
+
f"Use a casual and friendly tone. Include emojis and 3–5 SEO-relevant hashtags. Keep it under the character limit."
|
| 22 |
+
)
|
| 23 |
+
else:
|
| 24 |
+
gen_prompt = (
|
| 25 |
+
f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
|
| 26 |
+
f"Write a short, engaging Facebook post (max {max_len} characters) about: '{topic}'.\n"
|
| 27 |
+
f"Use a casual and friendly tone. Include emojis and 3–5 SEO-relevant hashtags. Keep it under the character limit."
|
| 28 |
+
)
|
| 29 |
|
| 30 |
post = get_response(
|
| 31 |
input=gen_prompt,
|
|
|
|
| 36 |
max_tokens=250
|
| 37 |
).strip()
|
| 38 |
|
|
|
|
| 39 |
if len(post) <= max_len:
|
| 40 |
return post
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Step 2: Shorten if needed, preserving the link
|
| 43 |
+
shorten_prompt = (
|
| 44 |
+
f"Shorten this Facebook post to {max_len} characters or fewer, but you MUST keep the Shopify blog link ({url}) and all original hashtags:\n\n"
|
| 45 |
+
f"{post}\n\n"
|
| 46 |
+
f"Only return the shortened version."
|
| 47 |
+
) if url else (
|
| 48 |
+
f"Shorten this Facebook post to {max_len} characters or fewer, keeping the tone, emojis, and all original hashtags:\n\n"
|
| 49 |
+
f"{post}"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
shortened = get_response(
|
| 53 |
+
input=shorten_prompt,
|
| 54 |
+
template=lambda x: x.strip(),
|
| 55 |
+
llm="gemini",
|
| 56 |
+
md=False,
|
| 57 |
+
temperature=0.7,
|
| 58 |
+
max_tokens=200
|
| 59 |
+
).strip()
|
| 60 |
+
|
| 61 |
+
return shortened
|
| 62 |
|
|
|
|
| 63 |
|
| 64 |
def post_to_facebook(message: str) -> str:
|
| 65 |
url = f"https://graph.facebook.com/{PAGE_ID}/feed"
|
ui/meta.py
CHANGED
|
@@ -3,27 +3,25 @@ from shiny import ui
|
|
| 3 |
ui = ui.nav_panel(
|
| 4 |
"Facebook Poster",
|
| 5 |
|
| 6 |
-
#
|
| 7 |
ui.input_text("fb_topic", "Enter Topic", placeholder="e.g. New Anime Drop This Friday"),
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Generate button
|
| 10 |
ui.div(
|
| 11 |
ui.input_action_button("gen_btn_fb", "Generate Facebook Post", class_="btn-primary"),
|
| 12 |
class_="mt-3 mb-2"
|
| 13 |
),
|
| 14 |
|
| 15 |
-
#
|
| 16 |
ui.output_ui("fb_post_draft"),
|
| 17 |
-
|
| 18 |
-
# Post button
|
| 19 |
ui.div(
|
| 20 |
ui.input_action_button("post_btn_fb", "Post to Facebook", class_="btn-success"),
|
| 21 |
class_="my-3"
|
| 22 |
),
|
| 23 |
|
| 24 |
-
# Status
|
| 25 |
-
ui.output_text("fb_post_status")
|
| 26 |
-
|
| 27 |
-
# Optional: spacing/padding at bottom
|
| 28 |
-
class_="p-4"
|
| 29 |
)
|
|
|
|
| 3 |
ui = ui.nav_panel(
|
| 4 |
"Facebook Poster",
|
| 5 |
|
| 6 |
+
# Topic input
|
| 7 |
ui.input_text("fb_topic", "Enter Topic", placeholder="e.g. New Anime Drop This Friday"),
|
| 8 |
|
| 9 |
+
# Optional blog link
|
| 10 |
+
ui.input_text("fb_url", "Optional Blog Link", placeholder="https://yourshop.com/blogs/xyz"),
|
| 11 |
+
|
| 12 |
# Generate button
|
| 13 |
ui.div(
|
| 14 |
ui.input_action_button("gen_btn_fb", "Generate Facebook Post", class_="btn-primary"),
|
| 15 |
class_="mt-3 mb-2"
|
| 16 |
),
|
| 17 |
|
| 18 |
+
# Post output and button
|
| 19 |
ui.output_ui("fb_post_draft"),
|
|
|
|
|
|
|
| 20 |
ui.div(
|
| 21 |
ui.input_action_button("post_btn_fb", "Post to Facebook", class_="btn-success"),
|
| 22 |
class_="my-3"
|
| 23 |
),
|
| 24 |
|
| 25 |
+
# Status output
|
| 26 |
+
ui.output_text("fb_post_status")
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|