shivrajkarewar's picture
Create app.py
80d070e verified
import gradio as gr
from crewai import Agent, Task, Crew
import os
# Load API key from environment variable
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
from langchain.chat_models import ChatOpenAI
# Initialize the language model
llm = ChatOpenAI(
openai_api_key=os.environ["OPENAI_API_KEY"],
model_name="gpt-3.5-turbo"
)
# Define AI agents
researcher = Agent(
role="Lead Research Analyst",
goal="Extract comprehensive insights on the provided subject",
backstory="A domain expert known for deep research and analytical precision.",
allow_delegation=False,
llm=llm
)
writer = Agent(
role="Professional Content Developer",
goal="Develop captivating and informative written content",
backstory="Skilled in transforming technical details into accessible narratives for broad audiences.",
allow_delegation=False,
llm=llm
)
editor = Agent(
role="Editorial Specialist",
goal="Refine articles to meet high standards of quality and coherence",
backstory="An editing professional with a passion for perfecting written communication.",
allow_delegation=False,
llm=llm
)
# Core workflow function
def generate_article_with_crewai(topic):
# Define agent tasks
research_task = Task(
description=f"Carry out thorough research on the topic: {topic}. Compile a well-organized summary.",
expected_output="An insightful and structured research overview highlighting key details and developments.",
agent=researcher
)
writing_task = Task(
description=f"Based on the research, compose a comprehensive 1000-word article on: {topic} with clear sections.",
expected_output="A detailed, engaging, and reader-friendly article suited for web publication.",
agent=writer
)
editing_task = Task(
description=f"Proofread and edit the draft article on {topic} to ensure fluidity, grammar accuracy, and overall appeal.",
expected_output="A polished, engaging final draft ready for publishing.",
agent=editor
)
# Assemble and run the crew
crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
verbose=True # Turn off for cleaner logs
)
final_output = crew.kickoff()
return final_output
# Gradio UI
def handle_topic_input(topic):
return generate_article_with_crewai(topic)
interface = gr.Interface(
fn=handle_topic_input,
inputs=gr.Textbox(lines=2, placeholder="Enter your topic here..."),
outputs=gr.Textbox(lines=20, label="Finalized Article"),
title="AI-Powered Article Generator with Research & Editing",
description="Input a topic to get a fully generated article researched, written, and edited by specialized AI agents."
)
if __name__ == "__main__":
interface.launch()