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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Sofia AI Multi-Agent System
|
| 7 |
+
# This space contains specialized agents for content creation and optimization
|
| 8 |
+
|
| 9 |
+
class ContentCreatorAgent:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.name = "Content Creator"
|
| 12 |
+
self.expertise = "Creating engaging social media content"
|
| 13 |
+
|
| 14 |
+
def generate_content(self, topic, platform, tone="engaging"):
|
| 15 |
+
content_templates = {
|
| 16 |
+
"instagram": f"β¨ {topic} β¨\n\nCaption: Let's talk about {topic}! π«\n\nHashtags: #{topic.replace(' ', '')} #SofiaAI #ContentCreation",
|
| 17 |
+
"twitter": f"π Thinking about {topic}...\n\nWhat's your take? π€\n\n#{topic.replace(' ', '')} #SofiaAI",
|
| 18 |
+
"linkedin": f"Professional insight on {topic}\n\nIn today's digital landscape, {topic} is becoming increasingly important.\n\n#ProfessionalDevelopment #{topic.replace(' ', '')}",
|
| 19 |
+
"tiktok": f"π¬ Video idea: {topic}\n\nHook: Did you know about {topic}?\nContent: [Engaging explanation]\nCTA: Follow for more!\n\n#{topic.replace(' ', '')} #Viral"
|
| 20 |
+
}
|
| 21 |
+
return content_templates.get(platform.lower(), f"Content about {topic} for {platform}")
|
| 22 |
+
|
| 23 |
+
class OptimizerAgent:
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.name = "Content Optimizer"
|
| 26 |
+
self.expertise = "Optimizing content for maximum engagement"
|
| 27 |
+
|
| 28 |
+
def optimize(self, content, goals="engagement"):
|
| 29 |
+
suggestions = []
|
| 30 |
+
|
| 31 |
+
# Check length
|
| 32 |
+
if len(content) < 50:
|
| 33 |
+
suggestions.append("β οΈ Content seems short. Consider adding more value.")
|
| 34 |
+
|
| 35 |
+
# Check hashtags
|
| 36 |
+
if "#" not in content:
|
| 37 |
+
suggestions.append("π‘ Add relevant hashtags to increase discoverability")
|
| 38 |
+
|
| 39 |
+
# Check emojis
|
| 40 |
+
emoji_count = sum(1 for char in content if ord(char) > 127462)
|
| 41 |
+
if emoji_count == 0:
|
| 42 |
+
suggestions.append("β¨ Add emojis to make content more engaging")
|
| 43 |
+
|
| 44 |
+
# Check call to action
|
| 45 |
+
cta_keywords = ["follow", "like", "comment", "share", "click"]
|
| 46 |
+
has_cta = any(keyword in content.lower() for keyword in cta_keywords)
|
| 47 |
+
if not has_cta:
|
| 48 |
+
suggestions.append("π― Include a call-to-action (CTA)")
|
| 49 |
+
|
| 50 |
+
optimization_score = 100 - (len(suggestions) * 15)
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"score": max(optimization_score, 0),
|
| 54 |
+
"suggestions": suggestions,
|
| 55 |
+
"optimized": len(suggestions) == 0
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
class TrendAnalyzerAgent:
|
| 59 |
+
def __init__(self):
|
| 60 |
+
self.name = "Trend Analyzer"
|
| 61 |
+
self.expertise = "Analyzing trends and suggesting content ideas"
|
| 62 |
+
|
| 63 |
+
def analyze_trends(self, industry="general"):
|
| 64 |
+
trend_data = {
|
| 65 |
+
"tech": ["AI & Machine Learning", "Web3 & Blockchain", "Cybersecurity", "Cloud Computing", "IoT"],
|
| 66 |
+
"fashion": ["Sustainable Fashion", "Y2K Revival", "Athleisure", "Vintage Style", "Minimalism"],
|
| 67 |
+
"food": ["Plant-Based Diets", "Fermented Foods", "Global Cuisine", "Meal Prep", "Food Sustainability"],
|
| 68 |
+
"general": ["AI Innovation", "Sustainability", "Remote Work", "Mental Health", "Digital Wellness"]
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
trends = trend_data.get(industry.lower(), trend_data["general"])
|
| 72 |
+
|
| 73 |
+
analysis = f"π Current Trends in {industry.capitalize()}:\n\n"
|
| 74 |
+
for i, trend in enumerate(trends, 1):
|
| 75 |
+
analysis += f"{i}. {trend}\n"
|
| 76 |
+
|
| 77 |
+
analysis += f"\nπ Analysis Date: {datetime.now().strftime('%Y-%m-%d')}\n"
|
| 78 |
+
analysis += "\nπ‘ Recommendation: Create content around these trending topics for maximum reach!"
|
| 79 |
+
|
| 80 |
+
return analysis
|
| 81 |
+
|
| 82 |
+
# Initialize agents
|
| 83 |
+
content_creator = ContentCreatorAgent()
|
| 84 |
+
optimizer = OptimizerAgent()
|
| 85 |
+
trend_analyzer = TrendAnalyzerAgent()
|
| 86 |
+
|
| 87 |
+
# Gradio Interface Functions
|
| 88 |
+
def create_content_tab(topic, platform, tone):
|
| 89 |
+
if not topic:
|
| 90 |
+
return "Please enter a topic!"
|
| 91 |
+
content = content_creator.generate_content(topic, platform, tone)
|
| 92 |
+
return content
|
| 93 |
+
|
| 94 |
+
def optimize_content_tab(content, goals):
|
| 95 |
+
if not content:
|
| 96 |
+
return "Please enter content to optimize!"
|
| 97 |
+
result = optimizer.optimize(content, goals)
|
| 98 |
+
|
| 99 |
+
output = f"π Optimization Score: {result['score']}/100\n\n"
|
| 100 |
+
if result['optimized']:
|
| 101 |
+
output += "β
Your content is well optimized!\n"
|
| 102 |
+
else:
|
| 103 |
+
output += "π‘ Suggestions for improvement:\n\n"
|
| 104 |
+
for suggestion in result['suggestions']:
|
| 105 |
+
output += f" {suggestion}\n"
|
| 106 |
+
|
| 107 |
+
return output
|
| 108 |
+
|
| 109 |
+
def analyze_trends_tab(industry):
|
| 110 |
+
return trend_analyzer.analyze_trends(industry)
|
| 111 |
+
|
| 112 |
+
def full_workflow(topic, platform, industry):
|
| 113 |
+
# Step 1: Analyze trends
|
| 114 |
+
trends = trend_analyzer.analyze_trends(industry)
|
| 115 |
+
|
| 116 |
+
# Step 2: Create content
|
| 117 |
+
content = content_creator.generate_content(topic, platform)
|
| 118 |
+
|
| 119 |
+
# Step 3: Optimize content
|
| 120 |
+
optimization = optimizer.optimize(content)
|
| 121 |
+
|
| 122 |
+
workflow_output = f"""π€ SOFIA AI MULTI-AGENT WORKFLOW
|
| 123 |
+
{'='*50}
|
| 124 |
+
|
| 125 |
+
π STEP 1: TREND ANALYSIS
|
| 126 |
+
{trends}
|
| 127 |
+
|
| 128 |
+
{'='*50}
|
| 129 |
+
|
| 130 |
+
βοΈ STEP 2: CONTENT CREATION
|
| 131 |
+
{content}
|
| 132 |
+
|
| 133 |
+
{'='*50}
|
| 134 |
+
|
| 135 |
+
π― STEP 3: CONTENT OPTIMIZATION
|
| 136 |
+
Score: {optimization['score']}/100
|
| 137 |
+
"""
|
| 138 |
+
|
| 139 |
+
if optimization['suggestions']:
|
| 140 |
+
workflow_output += "\nSuggestions:\n"
|
| 141 |
+
for suggestion in optimization['suggestions']:
|
| 142 |
+
workflow_output += f" {suggestion}\n"
|
| 143 |
+
|
| 144 |
+
return workflow_output
|
| 145 |
+
|
| 146 |
+
# Create Gradio Interface
|
| 147 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Sofia AI Agents") as demo:
|
| 148 |
+
gr.Markdown("""
|
| 149 |
+
# π€ Sofia AI - Multi-Agent System
|
| 150 |
+
### Specialized AI Agents for Content Creation & Optimization
|
| 151 |
+
|
| 152 |
+
This space contains 3 specialized agents:
|
| 153 |
+
- π¨βπ¨ **Content Creator**: Generates engaging content for different platforms
|
| 154 |
+
- π― **Optimizer**: Analyzes and optimizes your content
|
| 155 |
+
- π **Trend Analyzer**: Identifies trending topics in your industry
|
| 156 |
+
""")
|
| 157 |
+
|
| 158 |
+
with gr.Tabs():
|
| 159 |
+
# Content Creator Tab
|
| 160 |
+
with gr.Tab("π¨βπ¨ Content Creator"):
|
| 161 |
+
gr.Markdown("### Create engaging content for any platform")
|
| 162 |
+
with gr.Row():
|
| 163 |
+
with gr.Column():
|
| 164 |
+
topic_input = gr.Textbox(label="Topic", placeholder="Enter your content topic...")
|
| 165 |
+
platform_input = gr.Dropdown(
|
| 166 |
+
choices=["Instagram", "Twitter", "LinkedIn", "TikTok"],
|
| 167 |
+
label="Platform",
|
| 168 |
+
value="Instagram"
|
| 169 |
+
)
|
| 170 |
+
tone_input = gr.Dropdown(
|
| 171 |
+
choices=["Engaging", "Professional", "Casual", "Inspirational"],
|
| 172 |
+
label="Tone",
|
| 173 |
+
value="Engaging"
|
| 174 |
+
)
|
| 175 |
+
create_btn = gr.Button("β¨ Generate Content", variant="primary")
|
| 176 |
+
with gr.Column():
|
| 177 |
+
content_output = gr.Textbox(label="Generated Content", lines=10)
|
| 178 |
+
|
| 179 |
+
create_btn.click(create_content_tab, inputs=[topic_input, platform_input, tone_input], outputs=content_output)
|
| 180 |
+
|
| 181 |
+
# Optimizer Tab
|
| 182 |
+
with gr.Tab("π― Content Optimizer"):
|
| 183 |
+
gr.Markdown("### Optimize your content for maximum engagement")
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
content_input = gr.Textbox(label="Your Content", lines=8, placeholder="Paste your content here...")
|
| 187 |
+
goals_input = gr.Dropdown(
|
| 188 |
+
choices=["Engagement", "Reach", "Conversions", "Brand Awareness"],
|
| 189 |
+
label="Optimization Goal",
|
| 190 |
+
value="Engagement"
|
| 191 |
+
)
|
| 192 |
+
optimize_btn = gr.Button("π Optimize", variant="primary")
|
| 193 |
+
with gr.Column():
|
| 194 |
+
optimization_output = gr.Textbox(label="Optimization Results", lines=10)
|
| 195 |
+
|
| 196 |
+
optimize_btn.click(optimize_content_tab, inputs=[content_input, goals_input], outputs=optimization_output)
|
| 197 |
+
|
| 198 |
+
# Trend Analyzer Tab
|
| 199 |
+
with gr.Tab("π Trend Analyzer"):
|
| 200 |
+
gr.Markdown("### Discover trending topics in your industry")
|
| 201 |
+
with gr.Row():
|
| 202 |
+
with gr.Column():
|
| 203 |
+
industry_input = gr.Dropdown(
|
| 204 |
+
choices=["Tech", "Fashion", "Food", "General"],
|
| 205 |
+
label="Industry",
|
| 206 |
+
value="General"
|
| 207 |
+
)
|
| 208 |
+
analyze_btn = gr.Button("π Analyze Trends", variant="primary")
|
| 209 |
+
with gr.Column():
|
| 210 |
+
trends_output = gr.Textbox(label="Trend Analysis", lines=12)
|
| 211 |
+
|
| 212 |
+
analyze_btn.click(analyze_trends_tab, inputs=industry_input, outputs=trends_output)
|
| 213 |
+
|
| 214 |
+
# Full Workflow Tab
|
| 215 |
+
with gr.Tab("π Complete Workflow"):
|
| 216 |
+
gr.Markdown("### Run all agents in sequence")
|
| 217 |
+
with gr.Row():
|
| 218 |
+
with gr.Column():
|
| 219 |
+
wf_topic = gr.Textbox(label="Content Topic", placeholder="Enter topic...")
|
| 220 |
+
wf_platform = gr.Dropdown(
|
| 221 |
+
choices=["Instagram", "Twitter", "LinkedIn", "TikTok"],
|
| 222 |
+
label="Platform",
|
| 223 |
+
value="Instagram"
|
| 224 |
+
)
|
| 225 |
+
wf_industry = gr.Dropdown(
|
| 226 |
+
choices=["Tech", "Fashion", "Food", "General"],
|
| 227 |
+
label="Industry",
|
| 228 |
+
value="General"
|
| 229 |
+
)
|
| 230 |
+
workflow_btn = gr.Button("π Run Complete Workflow", variant="primary")
|
| 231 |
+
with gr.Column():
|
| 232 |
+
workflow_output = gr.Textbox(label="Workflow Results", lines=20)
|
| 233 |
+
|
| 234 |
+
workflow_btn.click(full_workflow, inputs=[wf_topic, wf_platform, wf_industry], outputs=workflow_output)
|
| 235 |
+
|
| 236 |
+
gr.Markdown("""
|
| 237 |
+
---
|
| 238 |
+
### π‘ About Sofia AI Agents
|
| 239 |
+
|
| 240 |
+
This multi-agent system is designed to help you create, optimize, and analyze content efficiently.
|
| 241 |
+
Each agent specializes in a specific task, working together to provide comprehensive content solutions.
|
| 242 |
+
|
| 243 |
+
**Created by:** GoGma | **Version:** 1.0.0
|
| 244 |
+
""")
|
| 245 |
+
|
| 246 |
+
if __name__ == "__main__":
|
| 247 |
+
demo.launch()
|