First, let’s quickly define what we’re building. An AI Agent is like a smart assistant that’s programmed to achieve a goal. It works by:
Perceiving: It gathers information from its environment. Our agent will “watch” a YouTube video by reading its transcript.
Thinking: It processes that information to make decisions. Our agent uses the Groq AI service to figure out how to best summarize the transcript for different social media platforms.
Acting: It performs tasks to meet its goal. Our agent’s action is to write and display the final posts for you.
Now, let’s build this agent from the ground up!
Step-by-Step Walkthrough to Using the Social Media Agent
Step 1: Create Project Structure
social-media-agent/
├── app.py # Main code
├── requirements.txt # Dependencies
├── .env # API key storage
Step 2: Install Dependencies and Environment Setup
requirements.txt
# Web Interface
gradio
# YouTube Integration
youtube-transcript-api
# Environment and Configuration
python-dotenv
# Groq API (Free and Fast!)
groq
Explanation:
gradio: Creates the web interface
youtube-transcript-api: Gets video transcripts
python-dotenv: Loads API keys from .env file
groq: AI model for content generation
Install Command
pip install -r requirements.txt
Get an API Key
Go to the Groq Console and sign up for a free account.
Navigate to the API Keys section and create a new secret key. Copy it immediately.
In your project folder, create a file named exactly
.env
.Open the
.env
file and add your key like this, pasting your actual key in place of the placeholder:
.env File
GROQ_API_KEY=your_actual_key_here
Step 3: Start Writing the Code (app.py)
4.1 Import Libraries and Initial Setup
First, let’s import the libraries we need and set up our connection to the Groq API.
# app.py
# --------------------------------------------------------------
# Import necessary libraries
# --------------------------------------------------------------
import os # Access environment variables
import asyncio # (Not used but good for future async features)
import re # Regular expressions for URL parsing
import gradio as gr # Web interface
from dotenv import load_dotenv # Load .env file
from youtube_transcript_api import YouTubeTranscriptApi # Get video text
from dataclasses import dataclass # Clean data structure
from typing import List # Type hints
from groq import Groq # AI model
load_dotenv() # Loads .env file
QROQ_API_KEY = os.getenv("GROQ_API_KEY") # Gets your API key
# Check if the API key is available
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY is not set in the environment variables.")
# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)
4.2 Getting the Video Transcript (Perception)
Next, our agent needs to “perceive” its input. These two functions will get the transcript from a YouTube URL.
# --------------------------------------------------------------
# Define data perception functions
# --------------------------------------------------------------
# Function to extract video ID from YouTube URL
def extract_video_id(url: str) -> str:
"""
Extracts the video ID from a YouTube URL.
Supports both standard and shortened YouTube URLs.
"""
regex = r'(?:https?://)?(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9_-]{11})'
match = re.search(regex, url)
if match:
return match.group(1)
else:
raise ValueError("Invalid YouTube URL")
# Function to get transcript of a YouTube video
def get_transcript(video_id: str, language: list = None) -> str:
if language is None:
language = ["en"]
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=language)
transcript_text = " ".join([entry['text'] for entry in transcript])
return transcript_text
except Exception as e:
return f"Error fetching transcript: {e}"
The code has a function called extract_video_id()
that takes a YouTube URL (e.g.,
and pulls out the unique 11-character video ID (dQw4w9WgXcQ).
It uses a regular expression (regex), which is like a search pattern, to find the ID in the URL.
If it finds the ID, it returns it. If not, it shows an error.
The get_transcript()
function uses the video ID to get the transcript from YouTube.
It asks the YouTubeTranscriptApi for the English transcript.
It combines all the spoken parts into one big string of text.
If something goes wrong (e.g., no transcript exists), it returns an error message.
4.3: The Agent’s Brain (Thinking)
This is the most important function. It takes the transcript and instructions, then “thinks” by asking the AI to generate a social media post.
# --------------------------------------------------------------
# Define the core thinking function
# --------------------------------------------------------------
# Function to generate content for social media platforms using GROQ
def generate_content_gorq(video_transcript: str, social_media_platform: str, custom_instructions: str = ""):
print(f"Generating content for {social_media_platform}...")
platform_prompts = {
"LinkedIn": "Create a professional LinkedIn post with industry insights and relevant hashtags.",
"Twitter": "Create a concise Twitter post under 280 characters with relevant hashtags.",
"Facebook": "Create a Facebook post that encourages engagement and discussion."
}
platform_spcific = platform_prompts.get(social_media_platform, "Create an engaging social media post")
system_prompt = f"You are a skilled social media content generator. {platform_spcific}"
user_prompt = f"""Based on this video transcript, create an engaging {social_media_platform} post:
VIDEO TRANSCRIPT:
{video_transcript[:2000]}...
{f'ADDITIONAL INSTRUCTIONS: {custom_instructions}' if custom_instructions else ''}
Please create content that is:
1. Platform-appropriate in length and style
2. Engaging and shareable
3. Includes relevant hashtags
4. Captures the key message from the video """
try:
response = client.chat.completions.create(
model="meta-llama/llama-4-scout-17b-16e-instruct",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
max_tokens=500,
temperature=0.7,
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"Error generating content: {e}"
The generate_content()
function uses AI to create a post based on the transcript and the chosen platform (e.g., “Twitter”).
It checks if the API key is available.
It sets up a connection to Groq’s AI using the key.
It has instructions (called prompts) for each platform:
LinkedIn: A professional post with insights and hashtags.
Twitter: A short post (under 280 characters) with hashtags.
Facebook: A post that invites discussion.
It sends the first 2000 characters of the transcript and the prompt to the AI, which generates the post.
If there’s an error, it returns a message like “Error generating content.”
4.4: Managing the Workflow (Acting)
These functions organize the whole process, from taking the URL to outputting the final formatted text.
# --------------------------------------------------------------
# Define workflow and user interface functions
# --------------------------------------------------------------
@dataclass
class Post:
platform: str
content: str
# Function to generate multiple posts for different platforms
def generate_multiple_posts(video_transcript: str, platforms: List[str], custom_instructions: str = ""):
posts = []
for platform in platforms:
content = generate_content_gorq(video_transcript, platform, custom_instructions)
posts.append(Post(platform=platform, content=content))
return posts
# Main function to generate social media posts (Complete Pipeline)
def generate_social_media_posts(video_url, platforms, custom_instructions=""):
"""
Generates social media posts based on a YouTube video URL and selected platforms.
"""
try:
video_id = extract_video_id(video_url)
transcript = get_transcript(video_id)
posts = generate_multiple_posts(transcript, platforms, custom_instructions)
# Format output
output_text = f"🎬 **Generated Content for Video ID: {video_id}**\n\n"
for post in posts:
output_text += f"🎯 **{post.platform.upper()}**\n"
output_text += "=" * 50 + "\n"
output_text += f"{post.content}\n\n"
output_text += "-" * 50 + "\n\n"
return output_text
except Exception as e:
return f"Error: {str(e)}"
Step 4: Create and Launch the Web Interface
Finally, let’s add the code to create the web page using Gradio and launch our app.
# --------------------------------------------------------------
# Gradio Interface
# --------------------------------------------------------------
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("# Social Media Content Generator")
video_url = gr.Textbox(label="YouTube Video URL", placeholder="Enter the YouTube video URL here...")
platforms = gr.CheckboxGroup(
label="Select Platforms",
choices=["LinkedIn", "Twitter", "Facebook"],
value=["LinkedIn", "Twitter", "Facebook"]
)
custom_instructions = gr.Textbox(label="Custom Instructions (optional)", placeholder="Enter any custom instructions here...")
generate_button = gr.Button("Generate Posts")
output_text = gr.Textbox(label="Generated Posts", lines=10, interactive=False)
generate_button.click(
fn=generate_social_media_posts,
inputs=[video_url, platforms, custom_instructions],
outputs=output_text
)
return demo
# --------------------------------------------------------------
# Launch the app
# --------------------------------------------------------------
if __name__ == "__main__":
# Create and launch the Gradio app
app = create_interface()
# Launch with custom settings
app.launch(
server_name="0.0.0.0", # Allow external access
server_port=7860, # Default Gradio port
share=False, # Set to True for public sharing
debug=True, # Enable debug mode
show_error=True, # Show detailed errors
inbrowser=True # Auto-open in browser
)
Step 5: Run Your AI Agent! 🚀
Your app.py
file is now complete! It's time to run it.
In your terminal (with the
(venv)
active), run the command:
python app.py