import gradio as gr from workflows.main_flow import instagram_autoposter_flow from database.operations import SessionLocal, InstagramPostRecord from prefect.deployments import run_deployment import schedule import threading import time from datetime import datetime def run_pipeline_manually(): """Manually trigger the pipeline""" try: instagram_autoposter_flow() return "✅ Pipeline executed successfully!" except Exception as e: return f"❌ Error: {str(e)}" def get_recent_posts(limit=10): """Get recent posts from database""" db = SessionLocal() try: posts = db.query(InstagramPostRecord).order_by( InstagramPostRecord.created_at.desc() ).limit(limit).all() data = [] for post in posts: data.append([ post.id, post.rss_entry_id[:20] + "...", post.status, post.instagram_post_id or "N/A", post.created_at.strftime("%Y-%m-%d %H:%M") if post.created_at else "N/A", post.error_message[:50] if post.error_message else "" ]) return data finally: db.close() def schedule_pipeline(): """Run pipeline on schedule""" schedule.every(1).hours.do(instagram_autoposter_flow) while True: schedule.run_pending() time.sleep(60) # Start scheduler in background scheduler_thread = threading.Thread(target=schedule_pipeline, daemon=True) scheduler_thread.start() # Create Gradio interface with gr.Blocks(title="Instagram Auto-Poster") as demo: gr.Markdown("# 📸 Instagram Auto-Posting System") gr.Markdown("Automated RSS to Instagram pipeline with CrewAI and Prefect") with gr.Tab("Manual Run"): run_btn = gr.Button("🚀 Run Pipeline Now", variant="primary") output = gr.Textbox(label="Status", lines=3) run_btn.click(run_pipeline_manually, outputs=output) with gr.Tab("Recent Posts"): refresh_btn = gr.Button("🔄 Refresh") posts_table = gr.Dataframe( headers=["ID", "Entry", "Status", "Instagram ID", "Created", "Error"], label="Recent Posts" ) refresh_btn.click(get_recent_posts, outputs=posts_table) # Auto-load on tab open demo.load(get_recent_posts, outputs=posts_table) with gr.Tab("Configuration"): gr.Markdown(""" ### Environment Variables Required: - `OPENROUTER_API_KEY`: Your OpenRouter API key - `INSTAGRAM_ACCOUNT_ID`: Instagram Business Account ID - `INSTAGRAM_ACCESS_TOKEN`: Instagram Access Token - `GOOGLE_CREDENTIALS_JSON`: Google Service Account credentials - `RSS_FEEDS`: Comma-separated RSS feed URLs ### Schedule: Pipeline runs automatically every hour. """) if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)