Spaces:
Sleeping
Sleeping
import os | |
from crewai import Agent, Task, Crew, Process | |
# from crewai_tools import CodeInterpreterTool | |
# Model options | |
llm_models = [ | |
"gemini/gemini-1.5-flash", | |
"gemini/gemini-1.5-pro", | |
"gemini/gemini-pro" | |
] | |
selected_model = llm_models[0] | |
def set_model(selected_model_name): | |
global selected_model | |
selected_model = selected_model_name | |
def configure_api_keys(gemini_api_key): | |
if not gemini_api_key: | |
raise ValueError("Gemini API key is required") | |
os.environ['GEMINI_API_KEY'] = gemini_api_key | |
def run_crew_game(gemini_api_key, game): | |
try: | |
# Configure API keys (no search tool used) | |
configure_api_keys(gemini_api_key) | |
# Agents | |
senior_game_developer_agent = Agent( | |
role='Senior Game Developer', | |
goal='Design and develop engaging games', | |
verbose=True, | |
backstory=( | |
"You are a Senior Game Developer at a leading game development studio. " | |
"Your expertise lies in game design, programming in Python, and creating immersive gaming experiences using pygame. " | |
"You strive to produce high-quality, innovative games that captivate players." | |
), | |
allow_delegation=True, | |
llm=selected_model, | |
) | |
qa_engineer_agent = Agent( | |
role='Software Quality Control Engineer', | |
goal='Create Perfect code, by analyzing the code that is given for errors', | |
backstory=( | |
"You are a software engineer that specializes in checking code for errors. " | |
"You have an eye for detail and a knack for finding hidden bugs. " | |
"You check for missing imports, variable declarations, mismatched brackets, syntax errors, " | |
"security vulnerabilities, and logic errors." | |
), | |
allow_delegation=False, | |
verbose=True, | |
# allow_code_execution=True, | |
llm=selected_model, | |
# tools=[CodeInterpreterTool()], | |
) | |
chief_qa_engineer_agent = Agent( | |
role='Chief Software Quality Control Engineer', | |
goal='Ensure that the code does the job that it is supposed to do', | |
backstory=( | |
"You feel that programmers always do only half the job, so you are super dedicated to making high quality code." | |
), | |
allow_delegation=True, | |
# allow_code_execution=True, | |
verbose=True, | |
llm=selected_model, | |
# tools=[CodeInterpreterTool()], | |
) | |
# Tasks | |
code_task = Task( | |
description=f'''You will create a game using Python, these are the instructions: | |
Instructions | |
------------ | |
{game} | |
''', | |
expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.', | |
agent=senior_game_developer_agent, | |
) | |
review_task = Task( | |
description=f'''You will create a game using Python, these are the instructions: | |
Instructions | |
------------ | |
{game} | |
Using the code you got, check for errors. Check for logic errors, | |
syntax errors, missing imports, variable declarations, mismatched brackets, | |
and security vulnerabilities. | |
''', | |
expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.', | |
agent=qa_engineer_agent, | |
) | |
evaluate_task = Task( | |
description=f'''You are helping create a game using Python, these are the instructions: | |
Instructions | |
------------ | |
{game} | |
You will look over the code to ensure that it is complete and | |
does the job that it is supposed to do. | |
''', | |
expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.', | |
agent=chief_qa_engineer_agent, | |
output_file='game.py' | |
) | |
crew = Crew( | |
agents=[senior_game_developer_agent , qa_engineer_agent, chief_qa_engineer_agent], | |
tasks=[code_task, review_task, evaluate_task], | |
process=Process.sequential, | |
verbose=True, | |
max_rpm=100, | |
share_crew=True, | |
output_log_file=True | |
) | |
crew.kickoff(inputs={'game': game}) | |
with open("game.py", "r", encoding='utf-8') as f: | |
content = f.read() | |
with open("logs.txt", 'r', encoding='utf-8') as f: | |
logs = f.read() | |
with open("logs.txt", 'w', encoding='utf-8') as f: | |
f.truncate(0) | |
return content, logs | |
except Exception as e: | |
return f"Error: {str(e)}", str(e) | |