Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from crewai import Agent, Task, Crew, Process | |
os.environ["OPENAI_API_KEY"] = "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" | |
from crewai import Agent, Task, Crew, Process | |
def run_therapy_session(group_size, topic): | |
participant_names = ['Alice', 'Bob', 'Charlie', 'Diana', 'Ethan', 'Fiona', 'George', 'Hannah', 'Ivan'] | |
if group_size > len(participant_names) + 1: # +1 for the therapist | |
return "Group size exceeds the number of available participant names." | |
# Create the therapist agent | |
dr_smith = Agent( | |
role='Therapist', | |
goal='Facilitate a supportive group discussion', | |
backstory='An experienced therapist specializing in group dynamics.', | |
verbose=True, | |
allow_delegation=False | |
) | |
# Create participant agents | |
participants = [Agent( | |
role=f'Group Therapy Participant - {name}', | |
goal='Participate in group therapy', | |
backstory=f'{name} is interested in sharing and learning from the group.', | |
verbose=True, | |
allow_delegation=False) | |
for name in participant_names[:group_size - 1]] | |
participants.append(dr_smith) | |
# Define tasks for each participant | |
tasks = [Task(description=f'{participant.role.split(" - ")[-1]}, please share your thoughts on the topic: "{topic}".', agent=participant) | |
for participant in participants] | |
# Instantiate the crew with a sequential process | |
therapy_crew = Crew( | |
agents=participants, | |
tasks=tasks, | |
process=Process.sequential, | |
verbose=True | |
) | |
# Start the group therapy session | |
result = therapy_crew.kickoff() | |
# Simulating a conversation (placeholder, adjust based on CrewAI capabilities) | |
conversation = "\n".join([f"{participant.role.split(' - ')[-1]}: [Participant's thoughts on '{topic}']" for participant in participants]) | |
return result | |
# Gradio interface | |
iface = gr.Interface( | |
fn=run_therapy_session, | |
inputs=[ | |
gr.Slider(minimum=2, maximum=10, label="Group Size", value=4), | |
gr.Textbox(lines=2, placeholder="Enter a topic or question for discussion", label="Discussion Topic") | |
], | |
outputs="text" | |
) | |
# Launch the interface | |
iface.launch() | |
# Choosing topics | |
''' | |
def run_crew(topic): | |
# Define your agents | |
researcher = Agent( | |
role='Senior Research Analyst', | |
goal='Uncover cutting-edge developments', | |
backstory="""You are a Senior Research Analyst at a leading tech think tank...""", | |
verbose=True, | |
allow_delegation=False | |
) | |
writer = Agent( | |
role='Tech Content Strategist', | |
goal='Craft compelling content', | |
backstory="""You are a renowned Tech Content Strategist...""", | |
verbose=True, | |
allow_delegation=False | |
) | |
# Assign tasks based on the selected topic | |
if topic == "write short story": | |
task_description = "Write a captivating short story about a journey through a futuristic city." | |
elif topic == "write an article": | |
task_description = "Compose an insightful article on the latest trends in technology." | |
elif topic == "analyze stock": | |
task_description = "Perform a detailed analysis of recent trends in the stock market." | |
elif topic == "create a vacation": | |
task_description = "Plan a perfect vacation itinerary for a family trip to Europe." | |
task1 = Task( | |
description=task_description, | |
agent=researcher | |
) | |
task2 = Task( | |
description=f"Use the findings from the researcher's task to develop a comprehensive report on '{topic}'.", | |
agent=writer | |
) | |
# Instantiate your crew with a sequential process | |
crew = Crew( | |
agents=[researcher, writer], | |
tasks=[task1, task2], | |
verbose=2, | |
process=Process.sequential | |
) | |
# Get your crew to work! | |
result = crew.kickoff() | |
return result | |
# Gradio Interface with Dropdown for Topic Selection | |
iface = gr.Interface( | |
fn=run_crew, | |
inputs=gr.Dropdown(choices=["write short story", "write an article", "analyze stock", "create a vacation"], label="Select Topic"), | |
outputs="text", | |
title="AI Research and Writing Crew", | |
description="Select a topic and click the button to run the crew of AI agents." | |
) | |
iface.launch() | |
''' | |