import streamlit as st import re import json import time from typing import Dict, List, Tuple, Optional import random import emoji from datetime import datetime from ....gpt_providers.text_generation.main_text_generation import llm_text_gen # Constants MAX_TWEET_LENGTH = 280 EMOJI_CATEGORIES = { "Humorous": ["ð", "ð", "ðĪĢ", "ð", "ð", "ð", "ðĪŠ", "ð", "ðĪ", "ð"], "Informative": ["ð", "ð", "ð", "ð", "ðĄ", "ð", "ð", "ð", "ð", "ð"], "Inspirational": ["âĻ", "ð", "ðŦ", "â", "ðĨ", "ðŠ", "ð", "ð", "ðŊ", "ðŊ"], "Serious": ["ðĪ", "ð", "ð§", "ðĒ", "ð", "âïļ", "ð", "ð", "ðŽ", "ð°"], "Casual": ["ð", "ð", "ð", "ð", "ðĪ", "ð", "âïļ", "ðĪ", "ð", "ð"] } def count_characters(text: str) -> int: """Count characters in tweet, accounting for emojis.""" return len(text) def extract_hashtags(text: str) -> List[str]: """Extract hashtags from tweet text.""" return re.findall(r'#\w+', text) def suggest_hashtags(topic: str, tone: str) -> List[str]: """Suggest relevant hashtags based on topic and tone.""" # Enhanced hashtag suggestions based on topic and tone base_hashtags = { "professional": ["#Business", "#Leadership", "#Innovation"], "casual": ["#Life", "#Fun", "#Trending"], "informative": ["#Learn", "#Tips", "#HowTo"], "humorous": ["#Funny", "#LOL", "#Humor"], "inspirational": ["#Motivation", "#Success", "#Growth"] } topic_hashtags = { "tech": ["#Technology", "#TechNews", "#Innovation"], "business": ["#Business", "#Entrepreneurship", "#Startup"], "marketing": ["#Marketing", "#DigitalMarketing", "#SocialMedia"], "education": ["#Education", "#Learning", "#Knowledge"], "health": ["#Health", "#Wellness", "#Fitness"] } # Combine base and topic hashtags suggested = base_hashtags.get(tone.lower(), []) + topic_hashtags.get(topic.lower(), []) return list(set(suggested))[:5] # Return unique hashtags, max 5 def suggest_emojis(tone: str, count: int = 3) -> List[str]: """Suggest emojis based on tone.""" emoji_map = { "professional": ["ðž", "ð", "ðŊ", "ðĄ", "ð"], "casual": ["ð", "ð", "ð", "âĻ", "ð"], "informative": ["ð", "ð", "ðĄ", "ð", "ð"], "humorous": ["ð", "ð", "ðĪĢ", "ð", "ð"], "inspirational": ["âĻ", "ð", "ðŦ", "ðĨ", "ðŠ"] } return emoji_map.get(tone.lower(), ["âĻ"])[:count] def predict_tweet_performance(tweet: str, target_audience: str, tone: str) -> Dict: """Predict tweet performance with enhanced metrics.""" char_count = count_characters(tweet) hashtags = extract_hashtags(tweet) # Enhanced performance metrics metrics = { "character_count": { "score": min(100, (char_count / 280) * 100), "status": "optimal" if 100 <= char_count <= 200 else "suboptimal", "suggestion": "Consider adjusting length for optimal engagement" if char_count < 100 or char_count > 200 else "Length is optimal" }, "hashtag_usage": { "score": min(100, (len(hashtags) / 3) * 100), "status": "optimal" if 1 <= len(hashtags) <= 3 else "suboptimal", "suggestion": "Add more hashtags" if len(hashtags) < 1 else "Reduce hashtag count" if len(hashtags) > 3 else "Hashtag count is optimal" }, "engagement_potential": { "score": 0, "status": "needs_improvement", "suggestion": "" }, "audience_alignment": { "score": 0, "status": "needs_improvement", "suggestion": "" } } # Calculate engagement potential engagement_triggers = ["?", "!", "RT", "like", "follow", "check", "learn", "discover"] trigger_count = sum(1 for trigger in engagement_triggers if trigger.lower() in tweet.lower()) metrics["engagement_potential"]["score"] = min(100, (trigger_count / 3) * 100) metrics["engagement_potential"]["status"] = "optimal" if trigger_count >= 1 else "needs_improvement" metrics["engagement_potential"]["suggestion"] = "Add engagement triggers" if trigger_count < 1 else "Good engagement potential" # Calculate audience alignment audience_keywords = { "professionals": ["business", "industry", "professional", "career"], "students": ["learn", "study", "education", "student"], "general": ["everyone", "people", "community", "world"] } keyword_count = sum(1 for keyword in audience_keywords.get(target_audience.lower(), []) if keyword.lower() in tweet.lower()) metrics["audience_alignment"]["score"] = min(100, (keyword_count / 2) * 100) metrics["audience_alignment"]["status"] = "optimal" if keyword_count >= 1 else "needs_improvement" metrics["audience_alignment"]["suggestion"] = "Add audience-specific keywords" if keyword_count < 1 else "Good audience alignment" # Calculate overall score overall_score = sum(metric["score"] for metric in metrics.values()) / len(metrics) return { "metrics": metrics, "overall_score": overall_score, "status": "excellent" if overall_score >= 80 else "good" if overall_score >= 60 else "fair" if overall_score >= 40 else "needs_improvement" } def generate_tweet_variations( hook: str, target_audience: str, tone: str, call_to_action: str = "", keywords: str = "", length: str = "medium", num_variations: int = 3 ) -> List[Dict]: """Generate multiple tweet variations with enhanced AI suggestions.""" # Enhanced prompt template for better AI suggestions prompt_template = f""" Create {num_variations} engaging tweet variations with the following parameters: - Hook/Topic: {hook} - Target Audience: {target_audience} - Tone: {tone} - Call to Action: {call_to_action} - Keywords: {keywords} - Length: {length} Each tweet should: 1. Start with an attention-grabbing hook 2. Include relevant hashtags 3. Use appropriate emojis 4. End with a clear call-to-action 5. Stay within Twitter's character limit 6. Match the specified tone and audience Format each tweet as a JSON object with: - text: The tweet content - hashtags: List of suggested hashtags - emojis: List of suggested emojis - engagement_score: Predicted engagement score (0-100) """ # Simulate AI-generated tweets (replace with actual AI call) sample_tweets = [ { "text": f"ð {hook} #Innovation #Tech", "hashtags": ["#Innovation", "#Tech"], "emojis": ["ð"], "engagement_score": 85 }, { "text": f"ðĄ {hook} #Business #Growth", "hashtags": ["#Business", "#Growth"], "emojis": ["ðĄ"], "engagement_score": 75 }, { "text": f"âĻ {hook} #Success #Leadership", "hashtags": ["#Success", "#Leadership"], "emojis": ["âĻ"], "engagement_score": 80 } ] return sample_tweets[:num_variations] def suggest_improvements(tweet: str, performance: Dict) -> List[str]: """Generate actionable improvement suggestions.""" suggestions = [] metrics = performance["metrics"] # Character count suggestions if metrics["character_count"]["status"] == "suboptimal": suggestions.append(f"ð {metrics['character_count']['suggestion']}") # Hashtag suggestions if metrics["hashtag_usage"]["status"] == "suboptimal": suggestions.append(f"#ïļâĢ {metrics['hashtag_usage']['suggestion']}") # Engagement suggestions if metrics["engagement_potential"]["status"] == "needs_improvement": suggestions.append(f"ðŊ {metrics['engagement_potential']['suggestion']}") # Audience alignment suggestions if metrics["audience_alignment"]["status"] == "needs_improvement": suggestions.append(f"ðĨ {metrics['audience_alignment']['suggestion']}") return suggestions def render_tweet_card(tweet: Dict, index: int) -> None: """Render an enhanced tweet card with interactive elements.""" with st.container(): st.markdown(f"""
{tweet['text']}