File size: 4,446 Bytes
28c3b21
 
 
9d12e16
28c3b21
9d12e16
28c3b21
9d12e16
fed1cb1
9d12e16
 
 
 
 
 
28c3b21
9d12e16
 
 
28c3b21
 
 
 
9d12e16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28c3b21
9d12e16
 
 
 
 
 
28c3b21
9d12e16
28c3b21
 
9d12e16
 
 
 
 
 
 
 
 
28c3b21
 
 
 
9d12e16
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
124
125
126
127
128
129
130
131
132
133
134
import streamlit as st
from few_shot import FewShotPosts
from post_generator import generate_post
import json

# Improved app with more functionality
def main():
    st.set_page_config(page_title="LinkedIn Post Generator", layout="wide")
    st.title("πŸš€ LinkedIn Post Generator")
    st.markdown(
        """
        **Create impactful LinkedIn posts effortlessly.**  
        Customize your post with advanced options, analyze generated content, and save your creations.
        """
    )

    # Sidebar for advanced options
    st.sidebar.title("πŸ”§ Customization Options")
    st.sidebar.markdown("Use these settings to personalize your generated post.")

    fs = FewShotPosts()
    tags = fs.get_tags()

    # User input sections
    st.sidebar.subheader("Post Preferences")
    selected_tag = st.sidebar.selectbox("Topic (Tag):", options=tags)
    selected_length = st.sidebar.radio("Post Length:", ["Short", "Medium", "Long"])
    selected_language = st.sidebar.radio("Language:", ["English", "Hinglish"])
    selected_tone = st.sidebar.selectbox(
        "Tone/Style:", ["Motivational", "Professional", "Informal", "Neutral"]
    )
    custom_context = st.sidebar.text_area(
        "Additional Context (Optional):",
        placeholder="Provide extra details or a specific direction for your post...",
    )

    # History tracking
    if "generated_posts" not in st.session_state:
        st.session_state.generated_posts = []

    # Main content
    st.subheader("Generate Your LinkedIn Post")

    if st.button("Generate Post"):
        with st.spinner("Generating your post..."):
            try:
                # Generate post
                post = generate_post(selected_length, selected_language, selected_tag)

                # Add tone and context to the prompt dynamically
                if selected_tone:
                    post = f"**Tone**: {selected_tone}\n\n{post}"
                if custom_context:
                    post += f"\n\n**Context Added**: {custom_context}"

                # Save to session history
                st.session_state.generated_posts.append(post)

                st.success("Post generated successfully!")
                st.markdown("### Your LinkedIn Post:")
                st.write(post)

                # Post analysis
                st.markdown("### Post Analysis:")
                post_analysis(post)

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

    # Display session history
    if st.session_state.generated_posts:
        st.markdown("### Generated Posts History:")
        for i, history_post in enumerate(st.session_state.generated_posts):
            st.markdown(f"**Post {i + 1}:**")
            st.write(history_post)

    # Save and Download
    st.markdown("---")
    st.subheader("Save or Share Your Post")
    if st.session_state.generated_posts:
        if st.button("Download All Posts"):
            download_posts(st.session_state.generated_posts)
        if st.button("Clear History"):
            st.session_state.generated_posts = []
            st.info("History cleared!")

    # Footer
    st.markdown(
        """
        ---
        πŸ“ *Powered by OpenAI's ChatGroq and LangChain.*  
        πŸ“§ For feedback, contact: [support@example.com](mailto:support@example.com)
        """
    )


def post_analysis(post):
    """Perform basic analysis of the generated post."""
    word_count = len(post.split())
    char_count = len(post)
    tone_keywords = {
        "Motivational": ["inspire", "achieve", "goal", "success"],
        "Professional": ["expertise", "career", "opportunity", "industry"],
        "Informal": ["hey", "cool", "fun", "awesome"],
    }

    st.write(f"**Word Count:** {word_count}")
    st.write(f"**Character Count:** {char_count}")

    # Detect tone based on keywords (simple heuristic)
    detected_tone = "Neutral"
    for tone, keywords in tone_keywords.items():
        if any(keyword in post.lower() for keyword in keywords):
            detected_tone = tone
            break

    st.write(f"**Detected Tone:** {detected_tone}")


def download_posts(posts):
    """Allow users to download their posts."""
    posts_json = json.dumps(posts, indent=4)
    st.download_button(
        label="πŸ“₯ Download Posts",
        data=posts_json,
        file_name="generated_posts.json",
        mime="application/json",
    )


# Run the app
if __name__ == "__main__":
    main()