Spaces:
Build error

poster / app.py
faiz3123's picture
Create app.py
e114eb6 verified
Raw History Blame Contribute Delete
5.99 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;
}
.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);
}
.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: white;
border-radius: 15px;
padding: 2rem;
box-shadow: 0 4px 25px rgba(0,0,0,0.1);
margin-top: 2rem;
border-left: 4px solid #4361ee;
}
.copy-btn {
background: #4cc9f0 !important;
color: #000 !important;
}
</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)
st.markdown('</div>', unsafe_allow_html=True)
submitted = st.form_submit_button("✨ Generate Post", use_container_width=True)
# Process topics
topics_list = [t.strip() for t in topics.split(',') if t.strip()] if topics 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)
# 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 fields!")
st.stop()
try:
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
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. Curiosity-sparking question for engagement
6. Relevant hashtags (5-7)
7. Format: Clear paragraphs, no markdown
Context:
- Career Goal: {goal}
- Learning Day: {day_number}
- Topics: {", ".join(topics_list)}
"""
with st.spinner("πŸš€ Crafting your viral post..."):
response = model.generate_content(prompt)
post = response.text
# 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 style="line-height: 1.8; margin: 1.5rem 0;">
{post}
</div>
</div>
""", unsafe_allow_html=True)
# Copy button
st.code(post, language="text")
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)