Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- __pycache__/agent_manager.cpython-313.pyc +0 -0
- __pycache__/marketing_graph.cpython-313.pyc +0 -0
- agent_manager.py +25 -0
- main.py +78 -0
- marketing_graph.py +40 -0
__pycache__/agent_manager.cpython-313.pyc
ADDED
|
Binary file (1.54 kB). View file
|
|
|
__pycache__/marketing_graph.cpython-313.pyc
ADDED
|
Binary file (2.02 kB). View file
|
|
|
agent_manager.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agent_manager.py
|
| 2 |
+
from langchain_ollama import OllamaLLM
|
| 3 |
+
|
| 4 |
+
class AgentManager:
|
| 5 |
+
def __init__(self, model_name="llama3.2:latest"):
|
| 6 |
+
self.llm = OllamaLLM(model=model_name)
|
| 7 |
+
self.logs = []
|
| 8 |
+
|
| 9 |
+
def log(self, message):
|
| 10 |
+
print(message)
|
| 11 |
+
self.logs.append(message)
|
| 12 |
+
|
| 13 |
+
def marketing_fix_node(self, node_name, input_data, error_msg):
|
| 14 |
+
prompt = f"""
|
| 15 |
+
You are a marketing AI agent. Node '{node_name}' failed with error:
|
| 16 |
+
{error_msg}
|
| 17 |
+
|
| 18 |
+
Input:
|
| 19 |
+
{input_data}
|
| 20 |
+
|
| 21 |
+
Generate proper output for this node as if it executed correctly.
|
| 22 |
+
"""
|
| 23 |
+
fixed_output = self.llm.invoke(prompt)
|
| 24 |
+
self.log(f"β
{node_name} recovered:\n{fixed_output}\n")
|
| 25 |
+
return fixed_output
|
main.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from agent_manager import AgentManager
|
| 3 |
+
from marketing_graph import build_marketing_graph
|
| 4 |
+
|
| 5 |
+
def run_marketing_agent(product_name, audience, goal):
|
| 6 |
+
manager = AgentManager(model_name="llama3.2:latest")
|
| 7 |
+
graph = build_marketing_graph()
|
| 8 |
+
|
| 9 |
+
state = {
|
| 10 |
+
"input_data": f"""
|
| 11 |
+
**Product:** {product_name}
|
| 12 |
+
**Target audience:** {audience}
|
| 13 |
+
**Goal:** {goal}
|
| 14 |
+
"""
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
current_node = "MarketResearch"
|
| 18 |
+
executed_nodes = []
|
| 19 |
+
|
| 20 |
+
while current_node:
|
| 21 |
+
executed_nodes.append(current_node)
|
| 22 |
+
node_func = graph.nodes[current_node]
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
output = node_func(state)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
# Self-healing
|
| 28 |
+
fixed = manager.marketing_fix_node(current_node, state.get("input_data", ""), str(e))
|
| 29 |
+
output = {current_node: fixed}
|
| 30 |
+
|
| 31 |
+
state.update(output)
|
| 32 |
+
|
| 33 |
+
# Determine next node
|
| 34 |
+
next_nodes = [to_node for (from_node, to_node) in graph.edges if from_node == current_node]
|
| 35 |
+
current_node = next_nodes[0] if next_nodes else None
|
| 36 |
+
|
| 37 |
+
# Get Market Research separately
|
| 38 |
+
market_output = state.get("MarketResearch", "No Market Research generated")
|
| 39 |
+
|
| 40 |
+
# Format full workflow output in Markdown
|
| 41 |
+
sections = []
|
| 42 |
+
emoji_map = {
|
| 43 |
+
"MarketResearch": "π Market Research",
|
| 44 |
+
"ContentPlanner": "π
Content Plan",
|
| 45 |
+
"PostGenerator": "π£ Generated Posts",
|
| 46 |
+
"AnalyticsAgent": "π Analytics & Suggestions"
|
| 47 |
+
}
|
| 48 |
+
for node in executed_nodes:
|
| 49 |
+
content = state.get(node, "")
|
| 50 |
+
if content:
|
| 51 |
+
sections.append(f"## {emoji_map.get(node, node)}")
|
| 52 |
+
sections.append(content)
|
| 53 |
+
|
| 54 |
+
sections.append("## β¨ FINAL MARKETING RESULT β¨")
|
| 55 |
+
sections.append(f"```json\n{state}\n```")
|
| 56 |
+
|
| 57 |
+
full_strategy = "\n\n".join(sections)
|
| 58 |
+
|
| 59 |
+
return market_output, full_strategy
|
| 60 |
+
|
| 61 |
+
# --- Gradio Interface ---
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=run_marketing_agent,
|
| 64 |
+
inputs=[
|
| 65 |
+
gr.Textbox(label="Product Name", value="Smart fitness band"),
|
| 66 |
+
gr.Textbox(label="Target Audience", value="young professionals"),
|
| 67 |
+
gr.Textbox(label="Goal", value="Increase brand awareness")
|
| 68 |
+
],
|
| 69 |
+
outputs=[
|
| 70 |
+
gr.Markdown(label="Market Research Output"),
|
| 71 |
+
gr.Markdown(label="Full Marketing Strategy")
|
| 72 |
+
],
|
| 73 |
+
title="Self-Healing Marketing Agent",
|
| 74 |
+
description="Enter your product info and get both Market Research and the full marketing strategy."
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
iface.launch()
|
marketing_graph.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# marketing_graph.py
|
| 2 |
+
from langgraph.graph.state import StateGraph
|
| 3 |
+
|
| 4 |
+
# Node functions
|
| 5 |
+
def market_research(state):
|
| 6 |
+
product_info = state.get("input_data", "")
|
| 7 |
+
return {
|
| 8 |
+
"MarketResearch": f"Research: {product_info}\nCompetitors: FitTrack, SmartPulse, ZenBand\nTrends: AI fitness, wearable analytics, app integration."
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def content_planner(state):
|
| 12 |
+
# Simulate failure
|
| 13 |
+
raise RuntimeError("Simulated bug in content planner")
|
| 14 |
+
|
| 15 |
+
def post_generator(state):
|
| 16 |
+
plan_output = state.get("ContentPlanner", "")
|
| 17 |
+
return {"PostGenerator": f"Generated posts for campaign: 'Get Fit with SmartPulse!'"}
|
| 18 |
+
|
| 19 |
+
def analytics_agent(state):
|
| 20 |
+
post_output = state.get("PostGenerator", "")
|
| 21 |
+
return {"AnalyticsAgent": f"Predicted engagement: Instagram > LinkedIn > Twitter. Suggestion: add CTA."}
|
| 22 |
+
|
| 23 |
+
def build_marketing_graph():
|
| 24 |
+
# β
Use a tuple/list of node names as state_schema (hashable)
|
| 25 |
+
state_schema = ("MarketResearch", "ContentPlanner", "PostGenerator", "AnalyticsAgent", "input_data")
|
| 26 |
+
|
| 27 |
+
graph = StateGraph(state_schema) # positional argument works now
|
| 28 |
+
|
| 29 |
+
# Add nodes (function directly)
|
| 30 |
+
graph.add_node("MarketResearch", market_research)
|
| 31 |
+
graph.add_node("ContentPlanner", content_planner)
|
| 32 |
+
graph.add_node("PostGenerator", post_generator)
|
| 33 |
+
graph.add_node("AnalyticsAgent", analytics_agent)
|
| 34 |
+
|
| 35 |
+
# Add edges
|
| 36 |
+
graph.add_edge("MarketResearch", "ContentPlanner")
|
| 37 |
+
graph.add_edge("ContentPlanner", "PostGenerator")
|
| 38 |
+
graph.add_edge("PostGenerator", "AnalyticsAgent")
|
| 39 |
+
|
| 40 |
+
return graph
|