initial commit

This commit is contained in:
badhezi 2024-12-06 13:06:17 +07:00
commit 0e534a1fc4
6 changed files with 220 additions and 0 deletions

31
Dockerfile Normal file

@ -0,0 +1,31 @@
# Use a slim base image to reduce potential vulnerabilities
FROM python:3.10-bookworm
# Create a non-root user and group with home directory set to /usr/src/app
RUN useradd --no-log-init -r -m -d /usr/src/app appuser
# Set the working directory
WORKDIR /usr/src/app
# Copy the requirements file and install the dependencies as root
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code and set ownership to appuser
COPY . .
# Change ownership of the application directory to appuser
RUN chown -R appuser:appuser /usr/src/app
# Switch to the non-root user
USER appuser
# Expose the application port
EXPOSE 7860
# Set environment variables
ENV GRADIO_SERVER_NAME="0.0.0.0"
CMD ["python", "app.py"]

29
README.md Normal file

@ -0,0 +1,29 @@
## agents.py
This file contains the definition of custom agents.
To create a Agent, you need to define the following:
1. Role: The role of the agent.
2. Backstory: The backstory of the agent.
3. Goal: The goal of the agent.
4. Tools: The tools that the agent has access to (optional).
5. Allow Delegation: Whether the agent can delegate tasks to other agents(optional).
[More Details about Agent](https://docs.crewai.com/concepts/agents).
## task.py
This file contains the definition of custom tasks.
To Create a task, you need to define the following :
1. description: A string that describes the task.
2. agent: An agent object that will be assigned to the task.
3. expected_output: The expected output of the task.
[More Details about Task](https://docs.crewai.com/concepts/tasks).
## crew (main.py)
This is the main file that you will use to run your custom crew.
To create a Crew , you need to define Agent ,Task and following Parameters:
1. Agent: List of agents that you want to include in the crew.
2. Task: List of tasks that you want to include in the crew.
3. verbose: If True, print the output of each task.(default is False).
4. debug: If True, print the debug logs.(default is False).
[More Details about Crew](https://docs.crewai.com/concepts/crew).

35
agents.py Normal file

@ -0,0 +1,35 @@
from crewai import Agent, LLM
from textwrap import dedent
# This is an example of how to define custom agents.
# You can define as many agents as you want.
# You can also define custom tasks in tasks.py
class CustomAgents:
def __init__(self):
self.OpenAILlama323 = LLM(
model="openai/llama-3.2-3B-instruct",
api_key="na",
base_url='https://hub.societyai.com/models/llama-3-2-3b/openai/v1'
)
def agent_1_name(self):
return Agent(
role="Define agent 1 role here",
backstory=dedent(f"""Define agent 1 backstory here"""),
goal=dedent(f"""Define agent 1 goal here"""),
# tools=[tool_1, tool_2],
allow_delegation=False,
verbose=True,
llm=self.OpenAILlama323,
)
def agent_2_name(self):
return Agent(
role="Define agent 2 role here",
backstory=dedent(f"""Define agent 2 backstory here"""),
goal=dedent(f"""Define agent 2 goal here"""),
# tools=[tool_1, tool_2],
allow_delegation=False,
verbose=True,
llm=self.OpenAILlama323,
)

80
app.py Normal file

@ -0,0 +1,80 @@
import os
from crewai import Crew
from agents import CustomAgents
from tasks import CustomTasks
class CustomCrew:
def __init__(self, var1, var2):
self.var1 = var1
self.var2 = var2
def run(self):
agents = CustomAgents()
tasks = CustomTasks()
# Define agents
custom_agent_1 = agents.agent_1_name()
custom_agent_2 = agents.agent_2_name()
# Define tasks
custom_task_1 = tasks.task_1_name(custom_agent_1, self.var1, self.var2)
custom_task_2 = tasks.task_2_name(custom_agent_2)
# Create and run the crew
crew = Crew(
agents=[custom_agent_1, custom_agent_2],
tasks=[custom_task_1, custom_task_2],
verbose=True,
)
result = crew.kickoff()
return result
########################################
# GRADIO INTERFACE
########################################
import gradio as gr
def run_crew(var1, var2, history):
# Append a user message
history.append({"role": "user", "content": f"Variable 1: {var1}\nVariable 2: {var2}"})
# Run the crew and ensure result is a string
custom_crew = CustomCrew(var1, var2)
result = custom_crew.run()
if result is None:
result = "No result was returned."
else:
result = str(result)
# Append the result as an assistant message
history.append({"role": "assistant", "content": result})
return history, history
with gr.Blocks(css="footer {visibility: hidden}") as demo:
gr.Markdown("# Crew AI Gradio Interface")
gr.Markdown("Enter your two variables below and click 'Run Crew' to execute.")
with gr.Row():
var1_input = gr.Textbox(label="Variable 1", placeholder="Enter the first variable...")
var2_input = gr.Textbox(label="Variable 2", placeholder="Enter the second variable...")
run_button = gr.Button("Run Crew")
chatbot = gr.Chatbot(label="Conversation", type="messages", height=300)
clear_button = gr.Button("Clear")
state = gr.State([])
run_button.click(
run_crew,
inputs=[var1_input, var2_input, state],
outputs=[chatbot, state],
)
clear_button.click(lambda: [], None, chatbot, queue=False)
clear_button.click(lambda: [], None, state, queue=False)
if __name__ == "__main__":
demo.launch()

3
requirements.txt Normal file

@ -0,0 +1,3 @@
gradio==5.8.0
uv==0.4.28
crewai==0.85.0

42
tasks.py Normal file

@ -0,0 +1,42 @@
# To know more about the Task class, visit: https://docs.crewai.com/concepts/tasks
from crewai import Task
from textwrap import dedent
class CustomTasks:
def __tip_section(self):
return "If you do your BEST WORK, I'll give you a $10,000 commission!"
def task_1_name(self, agent, var1, var2):
return Task(
description=dedent(
f"""
Do something as part of task 1
{self.__tip_section()}
Make sure to use the most recent data as possible.
Use this variable: {var1}
And also this variable: {var2}
"""
),
expected_output="The expected output of the task",
agent=agent,
)
def task_2_name(self, agent):
return Task(
description=dedent(
f"""
Take the input from task 1 and do something with it.
generate the result in nicely formatted markdown
{self.__tip_section()}
Make sure to do something else.
"""
),
expected_output="The expected output of the task",
agent=agent,
)