Create interim.py
Browse files- interim.py +131 -0
interim.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from few_shot import FewShotPosts
|
| 3 |
+
from post_generator import generate_post
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Improved app with more functionality
|
| 7 |
+
def main():
|
| 8 |
+
st.set_page_config(page_title="LinkedIn Post Generator", layout="wide")
|
| 9 |
+
st.title("π LinkedIn Post Generator")
|
| 10 |
+
st.markdown(
|
| 11 |
+
"""
|
| 12 |
+
**Create impactful LinkedIn posts effortlessly.**
|
| 13 |
+
Customize your post with advanced options, analyze generated content, and save your creations.
|
| 14 |
+
"""
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Sidebar for advanced options
|
| 18 |
+
st.sidebar.title("π§ Customization Options")
|
| 19 |
+
st.sidebar.markdown("Use these settings to personalize your generated post.")
|
| 20 |
+
|
| 21 |
+
fs = FewShotPosts()
|
| 22 |
+
tags = fs.get_tags()
|
| 23 |
+
|
| 24 |
+
# User input sections
|
| 25 |
+
st.sidebar.subheader("Post Preferences")
|
| 26 |
+
selected_tag = st.sidebar.selectbox("Topic (Tag):", options=tags)
|
| 27 |
+
selected_length = st.sidebar.radio("Post Length:", ["Short", "Medium", "Long"])
|
| 28 |
+
selected_language = st.sidebar.radio("Language:", ["English", "Hinglish"])
|
| 29 |
+
selected_tone = st.sidebar.selectbox(
|
| 30 |
+
"Tone/Style:", ["Motivational", "Professional", "Informal", "Neutral"]
|
| 31 |
+
)
|
| 32 |
+
custom_context = st.sidebar.text_area(
|
| 33 |
+
"Additional Context (Optional):",
|
| 34 |
+
placeholder="Provide extra details or a specific direction for your post...",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# History tracking
|
| 38 |
+
if "generated_posts" not in st.session_state:
|
| 39 |
+
st.session_state.generated_posts = []
|
| 40 |
+
|
| 41 |
+
# Main content
|
| 42 |
+
st.subheader("Generate Your LinkedIn Post")
|
| 43 |
+
|
| 44 |
+
if st.button("Generate Post"):
|
| 45 |
+
with st.spinner("Generating your post..."):
|
| 46 |
+
try:
|
| 47 |
+
# Generate post
|
| 48 |
+
post = generate_post(selected_length, selected_language, selected_tag)
|
| 49 |
+
|
| 50 |
+
# Add tone and context to the prompt dynamically
|
| 51 |
+
if selected_tone:
|
| 52 |
+
post = f"**Tone**: {selected_tone}\n\n{post}"
|
| 53 |
+
if custom_context:
|
| 54 |
+
post += f"\n\n**Context Added**: {custom_context}"
|
| 55 |
+
|
| 56 |
+
# Save to session history
|
| 57 |
+
st.session_state.generated_posts.append(post)
|
| 58 |
+
|
| 59 |
+
st.success("Post generated successfully!")
|
| 60 |
+
st.markdown("### Your LinkedIn Post:")
|
| 61 |
+
st.write(post)
|
| 62 |
+
|
| 63 |
+
# Post analysis
|
| 64 |
+
st.markdown("### Post Analysis:")
|
| 65 |
+
post_analysis(post)
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"An error occurred: {e}")
|
| 69 |
+
|
| 70 |
+
# Display session history
|
| 71 |
+
if st.session_state.generated_posts:
|
| 72 |
+
st.markdown("### Generated Posts History:")
|
| 73 |
+
for i, history_post in enumerate(st.session_state.generated_posts):
|
| 74 |
+
st.markdown(f"**Post {i + 1}:**")
|
| 75 |
+
st.write(history_post)
|
| 76 |
+
|
| 77 |
+
# Save and Download
|
| 78 |
+
st.markdown("---")
|
| 79 |
+
st.subheader("Save or Share Your Post")
|
| 80 |
+
if st.session_state.generated_posts:
|
| 81 |
+
if st.button("Download All Posts"):
|
| 82 |
+
download_posts(st.session_state.generated_posts)
|
| 83 |
+
if st.button("Clear History"):
|
| 84 |
+
st.session_state.generated_posts = []
|
| 85 |
+
st.info("History cleared!")
|
| 86 |
+
|
| 87 |
+
# Footer
|
| 88 |
+
st.markdown(
|
| 89 |
+
"""
|
| 90 |
+
---
|
| 91 |
+
"""
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def post_analysis(post):
|
| 96 |
+
"""Perform basic analysis of the generated post."""
|
| 97 |
+
word_count = len(post.split())
|
| 98 |
+
char_count = len(post)
|
| 99 |
+
tone_keywords = {
|
| 100 |
+
"Motivational": ["inspire", "achieve", "goal", "success"],
|
| 101 |
+
"Professional": ["expertise", "career", "opportunity", "industry"],
|
| 102 |
+
"Informal": ["hey", "cool", "fun", "awesome"],
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
st.write(f"**Word Count:** {word_count}")
|
| 106 |
+
st.write(f"**Character Count:** {char_count}")
|
| 107 |
+
|
| 108 |
+
# Detect tone based on keywords (simple heuristic)
|
| 109 |
+
detected_tone = "Neutral"
|
| 110 |
+
for tone, keywords in tone_keywords.items():
|
| 111 |
+
if any(keyword in post.lower() for keyword in keywords):
|
| 112 |
+
detected_tone = tone
|
| 113 |
+
break
|
| 114 |
+
|
| 115 |
+
st.write(f"**Detected Tone:** {detected_tone}")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def download_posts(posts):
|
| 119 |
+
"""Allow users to download their posts."""
|
| 120 |
+
posts_json = json.dumps(posts, indent=4)
|
| 121 |
+
st.download_button(
|
| 122 |
+
label="π₯ Download Posts",
|
| 123 |
+
data=posts_json,
|
| 124 |
+
file_name="generated_posts.json",
|
| 125 |
+
mime="application/json",
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Run the app
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
main()
|