File size: 4,185 Bytes
8db7b5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import json
from copilot import build_global_graph

# Build global graph once
global_app = build_global_graph()

st.set_page_config(
    page_title="AI Marketing Copilot",
    page_icon="πŸ€–",
    layout="centered",
    initial_sidebar_state="collapsed",
)

# --- HEADER ---
st.image("logo.png", width=150)  # <- replace with your project logo
st.title("AI Marketing Copilot")
st.markdown("Your intelligent assistant for **post generation & scheduling** ✨")

st.divider()

# --- PRODUCT INPUT FORM ---
st.subheader("πŸ›’ Product Information")

with st.form("product_form"):
    # Required fields
    product_id = st.text_input("Product ID *", "PEN0001")
    product_name = st.text_input("Product Name *", "EcoWave Stainless Steel Insulated Bottle")
    product_category = st.text_input("Category *", "Drinkware")
    product_description = st.text_area(
        "Description *", 
        "Durable, eco-friendly insulated bottle for everyday use."
    )

    # Optional fields in expander
    with st.expander("πŸ”§ Advanced fields (optional)"):
        product_type = st.text_input("Type", "Bottle")
        product_price = st.text_input("Price", "24.99")
        product_currency = st.text_input("Currency", "USD")
        product_stock = st.number_input("Stock Quantity", value=42, step=1)
        product_sku = st.text_input("SKU", "ECO-SS-500")
        product_options = st.text_area(
            "Options (JSON list)", 
            '[{"name": "Size", "value": "500ml"}]'
        )
        product_on_sale = st.checkbox("On Sale?", value=True)

    # Platform selection
    platform = st.selectbox("πŸ“± Target Platform *", ["Instagram", "Twitter", "Facebook", "LinkedIn", "TikTok"])

    submitted = st.form_submit_button("πŸš€ Generate & Schedule")

if submitted:
    try:
        # Parse options safely
        try:
            options = json.loads(product_options)
            if not isinstance(options, list):
                options = []
        except Exception:
            options = []

        # Build product dict
        product = {
            "id": product_id,
            "name": product_name,
            "category": product_category,
            "type": product_type,
            "price": product_price,
            "currency": product_currency,
            "description": product_description,
            "stock_quantity": product_stock,
            "sku": product_sku,
            "images": [],
            "options": options,
            "on_sale": product_on_sale,
        }

        # Templates placeholder (normally loaded from DB)
        templates = []  

        with st.spinner("πŸ€– Generating post and scheduling..."):
            state = {
                "product": product,
                "platform": platform,
                "templates": templates,
            }
            result = global_app.invoke(state)

        st.success("βœ… Post Generated & Scheduled!")

        # --- MAIN OUTPUT CARD ---
        st.subheader("πŸ“’ Final Post")
        final_post = result.get("final_post_struct", {}).get("post_text", "⚠️ No post generated")
        st.markdown(
            f"""

            <div style="padding:1.2em; border-radius:10px; background-color:#F0F9FF; border:1px solid #90CAF9;">

                <p style="font-size:1.1em;">{final_post}</p>

            </div>

            """,
            unsafe_allow_html=True
        )

        # Scheduled time
        st.subheader("⏰ Scheduled Time")
        st.info(result.get("scheduled_post", {}).get("scheduled_time", "Not scheduled"))

        # --- EXTRA: Ranked Templates ---
        if st.button("πŸ“Š Show Template Rankings"):
            ranked = result.get("ranked_templates", [])
            if ranked:
                st.markdown("### Template Scores")
                st.dataframe(ranked)
            else:
                st.warning("No ranked templates available.")

    except Exception as e:
        st.error(f"An error occurred: {e}")

# --- FOOTER ---
st.divider()
st.caption("⚑ Powered by LangGraph + Hugging Face Spaces")