AI twitter and linkedin writers changes.

This commit is contained in:
ajaysi
2025-04-16 18:27:37 +05:30
parent a2b92e27bc
commit a551f97904
9 changed files with 2025 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
# Twitter AI Writer Module
A comprehensive suite of AI-powered tools for Twitter/X content marketing and management.
## Features
### 1. Tweet Generation & Optimization
- **Smart Tweet Generator**
- Multiple tweet variations based on input parameters
- Character count optimization
- Hashtag suggestions and placement
- Emoji usage recommendations
- Thread creation capabilities
- **Tweet Performance Predictor**
- Engagement rate estimation
- Best time to post suggestions
- Audience reach predictions
- Viral potential scoring
### 2. Content Strategy Tools
- **Content Calendar Generator**
- Weekly/monthly content planning
- Theme-based content scheduling
- Event and holiday integration
- Content mix recommendations
- **Hashtag Strategy Manager**
- Trending hashtag research
- Custom hashtag creation
- Hashtag performance tracking
- Competitor hashtag analysis
### 3. Visual Content Creation
- **Image Generator**
- Tweet card creation
- Infographic templates
- Quote card designs
- Brand-consistent visuals
- **Video Content Assistant**
- Video script generation
- Storyboard creation
- Caption optimization
- Thumbnail design suggestions
### 4. Engagement & Community Management
- **Reply Generator**
- Context-aware responses
- Tone matching
- Crisis management templates
- Customer service responses
- **Community Engagement Tools**
- Poll creation
- Q&A session planning
- Community highlight suggestions
- User-generated content prompts
### 5. Analytics & Optimization
- **Performance Analytics**
- Tweet performance tracking
- Engagement metrics analysis
- Audience growth monitoring
- Content effectiveness scoring
- **A/B Testing Assistant**
- Tweet variation testing
- Headline optimization
- CTA effectiveness analysis
- Best performing content identification
### 6. Research & Intelligence
- **Market Research Tools**
- Competitor analysis
- Industry trend tracking
- Audience sentiment analysis
- Content gap identification
- **Content Inspiration**
- Trending topic suggestions
- Content idea generation
- Viral content analysis
- Industry-specific insights
## Best Practices Integration
### Tweet Optimization
- Optimal character count (240-280)
- Strategic hashtag placement
- Effective use of mentions and links
- Engaging call-to-actions
- Visual content optimization
### Content Strategy
- Consistent brand voice
- Regular posting schedule
- Content variety maintenance
- Engagement-driven approach
- Community building focus
### Visual Content
- Image size optimization
- Brand color consistency
- Text overlay best practices
- Mobile-friendly design
- Visual hierarchy principles
### Engagement
- Response time optimization
- Community management guidelines
- Crisis communication protocols
- User interaction best practices
- Content moderation assistance
## Technical Integration
### API Integration
- Twitter API v2 support
- Rate limit management
- Error handling
- Data synchronization
### Performance Optimization
- Caching mechanisms
- Batch processing
- Resource optimization
- Response time improvement
## Security & Compliance
### Data Protection
- User data encryption
- Secure API key management
- Privacy compliance
- Data retention policies
### Content Guidelines
- Platform policy compliance
- Copyright protection
- Brand safety measures
- Content moderation rules
## Coming Soon
- Advanced thread generator
- AI-powered image editor
- Real-time trend analyzer
- Automated content scheduler
- Advanced analytics dashboard
- Multi-account management
- Custom AI model training
- Integration with other social platforms
## Usage Guidelines
1. Ensure API keys are properly configured
2. Follow Twitter's terms of service
3. Maintain brand voice consistency
4. Regular content calendar updates
5. Monitor performance metrics
6. Engage with community regularly
7. Update content strategy based on analytics
8. Follow security best practices
## Support
For technical support or feature requests, please contact the development team or raise an issue in the repository. https://github.com/AJaySi/AI-Writer/issues

View File

@@ -0,0 +1,9 @@
"""
Twitter AI Writer Module
A comprehensive suite of AI-powered tools for Twitter/X content marketing and management.
"""
from .twitter_dashboard import run_dashboard
__all__ = ['run_dashboard']

View File

