Spaces:
Sleeping
Sleeping
File size: 6,625 Bytes
cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 cc98b5f 7a4e132 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | import os
import textwrap
# βββ CrewAI core βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool, ScrapeWebsiteTool
# βββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
import gradio as gr
# =============================================================================
# API KEYS
# =============================================================================
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
SERPER_API_KEY = os.environ.get("SERPER_API_KEY", "")
if not OPENAI_API_KEY:
raise EnvironmentError(
"β OPENAI_API_KEY not found. "
"Add it in Space β Settings β Variables and secrets."
)
if not SERPER_API_KEY:
raise EnvironmentError(
"β SERPER_API_KEY not found. "
"Add it in Space β Settings β Variables and secrets."
)
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
os.environ["SERPER_API_KEY"] = SERPER_API_KEY
# =============================================================================
# LLM
# =============================================================================
llm = LLM(
model="gpt-4o-mini",
temperature=0.4,
max_tokens=2000,
)
# =============================================================================
# TOOLS
# =============================================================================
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()
# =============================================================================
# MEMORY
# =============================================================================
MEMORY_DIR = "/tmp/crewai_memory"
os.makedirs(MEMORY_DIR, exist_ok=True)
EMBEDDER_CONFIG = {
"provider": "openai",
"config": {
"model": "text-embedding-3-small",
"api_key": OPENAI_API_KEY,
},
}
# =============================================================================
# AGENTS
# =============================================================================
researcher = Agent(
role="Senior Research Analyst",
goal=(
"Find the most accurate, up-to-date, and relevant information on the given topic."
),
backstory="Veteran research analyst with deep investigation skills.",
tools=[search_tool, scrape_tool],
llm=llm,
verbose=True,
allow_delegation=False,
max_iter=5,
)
writer = Agent(
role="Professional Content Writer",
goal="Write a compelling structured article from research.",
backstory="Expert in long-form and technical writing.",
llm=llm,
verbose=True,
allow_delegation=False,
max_iter=4,
)
editor = Agent(
role="Senior Editor",
goal="Polish article to publication quality.",
backstory="Meticulous editor ensuring clarity and consistency.",
llm=llm,
verbose=True,
allow_delegation=False,
max_iter=3,
)
# =============================================================================
# TASK BUILDER
# =============================================================================
def build_tasks(topic: str, audience: str, tone: str, length: str):
research_task = Task(
description=f"Research topic: {topic}",
expected_output="Structured research summary",
agent=researcher,
)
writing_task = Task(
description=f"Write article on: {topic}",
expected_output="Full article",
agent=writer,
context=[research_task],
)
editing_task = Task(
description="Edit article to final form",
expected_output="Final polished article",
agent=editor,
context=[writing_task],
)
return [research_task, writing_task, editing_task]
# =============================================================================
# CREW RUNNER
# =============================================================================
def run_crew(topic: str, audience: str, tone: str, length: str):
if not topic.strip():
return "Please enter a topic."
try:
tasks = build_tasks(topic, audience, tone, length)
crew = Crew(
agents=[researcher, writer, editor],
tasks=tasks,
process=Process.sequential,
verbose=True,
memory=True,
embedder=EMBEDDER_CONFIG,
memory_config={"long_term": {"storage_path": MEMORY_DIR}},
)
result = crew.kickoff()
return str(result)
except Exception as e:
return f"Error: {str(e)}"
# =============================================================================
# UI
# =============================================================================
with gr.Blocks() as demo:
gr.Markdown("# Multi-Agent Article Generator")
with gr.Row():
with gr.Column():
topic_input = gr.Textbox(label="Topic", lines=3)
audience_input = gr.Dropdown(
["General Public", "Business", "Students"],
value="General Public",
label="Audience",
)
tone_input = gr.Dropdown(
["Informative", "Conversational", "Academic"],
value="Informative",
label="Tone",
)
length_input = gr.Radio(
["Short", "Medium", "Long"],
value="Medium",
label="Length",
)
submit_btn = gr.Button("Generate")
clear_btn = gr.Button("Clear")
with gr.Column():
output_box = gr.Textbox(
label="Output",
lines=30,
interactive=False,
)
submit_btn.click(
fn=run_crew,
inputs=[topic_input, audience_input, tone_input, length_input],
outputs=output_box,
)
clear_btn.click(
fn=lambda: ("", ""),
inputs=[],
outputs=[topic_input, output_box],
)
# =============================================================================
# LAUNCH (UPDATED FOR GRADIO 6)
# =============================================================================
demo.launch(
server_name="0.0.0.0",
server_port=7860,
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate"),
css="""
footer { display: none !important; }
""",
) |