DrishtiSharma's picture
Update app.py
d6caebb verified
raw
history blame
5.26 kB
# to-do: add credits + improvize the app a bit more!
import streamlit as st
from few_shot import FewShotPosts
from post_generator import generate_post
import json
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 settings
st.sidebar.title("πŸ”§ Customization Options")
st.sidebar.markdown("Use these settings to personalize your 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...")
# Initialize session state
if "generated_posts" not in st.session_state:
st.session_state.generated_posts = []
# Tabs for different modes
tabs = st.tabs(["Generate Post", "Edit Post", "Analyze Post", "History", "Save/Share"])
# Tab 1: Generate Post
with tabs[0]:
st.subheader("Create a Post")
if st.button("Generate Post"):
with st.spinner("Generating your post..."):
try:
post = generate_post(selected_length, selected_language, selected_tag, selected_tone)
# Add tone and context dynamically
if selected_tone:
post = f"**Tone**: {selected_tone}\n\n{post}"
if custom_context:
post += f"\n\n**Additional Context**: {custom_context}"
# Save to session history
st.session_state.generated_posts.append(post)
st.success("Post generated successfully!")
st.markdown("### Your LinkedIn Post:")
st.markdown(f"<div style='background-color: #f9f9f9; padding: 10px; border-radius: 5px;'>{post}</div>", unsafe_allow_html=True)
except Exception as e:
st.error(f"An error occurred: {e}")
# Tab 2: Edit Post
with tabs[1]:
st.subheader("Edit Your Post")
if st.session_state.generated_posts:
editable_post = st.text_area("Edit your post:", st.session_state.generated_posts[-1])
if st.button("Save Changes"):
st.session_state.generated_posts[-1] = editable_post
st.success("Post updated!")
else:
st.info("Generate a post to edit.")
# Tab 3: Analyze Post
with tabs[2]:
st.subheader("Analyze Your Post")
if st.session_state.generated_posts:
post_to_analyze = st.session_state.generated_posts[-1]
st.markdown("### Post for Analysis:")
st.write(post_to_analyze)
post_analysis(post_to_analyze)
else:
st.info("Generate a post to analyze.")
# Tab 4: Post History
with tabs[3]:
st.subheader("Generated Posts History")
if st.session_state.generated_posts:
for i, post in enumerate(reversed(st.session_state.generated_posts), start=1):
with st.expander(f"Post {i}"):
st.write(post)
else:
st.info("No posts generated yet.")
# Tab 5: Save or Share
with tabs[4]:
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!")
else:
st.info("No posts to save or share.")
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.metric("Word Count", word_count)
st.metric("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.metric("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",
)
if __name__ == "__main__":
main()