File size: 2,972 Bytes
3f91f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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)