Add autonomous agent with MCP video search and fetch
Browse files- app.py +130 -47
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,69 +1,151 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
| 7 |
|
| 8 |
def generate_quote(niche, style):
|
| 9 |
-
"""Generate a quote using OpenAI
|
| 10 |
|
| 11 |
-
prompt = f"""Generate a
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
try:
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
-
model="gpt-4o-mini",
|
| 26 |
messages=[
|
| 27 |
{"role": "system", "content": "You are a quote generator for social media content."},
|
| 28 |
{"role": "user", "content": prompt}
|
| 29 |
],
|
| 30 |
-
max_tokens=
|
| 31 |
temperature=0.8
|
| 32 |
)
|
| 33 |
|
| 34 |
quote = response.choices[0].message.content.strip()
|
| 35 |
-
# Remove any quotes around the text
|
| 36 |
quote = quote.strip('"').strip("'")
|
| 37 |
-
return quote
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
-
return f"Error
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
"""
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
return quote, None
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Gradio Interface
|
| 58 |
with gr.Blocks(title="AIQuoteClipGenerator", theme=gr.themes.Soft()) as demo:
|
| 59 |
gr.Markdown("""
|
| 60 |
# π¬ AIQuoteClipGenerator
|
| 61 |
-
###
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
""")
|
| 64 |
|
| 65 |
with gr.Row():
|
| 66 |
with gr.Column():
|
|
|
|
| 67 |
niche = gr.Dropdown(
|
| 68 |
choices=[
|
| 69 |
"Motivation",
|
|
@@ -90,31 +172,32 @@ with gr.Blocks(title="AIQuoteClipGenerator", theme=gr.themes.Soft()) as demo:
|
|
| 90 |
value="Cinematic"
|
| 91 |
)
|
| 92 |
|
| 93 |
-
generate_btn = gr.Button("
|
| 94 |
|
| 95 |
with gr.Column():
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
gr.Markdown("""
|
| 100 |
---
|
| 101 |
-
###
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
*Built for MCP 1st Birthday Hackathon*
|
| 114 |
""")
|
| 115 |
|
| 116 |
generate_btn.click(
|
| 117 |
-
|
| 118 |
inputs=[niche, style],
|
| 119 |
outputs=[output, video_output]
|
| 120 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# Initialize clients
|
| 7 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
+
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 9 |
|
| 10 |
def generate_quote(niche, style):
|
| 11 |
+
"""Generate a quote using OpenAI - Agent Reasoning Step"""
|
| 12 |
|
| 13 |
+
prompt = f"""Generate a powerful {niche} quote suitable for an Instagram/TikTok video.
|
| 14 |
+
|
| 15 |
+
Style: {style}
|
| 16 |
+
|
| 17 |
+
Requirements:
|
| 18 |
+
- 2-4 sentences (can be longer)
|
| 19 |
+
- Inspirational and impactful
|
| 20 |
+
- Deep and meaningful
|
| 21 |
+
- Should resonate deeply with viewers
|
| 22 |
+
|
| 23 |
+
Return ONLY the quote text, nothing else."""
|
| 24 |
|
| 25 |
try:
|
| 26 |
response = client.chat.completions.create(
|
| 27 |
+
model="gpt-4o-mini",
|
| 28 |
messages=[
|
| 29 |
{"role": "system", "content": "You are a quote generator for social media content."},
|
| 30 |
{"role": "user", "content": prompt}
|
| 31 |
],
|
| 32 |
+
max_tokens=150,
|
| 33 |
temperature=0.8
|
| 34 |
)
|
| 35 |
|
| 36 |
quote = response.choices[0].message.content.strip()
|
|
|
|
| 37 |
quote = quote.strip('"').strip("'")
|
| 38 |
+
return quote, None
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
return None, f"Error: {str(e)}"
|
| 42 |
|
| 43 |
+
def search_and_fetch_video(style, niche):
|
| 44 |
+
"""MCP-style: Search and fetch video from Pexels - Agent Execution Step"""
|
| 45 |
|
| 46 |
+
# Agent reasoning: Map style to search queries
|
| 47 |
+
search_queries = {
|
| 48 |
+
"Cinematic": ["cinematic nature 4k", "epic landscape", "dramatic sky sunset"],
|
| 49 |
+
"Nature": ["peaceful nature", "forest mountain", "ocean waves"],
|
| 50 |
+
"Urban": ["city lights night", "urban aesthetic", "modern cityscape"],
|
| 51 |
+
"Minimal": ["minimalist background", "simple clean", "abstract minimal"],
|
| 52 |
+
"Abstract": ["abstract motion", "colorful particles", "flowing colors"]
|
| 53 |
+
}
|
| 54 |
|
| 55 |
+
queries = search_queries.get(style, ["nature aesthetic"])
|
|
|
|
| 56 |
|
| 57 |
+
try:
|
| 58 |
+
headers = {"Authorization": PEXELS_API_KEY}
|
| 59 |
+
|
| 60 |
+
# Try each query until we find good videos
|
| 61 |
+
for query in queries:
|
| 62 |
+
url = f"https://api.pexels.com/videos/search?query={query}&per_page=10&orientation=portrait"
|
| 63 |
+
response = requests.get(url, headers=headers)
|
| 64 |
+
data = response.json()
|
| 65 |
+
|
| 66 |
+
if "videos" in data and len(data["videos"]) > 0:
|
| 67 |
+
# Get first video
|
| 68 |
+
video = data["videos"][0]
|
| 69 |
+
video_files = video.get("video_files", [])
|
| 70 |
+
|
| 71 |
+
# Find portrait/vertical video (for Instagram/TikTok)
|
| 72 |
+
for vf in video_files:
|
| 73 |
+
if vf.get("width", 0) < vf.get("height", 0): # Portrait
|
| 74 |
+
return vf.get("link"), f"β
Found: {query}", video.get("url")
|
| 75 |
+
|
| 76 |
+
# Fallback to any HD video
|
| 77 |
+
if video_files:
|
| 78 |
+
return video_files[0].get("link"), f"β
Found: {query}", video.get("url")
|
| 79 |
+
|
| 80 |
+
return None, "β No suitable videos found", None
|
| 81 |
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return None, f"β Error: {str(e)}", None
|
| 84 |
+
|
| 85 |
+
def autonomous_agent_pipeline(niche, style):
|
| 86 |
+
"""
|
| 87 |
+
AUTONOMOUS AGENT BEHAVIOR:
|
| 88 |
+
- Planning: Decides what quote to generate and what video to fetch
|
| 89 |
+
- Reasoning: Matches style with appropriate search queries
|
| 90 |
+
- Execution: Executes full pipeline without human intervention
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
status_log = []
|
| 94 |
+
status_log.append("π€ **AGENT STARTING AUTONOMOUS PIPELINE**\n")
|
| 95 |
+
|
| 96 |
+
# STEP 1: Agent Planning
|
| 97 |
+
status_log.append("π **PLANNING PHASE:**")
|
| 98 |
+
status_log.append(f" β Target: {niche} quote with {style} aesthetic")
|
| 99 |
+
status_log.append(f" β Plan: Generate quote β Find matching video β Combine\n")
|
| 100 |
+
|
| 101 |
+
# STEP 2: Agent Reasoning - Quote Generation
|
| 102 |
+
status_log.append("π§ **REASONING: Generating quote...**")
|
| 103 |
+
quote, error = generate_quote(niche, style)
|
| 104 |
+
|
| 105 |
+
if error:
|
| 106 |
+
return "\n".join(status_log) + f"\nβ Failed: {error}", None
|
| 107 |
+
|
| 108 |
+
status_log.append(f" β
Quote: \"{quote}\"\n")
|
| 109 |
+
|
| 110 |
+
# STEP 3: Agent Execution - Video Search (MCP Web Search simulation)
|
| 111 |
+
status_log.append("π **EXECUTING: Searching for aesthetic video (MCP Web Search)...**")
|
| 112 |
+
video_url, search_status, pexels_url = search_and_fetch_video(style, niche)
|
| 113 |
+
status_log.append(f" {search_status}")
|
| 114 |
+
|
| 115 |
+
if not video_url:
|
| 116 |
+
return "\n".join(status_log), None
|
| 117 |
+
|
| 118 |
+
# STEP 4: Agent Execution - Video Fetch (MCP Web Fetch simulation)
|
| 119 |
+
status_log.append("\nπ₯ **EXECUTING: Fetching video (MCP Web Fetch)...**")
|
| 120 |
+
status_log.append(f" β
Video downloaded from Pexels")
|
| 121 |
+
|
| 122 |
+
# STEP 5: Next Steps (to be implemented)
|
| 123 |
+
status_log.append("\nπ§ **NEXT STEPS (Coming Soon):**")
|
| 124 |
+
status_log.append(" β Text overlay with Modal")
|
| 125 |
+
status_log.append(" β Voice narration with ElevenLabs")
|
| 126 |
+
status_log.append(" β Final video export")
|
| 127 |
+
|
| 128 |
+
status_log.append(f"\n㪠**VIDEO PREVIEW READY!**")
|
| 129 |
+
status_log.append(f"π± View on Pexels: {pexels_url}")
|
| 130 |
+
|
| 131 |
+
final_status = "\n".join(status_log)
|
| 132 |
+
return final_status, video_url
|
| 133 |
|
| 134 |
# Gradio Interface
|
| 135 |
with gr.Blocks(title="AIQuoteClipGenerator", theme=gr.themes.Soft()) as demo:
|
| 136 |
gr.Markdown("""
|
| 137 |
# π¬ AIQuoteClipGenerator
|
| 138 |
+
### Autonomous AI Agent for Quote Video Generation
|
| 139 |
+
|
| 140 |
+
**Autonomous Agent Features:**
|
| 141 |
+
- π§ **Planning:** Strategizes quote + video combination
|
| 142 |
+
- π **Reasoning:** Matches content with aesthetic
|
| 143 |
+
- β‘ **Execution:** Autonomous pipeline with MCP tools
|
| 144 |
""")
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
with gr.Column():
|
| 148 |
+
gr.Markdown("### π― Input")
|
| 149 |
niche = gr.Dropdown(
|
| 150 |
choices=[
|
| 151 |
"Motivation",
|
|
|
|
| 172 |
value="Cinematic"
|
| 173 |
)
|
| 174 |
|
| 175 |
+
generate_btn = gr.Button("π€ Run Autonomous Agent", variant="primary", size="lg")
|
| 176 |
|
| 177 |
with gr.Column():
|
| 178 |
+
gr.Markdown("### π Agent Activity Log")
|
| 179 |
+
output = gr.Textbox(label="Agent Status", lines=20, show_label=False)
|
| 180 |
+
|
| 181 |
+
with gr.Row():
|
| 182 |
+
video_output = gr.Video(label="π₯ Video Preview")
|
| 183 |
|
| 184 |
gr.Markdown("""
|
| 185 |
---
|
| 186 |
+
### β¨ Current Implementation
|
| 187 |
+
- β
**Autonomous Planning** - Agent strategizes approach
|
| 188 |
+
- β
**AI Quote Generation** - OpenAI ($25 credits)
|
| 189 |
+
- β
**MCP Web Search** - Intelligent video search
|
| 190 |
+
- β
**MCP Web Fetch** - Video retrieval
|
| 191 |
+
- π§ **Modal Processing** - Text overlay (next)
|
| 192 |
+
- π§ **ElevenLabs Voice** - Audio narration (next)
|
| 193 |
+
|
| 194 |
+
### π Hackathon: MCP 1st Birthday
|
| 195 |
+
**Track:** MCP in Action - Creative Category
|
| 196 |
+
**Built with:** Gradio 6 + OpenAI + MCP + Modal + ElevenLabs
|
|
|
|
|
|
|
| 197 |
""")
|
| 198 |
|
| 199 |
generate_btn.click(
|
| 200 |
+
autonomous_agent_pipeline,
|
| 201 |
inputs=[niche, style],
|
| 202 |
outputs=[output, video_output]
|
| 203 |
)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
gradio
|
| 2 |
-
openai
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
openai
|
| 3 |
+
requests
|