@@ -0,0 +1,163 @@
Heres an improved and enhanced version of your README. I've structured it for clarity, conciseness, and professionalism, while also making it more engaging and user-friendly.
---
# 🐦 Smart Tweet Generator
**Create tweets that stand out!** The Smart Tweet Generator is a cutting-edge AI-powered tool designed to craft optimized, engaging tweets that maximize your audience reach and engagement.
---
## ✨ Key Features
### 1. **Multi-Variation Tweet Generation**
- Generate 15 tweet variations from a single prompt.
- Each variation tailored to different engagement styles.
- Consistent tone and messaging across all versions.
### 2. **Real-Time Character Optimization**
- Live character count tracking, including emoji support.
- Visual indicators to maintain the ideal tweet length.
- Alerts when nearing Twitter's 280-character limit.
### 3. **Intelligent Hashtag Management**
- Auto-extract hashtags from generated tweets.
- Topic-based, AI-suggested hashtags to enhance discoverability.
- Recommendations for optimal hashtag count and placement.
### 4. **Emoji Suggestions That Fit**
- Context-sensitive and tone-appropriate emoji suggestions.
- Categories include:
- **Humorous**: 😄 😂 😉
- **Informative**: 📊 🔍 💡
- **Inspirational**: ✨ 🌟 🔥
- **Serious**: 🤔 📢 🔔
- **Casual**: 👋 👍 🤗
### 5. **Performance Prediction**
- Engagement score (0-100%) based on AI analysis.
- Metrics analyzed include:
- Character count optimization.
- Hashtag effectiveness.
- Emoji usage.
- Audience relevance.
- Categories:
- **Excellent** (80100%)
- **Good** (6079%)
- **Fair** (4059%)
- **Needs Improvement** (039%)
### 6. **Actionable Improvement Suggestions**
- Real-time feedback on tweet quality.
- Tailored recommendations to boost performance.
- Built-in best practices guidance for effective tweeting.
---
## 🎯 How to Use
### Step 1: **Enter Basic Information**
- Add your tweet topic or hook.
- Define the target audience.
- Choose the desired tone and tweet length.
- Optionally, include a call-to-action (CTA).
### Step 2: **Customize Advanced Options**
- Select the number of tweet variations (15).
- Input keywords or hashtags.
- Choose emoji preferences.
- Add @mentions or placeholders for links.
### Step 3: **Generate and Refine**
- Click **Generate Tweets** to create variations.
- Review performance metrics and apply improvement suggestions.
- Copy, save, or export your favorite version.
---
## 📊 Performance Metrics
**Your tweets are analyzed based on:**
1. **Character Count**
- Optimal: 100200 characters.
- Short: <100 characters.
- Long: >200 characters.
2. **Hashtag Usage**
- Optimal: 13 hashtags.
- Too few: 0 hashtags.
- Too many: >3 hashtags.
3. **Engagement Triggers**
- Questions, CTAs, or interactive elements.
4. **Emoji Optimization**
- Ideal: 13 emojis.
- Too few: 0 emojis.
- Too many: >3 emojis.
5. **Audience Relevance**
- Alignment with keywords, tone, and context.
---
## 💡 Best Practices
1. **Craft Attention-Grabbing Hooks**
- Start with bold statements or thought-provoking questions.
- Use stats or facts to capture attention.
2. **Align Tone with Audience**
- Maintain consistency with your brand voice.
- Adapt tone to audience preferences (e.g., formal, casual).
3. **Strategic Hashtag Usage**
- Use trending and relevant hashtags.
- Limit to 13 for optimal engagement.
4. **Effective Emoji Usage**
- Enhance meaning and context with emojis.
- Match the tone and avoid overuse.
5. **Clear Calls-to-Action**
- Encourage action with clarity and urgency.
- Use action verbs like "Discover," "Join," or "Explore."
---
## 🔄 Export Options
- Copy individual tweets.
- Export all variations as a JSON file.
- Save performance metrics and recommendations.
---
## 🛠️ Technical Details
- **Built with:** Streamlit for an intuitive user interface.
- **AI-powered:** Advanced natural language models for tweet generation.
- **Real-time:** Instant feedback and suggestions.
- **Cross-platform compatibility:** Works seamlessly across devices.
---
## 📝 Notes
- Tweets are optimized for Twitters 280-character limit.
- Performance predictions are derived from AI insights and engagement patterns.
- Suggestions adapt to your audience, ensuring relevancy.
- Regular updates keep the tool current with Twitter trends.
---
## 🤝 Support
Have questions or feature requests? Reach out to our support team or submit an issue on our GitHub repository.
---
*Last updated: Yesterday*
---

