Update src/streamlit_app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940import altair as altimport numpy as npimport pandas as pdimport streamlit as st"""# Welcome to Streamlit!Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).In the meantime, below is an example of what you can do with just a few lines of code:"""num_points = st.slider("Number of points in spiral", 1, 10000, 1100)num_turns = st.slider("Number of turns in spiral", 1, 300, 31)indices = np.linspace(0, 1, num_points)theta = 2 * np.pi * num_turns * indicesradius = indicesx = radius * np.cos(theta)y = radius * np.sin(theta)df = pd.DataFrame({"x": x,"y": y,"idx": indices,"rand": np.random.randn(num_points),})st.altair_chart(alt.Chart(df, height=700, width=700).mark_point(filled=True).encode(x=alt.X("x", axis=None),y=alt.Y("y", axis=None),color=alt.Color("idx", legend=None, scale=alt.Scale()),size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181import streamlit as stimport google.generativeai as genaiimport reimport os# Set up pagest.set_page_config(page_title="LinkedIn Post Generator",page_icon="π",layout="centered",initial_sidebar_state="collapsed")# Modern CSS stylingst.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 sectionst.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 cardwith 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 topicstopics_list = [t.strip() for t in topics.split(',') if t.strip()] if topics else []# Display topics as pillsif 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 setupapi_key = os.getenv("GEMINI_API_KEY") or "AIzaSyCWxM6Ic9zs-hZiovnMU8a4UJko7ObqPqU"# Generate post on submitif 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 attention2. Professional tone with 1-2 relevant emojis3. Show learning journey progress4. Value-packed insights from topics5. Curiosity-sparking question for engagement6. Relevant hashtags (5-7)7. Format: Clear paragraphs, no markdownContext:- 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-processingpost = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', post) # Bold formattingpost = post.replace('β’', 'β') # Better bullet points# Display resultst.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 buttonst.code(post, language="text")except Exception as e:st.error(f"Error generating post: {str(e)}")# Empty state illustrationelif 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)
src/streamlit_app.py+179-38