Spaces:
Sleeping
Sleeping
rsm-roguchi commited on
Commit ·
06d3276
1
Parent(s): d465b34
update meta prompting
Browse files- server/meta.py +56 -25
server/meta.py
CHANGED
|
@@ -2,7 +2,8 @@ from shiny import reactive, render, ui
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
load_dotenv()
|
| 8 |
|
|
@@ -11,24 +12,51 @@ ACCESS_TOKEN = os.getenv("FB_PAGE_ACCESS_TOKEN")
|
|
| 11 |
|
| 12 |
generated_fb_post = reactive.Value("")
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if url:
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
|
| 19 |
-
f"Write a
|
| 20 |
-
f"
|
| 21 |
-
f"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
post = get_response(
|
| 31 |
-
input=
|
| 32 |
template=lambda x: x.strip(),
|
| 33 |
llm="gemini",
|
| 34 |
md=False,
|
|
@@ -39,14 +67,12 @@ def generate_facebook_post(topic: str, url: str = "", max_len: int = 500) -> str
|
|
| 39 |
if len(post) <= max_len:
|
| 40 |
return post
|
| 41 |
|
| 42 |
-
# Step 2: Shorten if needed
|
| 43 |
shorten_prompt = (
|
| 44 |
-
f"Shorten this Facebook post to {max_len} characters or fewer
|
| 45 |
-
f"{
|
| 46 |
-
|
| 47 |
-
|
| 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(
|
|
@@ -58,8 +84,7 @@ def generate_facebook_post(topic: str, url: str = "", max_len: int = 500) -> str
|
|
| 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"
|
|
@@ -82,10 +107,16 @@ def server(input, output, session):
|
|
| 82 |
@render.ui
|
| 83 |
@reactive.event(input.gen_btn_fb)
|
| 84 |
def fb_post_draft():
|
| 85 |
-
topic = input.fb_topic()
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
generated_fb_post.set(post)
|
| 90 |
return ui.HTML(f"<p><strong>Generated Facebook Post:</strong><br>{post}</p>")
|
| 91 |
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
from llm_connect import get_response
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
| 12 |
|
| 13 |
generated_fb_post = reactive.Value("")
|
| 14 |
|
| 15 |
+
def scrape_shopify_blog(url: str) -> str:
|
| 16 |
+
try:
|
| 17 |
+
resp = requests.get(url, timeout=10)
|
| 18 |
+
soup = BeautifulSoup(resp.content, 'html.parser')
|
| 19 |
+
section = soup.find(
|
| 20 |
+
'div',
|
| 21 |
+
class_='article-template__content page-width page-width--narrow rte scroll-trigger animate--slide-in'
|
| 22 |
+
)
|
| 23 |
+
return section.get_text(strip=True, separator=' ') if section else ""
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"❌ Error scraping blog: {e}"
|
| 26 |
+
|
| 27 |
+
def generate_facebook_post(topic: str = "", url: str = "", min_len: int = 500, max_len: int = 1000) -> str:
|
| 28 |
+
# Handle blog scraping
|
| 29 |
if url:
|
| 30 |
+
scraped = scrape_shopify_blog(url)
|
| 31 |
+
if scraped.startswith("❌") or not scraped.strip():
|
| 32 |
+
return f"⚠️ Failed to extract blog content from URL: {url}"
|
| 33 |
+
prompt = (
|
| 34 |
f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
|
| 35 |
+
f"Write a detailed, engaging Facebook post (min {min_len} characters, max {max_len} characters) summarizing the following Shopify blog:\n\n"
|
| 36 |
+
f"{scraped}\n\n"
|
| 37 |
+
f"The post MUST include this exact URL: {url}\n"
|
| 38 |
+
f"Use a casual, friendly tone with emojis.\n"
|
| 39 |
+
f"VERY IMPORTANT: The post MUST include exactly 5 to 10 SEO-relevant hashtags at the end.\n"
|
| 40 |
+
f"Do not include hashtags inline — list them as a group at the end of the post.\n"
|
| 41 |
+
f"Keep everything under {max_len} characters total."
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
elif topic:
|
| 45 |
+
prompt = (
|
| 46 |
f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n"
|
| 47 |
+
f"Write a short, engaging Facebook post (min {min_len} characters, max {max_len} characters) about: '{topic}'.\n"
|
| 48 |
+
f"Use a casual, friendly tone with fun emojis.\n"
|
| 49 |
+
f"VERY IMPORTANT: Include exactly 5 to 10 SEO-relevant hashtags grouped at the end of the post.\n"
|
| 50 |
+
f"Do NOT place hashtags inline — they must be in a clean list at the end.\n"
|
| 51 |
+
f"The entire post must be under {max_len} characters total."
|
| 52 |
)
|
| 53 |
|
| 54 |
+
else:
|
| 55 |
+
return "⚠️ Provide either a topic or a Shopify blog URL."
|
| 56 |
+
|
| 57 |
+
# Step 1: Generate initial post
|
| 58 |
post = get_response(
|
| 59 |
+
input=prompt,
|
| 60 |
template=lambda x: x.strip(),
|
| 61 |
llm="gemini",
|
| 62 |
md=False,
|
|
|
|
| 67 |
if len(post) <= max_len:
|
| 68 |
return post
|
| 69 |
|
| 70 |
+
# Step 2: Shorten if needed
|
| 71 |
shorten_prompt = (
|
| 72 |
+
f"Shorten this Facebook post to {max_len} characters or fewer.\n"
|
| 73 |
+
f"You MUST keep the Shopify blog link ({url}) and all original hashtags:\n\n{post}"
|
| 74 |
+
if url else
|
| 75 |
+
f"Shorten this Facebook post to {max_len} characters or fewer, keeping the tone, emojis, and all original hashtags:\n\n{post}"
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
shortened = get_response(
|
|
|
|
| 84 |
max_tokens=200
|
| 85 |
).strip()
|
| 86 |
|
| 87 |
+
return shortened[:max_len]
|
|
|
|
| 88 |
|
| 89 |
def post_to_facebook(message: str) -> str:
|
| 90 |
url = f"https://graph.facebook.com/{PAGE_ID}/feed"
|
|
|
|
| 107 |
@render.ui
|
| 108 |
@reactive.event(input.gen_btn_fb)
|
| 109 |
def fb_post_draft():
|
| 110 |
+
topic = input.fb_topic().strip()
|
| 111 |
+
url = input.fb_url().strip()
|
| 112 |
+
|
| 113 |
+
if not topic and not url:
|
| 114 |
+
return ui.HTML("<p><strong>⚠️ Enter a topic or a blog URL.</strong></p>")
|
| 115 |
+
|
| 116 |
+
post = generate_facebook_post(topic=topic, url=url)
|
| 117 |
+
if post.startswith("⚠️") or post.startswith("❌"):
|
| 118 |
+
return ui.HTML(f"<p><strong>{post}</strong></p>")
|
| 119 |
+
|
| 120 |
generated_fb_post.set(post)
|
| 121 |
return ui.HTML(f"<p><strong>Generated Facebook Post:</strong><br>{post}</p>")
|
| 122 |
|