🚀 Enhanced LinkedIn Post Generator
Powered by Google Gemini AI
Create professional, engaging LinkedIn content with advanced AI assistance
""" Enhanced LinkedIn Post Generator Powered by Google Gemini AI Features: - Multiple post formats (Standard, Story, List, Question) - Industry-specific templates - Post analytics predictions - Multiple language support - Batch generation - Advanced customization options Requirements: google-generativeai==0.3.2 gradio==4.20.0 requests==2.31.0 """ import gradio as gr import google.generativeai as genai import os import json import re from datetime import datetime from typing import List, Dict, Tuple, Optional # --- Configuration --- MODEL = "gemini-2.5-flash" SUPPORTED_LANGUAGES = { "English": "en", "Spanish": "es", "French": "fr", "German": "de", "Portuguese": "pt", "Italian": "it", "Dutch": "nl", "Japanese": "ja", "Korean": "ko", "Chinese": "zh" } class EnhancedLinkedInGenerator: def __init__(self, api_key=None): self.api_key = api_key self.model = None if api_key: self.configure_api(api_key) def configure_api(self, api_key: str) -> bool: """Configure Gemini API with provided key""" try: genai.configure(api_key=api_key) self.model = genai.GenerativeModel(MODEL) self.api_key = api_key return True except Exception as e: print(f"API Configuration Error: {e}") return False def extract_response_text(self, response) -> str: """Robust response text extraction for various Gemini API response formats""" try: # Method 1: Simple text accessor (most common) if hasattr(response, 'text') and response.text: return response.text # Method 2: Candidates with parts if hasattr(response, 'candidates') and response.candidates: candidate = response.candidates[0] if hasattr(candidate, 'content') and candidate.content: if hasattr(candidate.content, 'parts') and candidate.content.parts: return candidate.content.parts[0].text # Method 3: Direct parts access if hasattr(response, 'parts') and response.parts: return response.parts[0].text return "❌ Error: Unable to extract text from Gemini response." except Exception as e: return f"❌ Error parsing response: {str(e)}" def get_industry_context(self, industry: str) -> str: """Get industry-specific context and terminology""" industry_contexts = { "Technology": "Use tech terminology, mention innovation, digital transformation, and emerging technologies", "Healthcare": "Focus on patient care, medical advances, healthcare accessibility, and wellness", "Finance": "Emphasize financial literacy, market trends, investment strategies, and economic insights", "Education": "Highlight learning methodologies, educational technology, skill development, and knowledge sharing", "Marketing": "Discuss brand strategies, customer engagement, digital marketing trends, and creative campaigns", "Sales": "Focus on relationship building, sales techniques, customer success, and revenue growth", "HR": "Emphasize talent management, workplace culture, employee engagement, and professional development", "Consulting": "Highlight problem-solving, strategic thinking, client success stories, and industry expertise", "Real Estate": "Focus on market trends, property investment, client relationships, and industry insights", "Retail": "Discuss customer experience, retail innovation, market trends, and brand loyalty", "Manufacturing": "Emphasize operational efficiency, quality control, supply chain, and industrial innovation", "Non-Profit": "Focus on social impact, community engagement, fundraising, and mission-driven work" } return industry_contexts.get(industry, "Use professional language appropriate for your industry") def get_post_template(self, post_format: str, tone: str) -> str: """Get format-specific templates for different post types""" templates = { "Standard": f""" Create a {tone} LinkedIn post with this structure: 1. **Hook** (1-2 sentences): Start with an attention-grabbing statement or question 2. **Body** (2-3 paragraphs): Develop the main points with specific examples 3. **Call to Action**: End with engagement-driving question or action request 4. **Hashtags**: Include 3-5 relevant hashtags at the end """, "Story": f""" Create a {tone} LinkedIn story post with this structure: 1. **Opening** (1 sentence): Set the scene with "Recently..." or "Last week..." 2. **Challenge/Situation** (1-2 sentences): Describe the problem or situation 3. **Action/Solution** (2-3 sentences): What was done to address it 4. **Outcome/Lesson** (1-2 sentences): Results and key takeaway 5. **Question**: Ask readers about their similar experiences 6. **Hashtags**: Include 3-5 relevant hashtags """, "List": f""" Create a {tone} LinkedIn list post with this structure: 1. **Introduction** (1-2 sentences): Introduce the list topic 2. **List Items** (5-7 items): Each with brief explanation • Use bullet points or numbers • Keep each point concise but valuable 3. **Conclusion** (1 sentence): Summarize the value 4. **Engagement**: Ask which point resonates most 5. **Hashtags**: Include 3-5 relevant hashtags """, "Question": f""" Create a {tone} LinkedIn question post with this structure: 1. **Context** (2-3 sentences): Provide background for the question 2. **Main Question** (1 sentence): Clear, thought-provoking question 3. **Sub-questions** (2-3 follow-up questions): Guide the discussion 4. **Your Take** (1-2 sentences): Share your initial thoughts 5. **Call to Participate**: Encourage comments and discussion 6. **Hashtags**: Include 3-5 relevant hashtags """, "Achievement": f""" Create a {tone} LinkedIn achievement post with this structure: 1. **Announcement** (1 sentence): Share the achievement 2. **Journey** (2-3 sentences): Brief story of how you got there 3. **Gratitude** (1-2 sentences): Thank people who helped 4. **Learning** (1-2 sentences): What you learned along the way 5. **Forward Look**: What's next or how others can achieve similar success 6. **Hashtags**: Include 3-5 relevant hashtags """ } return templates.get(post_format, templates["Standard"]) def generate_post(self, topic: str, audience: str, key_points: str, tone: str, post_format: str, industry: str, language: str, api_key: str, include_emoji: bool = True, post_length: str = "Medium") -> str: """Generate enhanced LinkedIn post with advanced options""" # Validate inputs if not all([topic.strip(), audience.strip(), key_points.strip()]): return "❌ Error: Please fill in all required fields (Topic, Audience, Key Points)." if not api_key.strip(): return "❌ Error: Please provide your Gemini API key." # Configure API if not self.configure_api(api_key): return "❌ Error: Invalid API key. Please check your Gemini API key and try again." # Format key points formatted_key_points = "\n".join([f"- {line.strip()}" for line in key_points.split("\n") if line.strip()]) # Get industry context and post template industry_context = self.get_industry_context(industry) post_template = self.get_post_template(post_format, tone) # Determine post length guidance length_guidance = { "Short": "Keep the post concise (100-150 words). Perfect for quick insights.", "Medium": "Create a medium-length post (150-250 words). Balanced detail and readability.", "Long": "Write a comprehensive post (250-400 words). Detailed and informative." } # Build comprehensive prompt prompt = f""" You are an expert LinkedIn content strategist and copywriter with deep understanding of professional social media engagement. **Your Task:** Create a high-quality LinkedIn post based on the specifications below. **Post Specifications:** - **Topic:** {topic} - **Target Audience:** {audience} - **Post Format:** {post_format} - **Tone:** {tone} - **Industry:** {industry} - **Language:** {language} - **Length:** {length_guidance[post_length]} - **Include Emojis:** {include_emoji} **Industry Context:** {industry_context} **Key Points to Include:** {formatted_key_points} **Post Structure Guidelines:** {post_template} **Additional Requirements:** 1. **Professional Quality:** Ensure content is polished and error-free 2. **Engagement Optimization:** Use techniques that encourage likes, comments, and shares 3. **Value-First:** Every sentence should provide value to the reader 4. **Authenticity:** Make it sound natural and genuine, not overly promotional 5. **Visual Appeal:** {"Use relevant emojis strategically to enhance readability" if include_emoji else "Do not use emojis"} 6. **Language:** Write entirely in {language} 7. **Hashtag Strategy:** Choose hashtags that are popular but not oversaturated **Engagement Best Practices:** - Start with a hook that makes people want to read more - Use short paragraphs for better mobile readability - Include specific examples or data when possible - End with a question or call-to-action that encourages responses - Make it scannable with bullet points or line breaks Generate the complete LinkedIn post now: """ try: # Generate the post response = self.model.generate_content( prompt, generation_config=genai.types.GenerationConfig( temperature=0.7, top_p=0.9, max_output_tokens=1500, ) ) # Extract response text post_content = self.extract_response_text(response) if post_content.startswith("❌"): return post_content # Add metadata metadata = f""" **Post Analytics Prediction:** - **Estimated Reach:** {self.predict_reach(topic, audience, tone)} - **Best Posting Time:** {self.suggest_posting_time(audience)} - **Engagement Potential:** {self.predict_engagement(post_format, tone)} --- *Generated on {datetime.now().strftime("%Y-%m-%d at %H:%M")} using Enhanced LinkedIn Post Generator* """ return f"{post_content}\n\n{metadata}" except Exception as e: return f"❌ Error generating post: {str(e)}" def predict_reach(self, topic: str, audience: str, tone: str) -> str: """Predict potential reach based on topic and audience""" # Simplified prediction logic if "AI" in topic or "technology" in topic.lower(): return "High (5,000-15,000 impressions)" elif "business" in topic.lower() or "leadership" in topic.lower(): return "Medium-High (3,000-10,000 impressions)" else: return "Medium (1,000-5,000 impressions)" def suggest_posting_time(self, audience: str) -> str: """Suggest optimal posting times based on audience""" if "executive" in audience.lower() or "ceo" in audience.lower(): return "Tuesday-Thursday, 8-9 AM or 12-1 PM" elif "developer" in audience.lower() or "engineer" in audience.lower(): return "Tuesday-Wednesday, 9-10 AM or 2-3 PM" else: return "Tuesday-Thursday, 9 AM-12 PM" def predict_engagement(self, post_format: str, tone: str) -> str: """Predict engagement potential""" engagement_scores = { "Question": "High", "Story": "High", "List": "Medium-High", "Standard": "Medium", "Achievement": "Medium" } return f"{engagement_scores.get(post_format, 'Medium')} engagement expected" def generate_multiple_posts(self, topic: str, audience: str, key_points: str, api_key: str, count: int = 3) -> str: """Generate multiple post variations""" formats = ["Standard", "Story", "Question"] tones = ["Professional", "Inspirational", "Conversational"] results = [] for i in range(min(count, 3)): post = self.generate_post( topic=topic, audience=audience, key_points=key_points, tone=tones[i], post_format=formats[i], industry="Technology", language="English", api_key=api_key, include_emoji=True, post_length="Medium" ) results.append(f"**Variation {i+1} ({formats[i]} - {tones[i]}):**\n{post}\n\n{'='*50}\n") return "\n".join(results) # Initialize generator generator = EnhancedLinkedInGenerator() def save_post_to_file(post_content: str, topic: str) -> str: """Save generated post to downloadable file""" if not post_content or post_content.startswith("❌"): return None timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"linkedin_post_{topic.replace(' ', '_')}_{timestamp}.txt" with open(filename, 'w', encoding='utf-8') as f: f.write(post_content) return filename # Create the enhanced Gradio interface with gr.Blocks( theme=gr.themes.Soft(), title="Enhanced LinkedIn Post Generator - Powered by Google Gemini AI", css=""" .main-header { text-align: center; margin-bottom: 2rem; } .feature-box { background: linear-gradient(45deg, #667eea, #764ba2); padding: 1rem; border-radius: 8px; color: white; margin: 1rem 0; } .pro-tip { background: #f0f9ff; padding: 1rem; border-left: 4px solid #3b82f6; margin: 1rem 0; } """ ) as demo: # Header gr.Markdown("""
Create professional, engaging LinkedIn content with advanced AI assistance