import streamlit as st import time import os from utils.run_agent import execute_agent from config.configs import AZURE_OPENAI_API_KEY, AZURE_OPENAI_ENDPOINT, AZURE_DEPLOYMENT from config.generation_config import get_generation_prompt, get_hashtag_suggestions, DEFAULT_SETTINGS from utils.credential_manager import CredentialManager, format_endpoint_url, mask_api_key st.set_page_config( page_title="LinkedIn Post Generator", page_icon="๐Ÿ“", layout="wide", initial_sidebar_state="expanded" ) st.markdown(""" """, unsafe_allow_html=True) # Initialize session state for credentials if 'azure_api_key' not in st.session_state: st.session_state.azure_api_key = "" if 'azure_endpoint' not in st.session_state: st.session_state.azure_endpoint = "" if 'azure_deployment' not in st.session_state: st.session_state.azure_deployment = "" if 'azure_api_version' not in st.session_state: st.session_state.azure_api_version = "2024-02-01" with st.sidebar: st.title("โš™๏ธ Configuration") st.subheader("๐Ÿ”‘ Azure OpenAI Credentials") st.info("๐Ÿ’ก Enter your Azure OpenAI credentials below:") api_key = st.text_input( "Azure OpenAI API Key", value=st.session_state.azure_api_key, type="password", help="Your Azure OpenAI API key" ) endpoint = st.text_input( "Azure OpenAI Endpoint", value=st.session_state.azure_endpoint, placeholder="https://your-resource.openai.azure.com/", help="Your Azure OpenAI endpoint URL" ) deployment = st.text_input( "Deployment Name", value=st.session_state.azure_deployment, placeholder="gpt-35-turbo", help="Name of your deployed model" ) api_version = st.text_input( "API Version", value=st.session_state.azure_api_version, placeholder="2024-02-01", help="Azure OpenAI API version" ) if st.button("๐Ÿ’พ Save Credentials", use_container_width=True): credentials = { 'api_key': api_key, 'endpoint': format_endpoint_url(endpoint) if endpoint else '', 'deployment': deployment, 'api_version': api_version } is_valid, message = CredentialManager.validate_credentials(credentials) if is_valid: CredentialManager.save_credentials_to_session( credentials['api_key'], credentials['endpoint'], credentials['deployment'], credentials['api_version'] ) st.success("โœ… Credentials saved successfully!") st.rerun() else: st.error(f"โŒ {message}") if st.button("๐Ÿ—‘๏ธ Clear Credentials", use_container_width=True): CredentialManager.clear_session_credentials() st.success("โœ… Credentials cleared!") st.rerun() st.markdown('', unsafe_allow_html=True) st.subheader("๐Ÿ” API Status") current_credentials = CredentialManager.get_credentials_from_session() if current_credentials['api_key']: masked_key = mask_api_key(current_credentials['api_key']) st.success(f"โœ… API Key: {masked_key}") else: st.error("โŒ API Key not found") if current_credentials['endpoint']: st.success(f"โœ… Endpoint: {current_credentials['endpoint']}") else: st.warning("โš ๏ธ Endpoint not configured") if current_credentials['deployment']: st.success(f"โœ… Deployment: {current_credentials['deployment']}") else: st.warning("โš ๏ธ Deployment not configured") if all(current_credentials.values()): if st.button("๐Ÿงช Test Connection", use_container_width=True): with st.spinner("Testing Azure OpenAI connection..."): try: CredentialManager.set_environment_variables(current_credentials) test_output = execute_agent("Test connection") st.success("โœ… Connection successful! Azure OpenAI is working.") except Exception as e: st.error(f"โŒ Connection failed: {str(e)}") st.subheader("๐ŸŽฏ Generation Settings") post_length = st.selectbox( "Post Length", ["Short (100-200 words)", "Medium (200-400 words)", "Long (400-600 words)"], index=1 ) tone = st.selectbox( "Tone", ["Professional", "Casual", "Enthusiastic", "Educational", "Inspirational"], index=0 ) include_hashtags = st.checkbox("Include Hashtags", value=True) include_call_to_action = st.checkbox("Include Call to Action", value=True) st.subheader("โ„น๏ธ About") st.write("**LinkedIn Post Generator**") st.write("Powered by LangGraph and Azure OpenAI") st.write("Version: 1.0.0") st.title("๐Ÿ“ LinkedIn Post Generator") st.markdown("Transform your ideas into engaging LinkedIn posts using AI") col1, col2 = st.columns([2, 1]) with col1: st.subheader("๐ŸŽฏ What would you like to write about?") user_input = st.text_area( "Enter your topic, idea, or subject matter:", value=st.session_state.get("user_input", ""), placeholder="e.g., 'The future of artificial intelligence in healthcare' or 'Tips for remote team collaboration'", height=100, help="Be specific about your topic to get better results" ) if user_input and include_hashtags: suggested_hashtags = get_hashtag_suggestions(user_input) st.info(f"๐Ÿ’ก **Suggested hashtags for '{user_input}':**") hashtag_cols = st.columns(3) for i, hashtag in enumerate(suggested_hashtags[:3]): with hashtag_cols[i]: st.write(f"โ€ข {hashtag}") with st.expander("๐Ÿ” Additional Context (Optional)"): context = st.text_area( "Add any specific details, target audience, or context:", placeholder="e.g., 'Target audience: Tech professionals, Focus on practical applications'", height=80 ) col1_btn, col2_btn = st.columns([3, 1]) with col1_btn: generate_clicked = st.button("๐Ÿš€ Generate LinkedIn Post", type="primary", use_container_width=True) with col2_btn: clear_clicked = st.button("๐Ÿ—‘๏ธ Clear", use_container_width=True) if clear_clicked: st.session_state.user_input = "" st.session_state.example_topic = "" st.rerun() if generate_clicked: if user_input: current_credentials = CredentialManager.get_credentials_from_session() if not all(current_credentials.values()): st.error("โŒ Please configure your Azure OpenAI credentials in the sidebar first.") else: CredentialManager.set_environment_variables(current_credentials) with st.spinner("๐Ÿค– AI is crafting your perfect LinkedIn post..."): try: settings = { "post_length": post_length, "tone": tone, "include_hashtags": include_hashtags, "include_call_to_action": include_call_to_action } # Generate formatted prompt using the configuration full_query = get_generation_prompt(user_input, context, settings) # Execute the agent output = execute_agent(full_query) # Update session state if 'generated_posts' not in st.session_state: st.session_state.generated_posts = 0 st.session_state.generated_posts += 1 st.success("โœ… Your LinkedIn post has been generated successfully!") st.subheader("๐Ÿ“„ Generated LinkedIn Post") st.markdown("---") st.write(output) st.markdown("---") if st.button("๐Ÿ“‹ Copy to Clipboard", use_container_width=True): st.write("๐Ÿ“‹ Post copied to clipboard!") except Exception as e: st.error(f"โŒ An error occurred: {str(e)}") else: st.warning("โš ๏ธ Please enter a topic to generate a post.") with col2: st.subheader("๐Ÿ’ก Tips for Better Posts") tips = [ "๐ŸŽฏ Be specific about your topic", "๐Ÿ‘ฅ Consider your target audience", "๐Ÿ“Š Include relevant statistics if applicable", "๐Ÿ’ฌ Ask engaging questions", "๐Ÿ”— Reference current trends or news", "๐Ÿ“ Keep paragraphs short and readable", "๐ŸŽจ Use emojis sparingly but effectively" ] for tip in tips: st.write(tip) st.markdown("---") st.subheader("๐Ÿ“ˆ Post Analytics") if 'generated_posts' not in st.session_state: st.session_state.generated_posts = 0 st.metric("Posts Generated", st.session_state.generated_posts) st.subheader("๐ŸŽฏ Example Topics") example_topics = [ "AI in healthcare", "Remote work productivity", "Digital transformation", "Leadership skills", "Innovation in business" ] for topic in example_topics: if st.button(topic, key=f"example_{topic}"): st.session_state.example_topic = topic st.session_state.user_input = topic st.rerun() st.markdown("---") st.markdown( """

Made with โค๏ธ using Streamlit, LangGraph, and Azure OpenAI

For questions or support, please check the documentation

""", unsafe_allow_html=True )