Spaces:
Running
Running
| import streamlit as st | |
| from groq import Groq | |
| import os | |
| # ------------------ PAGE CONFIG ------------------ | |
| st.set_page_config( | |
| page_title="AI LinkedIn Post Generator", | |
| page_icon="πΌ", | |
| layout="centered" | |
| ) | |
| # ------------------ CUSTOM CSS ------------------ | |
| st.markdown(""" | |
| <style> | |
| body { | |
| background: linear-gradient(135deg, #4facfe, #00f2fe); | |
| } | |
| .main { | |
| background-color: white; | |
| padding: 25px; | |
| border-radius: 15px; | |
| } | |
| h1, h2, h3 { | |
| color: #1f2937; | |
| font-weight: 800; | |
| } | |
| p, label { | |
| color: #374151; | |
| font-weight: 600; | |
| font-size: 16px; | |
| } | |
| .stButton>button { | |
| background-color: #2563eb; | |
| color: white; | |
| font-weight: bold; | |
| border-radius: 8px; | |
| padding: 10px; | |
| } | |
| .stTextInput>div>div>input, .stTextArea textarea { | |
| background-color: #f9fafb; | |
| color: black; | |
| font-weight: 600; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # ------------------ TITLE ------------------ | |
| st.title("πΌ AI LinkedIn Post Generator") | |
| st.write("Let's craft an engaging LinkedIn post step-by-step π") | |
| # Initialize session state variables | |
| if 'step' not in st.session_state: | |
| st.session_state['step'] = 1 | |
| if 'topic' not in st.session_state: | |
| st.session_state['topic'] = '' | |
| if 'tone' not in st.session_state: | |
| st.session_state['tone'] = '' | |
| if 'audience' not in st.session_state: | |
| st.session_state['audience'] = '' | |
| if 'length' not in st.session_state: | |
| st.session_state['length'] = 150 | |
| if 'post' not in st.session_state: | |
| st.session_state['post'] = '' | |
| # Step 1: Input Topic | |
| if st.session_state['step'] == 1: | |
| st.subheader("Step 1: What's the topic?") | |
| topic_input = st.text_input("Enter the topic for your LinkedIn post", value=st.session_state['topic']) | |
| if st.button("Next"): | |
| st.session_state['topic'] = topic_input | |
| st.session_state['step'] = 2 | |
| # Step 2: Input Tone | |
| elif st.session_state['step'] == 2: | |
| st.subheader("Step 2: What's the tone?") | |
| tone_input = st.text_input("E.g., professional, friendly, inspiring", value=st.session_state['tone']) | |
| if st.button("Next"): | |
| st.session_state['tone'] = tone_input | |
| st.session_state['step'] = 3 | |
| if st.button("Back"): | |
| st.session_state['step'] = 1 | |
| # Step 3: Input Audience | |
| elif st.session_state['step'] == 3: | |
| st.subheader("Step 3: Who's the audience?") | |
| audience_input = st.text_input("E.g., marketers, developers", value=st.session_state['audience']) | |
| if st.button("Next"): | |
| st.session_state['audience'] = audience_input | |
| st.session_state['step'] = 4 | |
| if st.button("Back"): | |
| st.session_state['step'] = 2 | |
| # Step 4: Input Length | |
| elif st.session_state['step'] == 4: | |
| st.subheader("Step 4: How long should the post be?") | |
| length_input = st.slider("Number of words", min_value=50, max_value=500, value=st.session_state['length']) | |
| if st.button("Generate Post"): | |
| st.session_state['length'] = length_input | |
| # Proceed to generate the post | |
| # Fetch API key | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| st.error("β οΈ API key not found! Add GROQ_API_KEY in Hugging Face Secrets.") | |
| else: | |
| client = Groq(api_key=api_key) | |
| prompt = f""" | |
| Write a LinkedIn post with: | |
| Topic: {st.session_state['topic']} | |
| Tone: {st.session_state['tone']} | |
| Audience: {st.session_state['audience']} | |
| Length: {st.session_state['length']} words | |
| Make it engaging, clear, and professional. Add hook and call-to-action. | |
| """ | |
| with st.spinner("Generating your post... β¨"): | |
| try: | |
| response = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=600 | |
| ) | |
| post = response.choices[0].message.content | |
| st.session_state['post'] = post | |
| st.success("β Your LinkedIn Post is Ready!") | |
| st.markdown("### π’ Generated Post") | |
| st.write(post) | |
| if st.button("Create Another Post"): | |
| st.session_state['step'] = 1 | |
| except Exception as e: | |
| st.error(f"Error: {str(e)}") | |
| if st.button("Back"): | |
| st.session_state['step'] = 3 |