Spaces:
Build error

poster / src /streamlit_app.py
faiz3123's picture
Update src/streamlit_app.py
f77d75b verified
Raw History Blame Contribute Delete
8.17 kB
import streamlit as st
import google.generativeai as genai
import re
import os
# Set up page
st.set_page_config(
page_title="LinkedIn Post Generator",
page_icon="πŸš€",
layout="centered",
initial_sidebar_state="collapsed"
)
# Modern CSS styling
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
html, body, [class*="css"] {
font-family: 'Poppins', sans-serif;
background-color: #f5f7fb;
}
.header {
text-align: center;
margin-bottom: 2rem;
}
.header h1 {
font-weight: 700;
font-size: 2.5rem;
background: linear-gradient(135deg, #4361ee, #3a0ca3);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
}
.input-card {
background: white;
border-radius: 15px;
padding: 1.5rem;
box-shadow: 0 4px 20px rgba(0,0,0,0.08);
margin-bottom: 1.5rem;
}
.topic-pill {
display: inline-block;
background: linear-gradient(135deg, #f72585, #b5179e);
color: white;
padding: 0.3rem 1rem;
border-radius: 20px;
margin: 0.3rem;
font-size: 0.9rem;
box-shadow: 0 2px 5px rgba(247, 37, 133, 0.3);
}
.remark-pill {
display: inline-block;
background: linear-gradient(135deg, #4cc9f0, #4895ef);
color: white;
padding: 0.3rem 1rem;
border-radius: 20px;
margin: 0.3rem;
font-size: 0.9rem;
box-shadow: 0 2px 5px rgba(76, 201, 240, 0.3);
}
.generate-btn {
background: linear-gradient(135deg, #4361ee, #3a0ca3) !important;
color: white !important;
border: none !important;
padding: 0.7rem 2rem !important;
font-size: 1.1rem !important;
border-radius: 12px !important;
margin: 1rem auto;
display: block;
width: 100%;
transition: all 0.3s ease !important;
}
.generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 15px rgba(67, 97, 238, 0.4);
}
.post-card {
background: linear-gradient(135deg, #ffffff, #f8f9ff);
border-radius: 15px;
padding: 2rem;
box-shadow: 0 4px 25px rgba(0,0,0,0.1);
margin-top: 2rem;
border-left: 4px solid #4361ee;
color: #333333;
}
.copy-btn {
background: #4cc9f0 !important;
color: #000 !important;
margin-top: 1rem;
border: none !important;
}
.post-content {
line-height: 1.8;
color: #333333;
margin: 1.5rem 0;
}
.section-title {
color: #3a0ca3;
border-bottom: 2px solid #4cc9f0;
padding-bottom: 0.5rem;
margin-top: 1.5rem;
}
</style>
""", unsafe_allow_html=True)
# Header section
st.markdown('<div class="header"><h1>LinkedIn Post Generator</h1><p>Create viral professional posts from your learning journey</p></div>', unsafe_allow_html=True)
# Input card
with st.form("input_form"):
st.markdown('<div class="input-card">', unsafe_allow_html=True)
goal = st.text_input("🎯 Your Career Goal", placeholder="e.g., Become a Full-Stack Developer")
day_number = st.text_input("πŸ“† Day Number", placeholder="e.g., Day 42")
topics = st.text_area("πŸ“š Topics Learned Today (Separate with commas)",
placeholder="e.g., Python decorators, API design, Data visualization",
height=100)
# New remarks section
remarks = st.text_area("πŸ’‘ Remarks (Optional)",
placeholder="e.g., Created project notes, Built a mini-application, Solved 5 Leetcode problems",
height=100,
help="Additional learning activities you did today")
st.markdown('</div>', unsafe_allow_html=True)
submitted = st.form_submit_button("✨ Generate Post", use_container_width=True)
# Process inputs
topics_list = [t.strip() for t in topics.split(',') if t.strip()] if topics else []
remarks_list = [r.strip() for r in remarks.split(',') if r.strip()] if remarks else []
# Display topics as pills
if topics_list:
st.markdown("### Today's Topics")
pills_html = "".join([f'<span class="topic-pill">{topic}</span>' for topic in topics_list])
st.markdown(pills_html, unsafe_allow_html=True)
# Display remarks as pills
if remarks_list:
st.markdown("### Additional Activities")
pills_html = "".join([f'<span class="remark-pill">{remark}</span>' for remark in remarks_list])
st.markdown(pills_html, unsafe_allow_html=True)
# Gemini API setup
api_key = os.getenv("GEMINI_API_KEY") or "AIzaSyCWxM6Ic9zs-hZiovnMU8a4UJko7ObqPqU"
# Generate post on submit
if submitted:
if not all([goal, day_number, topics_list]):
st.warning("Please fill all required fields!")
st.stop()
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
# Build prompt with remarks
prompt = f"""
You are a LinkedIn content expert helping create viral professional posts for tech learners.
Create an engaging LinkedIn post with these elements:
1. Catchy hook that grabs attention
2. Professional tone with 1-2 relevant emojis
3. Show learning journey progress
4. Value-packed insights from topics
5. Include any additional learning activities if provided
6. Curiosity-sparking question for engagement
7. Relevant hashtags (5-7)
8. Format: Clear paragraphs, no markdown
Context:
- Career Goal: {goal}
- Learning Day: {day_number}
- Topics: {", ".join(topics_list)}
- Additional Activities: {", ".join(remarks_list) if remarks_list else "None"}
"""
with st.spinner("πŸš€ Crafting your viral post..."):
response = model.generate_content(prompt)
post = response.text
# Store post in session state for copying
st.session_state.post_content = post
# Post-processing
post = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', post) # Bold formatting
post = post.replace('β€’', 'β—‰') # Better bullet points
# Display result
st.markdown(f"""
<div class="post-card">
<h3>Your Viral LinkedIn Post</h3>
<div class="post-content">
{post}
</div>
</div>
""", unsafe_allow_html=True)
# Copy button with working functionality
if st.button("πŸ“‹ Copy to Clipboard", key="copy_button", use_container_width=True,
type="primary", help="Click to copy post to clipboard"):
st.session_state.post_content = st.session_state.post_content.replace("β—‰", "β€’") # Revert bullets for plain text
st.write(f'<script>navigator.clipboard.writeText(`{st.session_state.post_content}`);</script>',
unsafe_allow_html=True)
st.success("Copied to clipboard! Ready to paste on LinkedIn πŸš€")
except Exception as e:
st.error(f"Error generating post: {str(e)}")
# Empty state illustration
elif not submitted:
st.markdown("""
<div style="text-align: center; margin-top: 3rem; opacity: 0.8;">
<svg width="200" height="200" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8 12H8.01M12 12H12.01M16 12H16.01M21 12C21 16.4183 16.9706 20 12 20C10.4607 20 9.01172 19.6565 7.74467 19.0511L3 20L4.39499 16.28C3.51156 14.5423 3 12.5722 3 10.5C3 5.80558 7.02944 2 12 2C16.9706 2 21 5.80558 21 10.5V12Z" stroke="#4361ee" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<h3>Fill in your learning details</h3>
<p>Enter your goal, day number, and topics to generate a viral LinkedIn post</p>
</div>
""", unsafe_allow_html=True)