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