View File

@@ -0,0 +1,342 @@
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"""
<div style='padding: 20px; border-radius: 10px; background-color: #f0f2f6; margin-bottom: 20px;'>
<h3 style='margin: 0;'>Tweet Variation {index + 1}</h3>
<p style='margin: 10px 0;'>{tweet['text']}</p>
<div style='display: flex; gap: 10px;'>
<span style='background-color: #e1e4e8; padding: 5px 10px; border-radius: 15px; font-size: 0.8em;'>
Score: {tweet['engagement_score']}%
</span>
</div>
</div>
""", unsafe_allow_html=True)
# Interactive elements
col1, col2 = st.columns(2)
with col1:
if st.button(f"Copy Tweet {index + 1}", key=f"copy_{index}"):
st.write("Tweet copied to clipboard!")
with col2:
if st.button(f"Save Tweet {index + 1}", key=f"save_{index}"):
st.write("Tweet saved!")
def smart_tweet_generator():
"""Enhanced Smart Tweet Generator with improved UI and AI integration."""
st.title("✨ Smart Tweet Generator")
st.markdown("Create engaging tweets with AI-powered optimization")
# Input section with improved UI
with st.expander("Tweet Parameters", expanded=True):
col1, col2 = st.columns(2)
with col1:
hook = st.text_area("Tweet Hook/Topic",
placeholder="Enter your main message or topic...",
help="The main message or topic of your tweet")
target_audience = st.selectbox(
"Target Audience",
["Professionals", "Students", "General"],
help="Select your target audience"
)
tone = st.radio(
"Tweet Tone",
["Professional", "Casual", "Informative", "Humorous", "Inspirational"],
horizontal=True,
help="Choose the tone for your tweet"
)
with col2:
call_to_action = st.text_input(
"Call to Action",
placeholder="e.g., Learn more, Follow us...",
help="What action do you want your audience to take?"
)
keywords = st.text_input(
"Keywords/Hashtags",
placeholder="Enter keywords separated by commas",
help="Keywords to include in your tweet"
)
length = st.select_slider(
"Tweet Length",
options=["short", "medium", "long"],
value="medium",
help="Choose your desired tweet length"
)
num_variations = st.slider(
"Number of Variations",
min_value=1,
max_value=5,
value=3,
help="How many tweet variations would you like to generate?"
)
# Generate button with loading state
if st.button("Generate Tweets", use_container_width=True):
with st.spinner("Generating tweet variations..."):
tweets = generate_tweet_variations(
hook, target_audience, tone,
call_to_action, keywords, length,
num_variations
)
# Display performance metrics
st.markdown("### 📊 Performance Metrics")
for tweet in tweets:
performance = predict_tweet_performance(tweet["text"], target_audience, tone)
# Overall score with progress bar
st.progress(performance["overall_score"] / 100)
st.metric("Overall Score", f"{performance['overall_score']:.1f}%")
# Detailed metrics in columns
cols = st.columns(4)
metrics = performance["metrics"]
with cols[0]:
st.metric("Character Count", f"{metrics['character_count']['score']:.1f}%")
with cols[1]:
st.metric("Hashtag Usage", f"{metrics['hashtag_usage']['score']:.1f}%")
with cols[2]:
st.metric("Engagement", f"{metrics['engagement_potential']['score']:.1f}%")
with cols[3]:
st.metric("Audience Fit", f"{metrics['audience_alignment']['score']:.1f}%")
# Improvement suggestions
suggestions = suggest_improvements(tweet["text"], performance)
if suggestions:
st.markdown("### 💡 Improvement Suggestions")
for suggestion in suggestions:
st.info(suggestion)
# Tweet card
render_tweet_card(tweet, tweets.index(tweet))
st.markdown("---")
# Export options
st.markdown("### 📥 Export Options")
col1, col2 = st.columns(2)
with col1:
if st.button("Export as JSON"):
st.download_button(
"Download JSON",
data=json.dumps(tweets, indent=2),
file_name=f"tweets_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
with col2:
if st.button("Copy All Tweets"):
tweet_texts = "\n\n".join(tweet["text"] for tweet in tweets)
st.code(tweet_texts)
if __name__ == "__main__":
smart_tweet_generator()

View File

@@ -0,0 +1,342 @@
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"""
<div style='padding: 20px; border-radius: 10px; background-color: #f0f2f6; margin-bottom: 20px;'>
<h3 style='margin: 0;'>Tweet Variation {index + 1}</h3>
<p style='margin: 10px 0;'>{tweet['text']}</p>
<div style='display: flex; gap: 10px;'>
<span style='background-color: #e1e4e8; padding: 5px 10px; border-radius: 15px; font-size: 0.8em;'>
Score: {tweet['engagement_score']}%
</span>
</div>
</div>
""", unsafe_allow_html=True)
# Interactive elements
col1, col2 = st.columns(2)
with col1:
if st.button(f"Copy Tweet {index + 1}", key=f"copy_{index}"):
st.write("Tweet copied to clipboard!")
with col2:
if st.button(f"Save Tweet {index + 1}", key=f"save_{index}"):
st.write("Tweet saved!")
def smart_tweet_generator():
"""Enhanced Smart Tweet Generator with improved UI and AI integration."""
st.title("✨ Smart Tweet Generator")
st.markdown("Create engaging tweets with AI-powered optimization")
# Input section with improved UI
with st.expander("Tweet Parameters", expanded=True):
col1, col2 = st.columns(2)
with col1:
hook = st.text_area("Tweet Hook/Topic",
placeholder="Enter your main message or topic...",
help="The main message or topic of your tweet")
target_audience = st.selectbox(
"Target Audience",
["Professionals", "Students", "General"],
help="Select your target audience"
)
tone = st.radio(
"Tweet Tone",
["Professional", "Casual", "Informative", "Humorous", "Inspirational"],
horizontal=True,
help="Choose the tone for your tweet"
)
with col2:
call_to_action = st.text_input(
"Call to Action",
placeholder="e.g., Learn more, Follow us...",
help="What action do you want your audience to take?"
)
keywords = st.text_input(
"Keywords/Hashtags",
placeholder="Enter keywords separated by commas",
help="Keywords to include in your tweet"
)
length = st.select_slider(
"Tweet Length",
options=["short", "medium", "long"],
value="medium",
help="Choose your desired tweet length"
)
num_variations = st.slider(
"Number of Variations",
min_value=1,
max_value=5,
value=3,
help="How many tweet variations would you like to generate?"
)
# Generate button with loading state
if st.button("Generate Tweets", use_container_width=True):
with st.spinner("Generating tweet variations..."):
tweets = generate_tweet_variations(
hook, target_audience, tone,
call_to_action, keywords, length,
num_variations
)
# Display performance metrics
st.markdown("### 📊 Performance Metrics")
for tweet in tweets:
performance = predict_tweet_performance(tweet["text"], target_audience, tone)
# Overall score with progress bar
st.progress(performance["overall_score"] / 100)
st.metric("Overall Score", f"{performance['overall_score']:.1f}%")
# Detailed metrics in columns
cols = st.columns(4)
metrics = performance["metrics"]
with cols[0]:
st.metric("Character Count", f"{metrics['character_count']['score']:.1f}%")
with cols[1]:
st.metric("Hashtag Usage", f"{metrics['hashtag_usage']['score']:.1f}%")
with cols[2]:
st.metric("Engagement", f"{metrics['engagement_potential']['score']:.1f}%")
with cols[3]:
st.metric("Audience Fit", f"{metrics['audience_alignment']['score']:.1f}%")
# Improvement suggestions
suggestions = suggest_improvements(tweet["text"], performance)
if suggestions:
st.markdown("### 💡 Improvement Suggestions")
for suggestion in suggestions:
st.info(suggestion)
# Tweet card
render_tweet_card(tweet, tweets.index(tweet))
st.markdown("---")
# Export options
st.markdown("### 📥 Export Options")
col1, col2 = st.columns(2)
with col1:
if st.button("Export as JSON"):
st.download_button(
"Download JSON",
data=json.dumps(tweets, indent=2),
file_name=f"tweets_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json"
)
with col2:
if st.button("Copy All Tweets"):
tweet_texts = "\n\n".join(tweet["text"] for tweet in tweets)
st.code(tweet_texts)
if __name__ == "__main__":
smart_tweet_generator()