Spaces:
Sleeping
Sleeping
| from shiny import reactive, render, ui | |
| import os | |
| import requests | |
| from dotenv import load_dotenv | |
| from llm_connect import get_response # your existing function | |
| load_dotenv() | |
| PAGE_ID = os.getenv("FB_PAGE_ID") | |
| ACCESS_TOKEN = os.getenv("FB_PAGE_ACCESS_TOKEN") | |
| generated_fb_post = reactive.Value("") | |
| def generate_facebook_post(topic: str) -> str: | |
| prompt = ( | |
| f"You are a social media manager for a hobby e-commerce company called 'Ultima Supply'.\n" | |
| f"Write a short, engaging Facebook post (max 500 characters) about: '{topic}'.\n" | |
| f"Use casual and friendly tone. Include emojis and 3-5 relevant hashtags." | |
| ) | |
| return get_response( | |
| input=prompt, | |
| template=lambda x: x, | |
| llm="gemini", | |
| md=False, | |
| temperature=0.9, | |
| max_tokens=500 | |
| ) | |
| def post_to_facebook(message: str) -> str: | |
| url = f"https://graph.facebook.com/{PAGE_ID}/feed" | |
| payload = { | |
| "message": message, | |
| "access_token": ACCESS_TOKEN | |
| } | |
| response = requests.post(url, data=payload) | |
| if response.status_code != 200: | |
| return f"❌ Facebook post failed: {response.status_code} - {response.text}" | |
| post_id = response.json().get("id", "Unknown") | |
| return f"✅ Post successful! Facebook Post ID: {post_id}" | |
| def server(input, output, session): | |
| post_status = reactive.Value("") | |
| def fb_post_draft(): | |
| topic = input.fb_topic() | |
| if not topic.strip(): | |
| return ui.HTML("<p><strong>⚠️ Enter a topic first.</strong></p>") | |
| post = generate_facebook_post(topic) | |
| generated_fb_post.set(post) | |
| return ui.HTML(f"<p><strong>Generated Facebook Post:</strong><br>{post}</p>") | |
| def fb_post_status(): | |
| return post_status() | |
| def _(): | |
| post = generated_fb_post() | |
| if not post: | |
| post_status.set("⚠️ No post generated yet.") | |
| else: | |
| result = post_to_facebook(post) | |
| post_status.set(result) | |