Spaces:
Sleeping
Sleeping
Updated to access information from server
Browse files
.env-sample
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
HF_TOKEN=
|
| 2 |
+
NOODLE_PUBLIC_API_URL=
|
| 3 |
+
GAME_DEPLOYMENT_URL=
|
app.py
CHANGED
|
@@ -2,7 +2,11 @@ import gradio as gr
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from smol import ChatAgent
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
GLOBAL_STATE_FILE = "global_state.json"
|
| 7 |
if os.path.exists(GLOBAL_STATE_FILE):
|
| 8 |
with open(GLOBAL_STATE_FILE, "r") as f:
|
|
@@ -40,7 +44,6 @@ def chat_function(user_prompt, history):
|
|
| 40 |
# Clear the input and return the updated history
|
| 41 |
return "", history
|
| 42 |
|
| 43 |
-
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
gr.Markdown("## Live Game & Chat Interface")
|
| 46 |
|
|
@@ -50,8 +53,8 @@ with gr.Blocks() as demo:
|
|
| 50 |
gr.Markdown("### Game")
|
| 51 |
# The iframe points to your static Game
|
| 52 |
gr.HTML(
|
| 53 |
-
value="""
|
| 54 |
-
<iframe src="
|
| 55 |
"""
|
| 56 |
)
|
| 57 |
# Right Column: Chat interface (smaller column)
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
from smol import ChatAgent
|
| 5 |
+
from dotenv import load_dotenv, dotenv_values
|
| 6 |
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
GAME_DEPLOYMENT_URL= os.getenv("GAME_DEPLOYMENT_URL")
|
| 10 |
GLOBAL_STATE_FILE = "global_state.json"
|
| 11 |
if os.path.exists(GLOBAL_STATE_FILE):
|
| 12 |
with open(GLOBAL_STATE_FILE, "r") as f:
|
|
|
|
| 44 |
# Clear the input and return the updated history
|
| 45 |
return "", history
|
| 46 |
|
|
|
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("## Live Game & Chat Interface")
|
| 49 |
|
|
|
|
| 53 |
gr.Markdown("### Game")
|
| 54 |
# The iframe points to your static Game
|
| 55 |
gr.HTML(
|
| 56 |
+
value=f"""
|
| 57 |
+
<iframe src="{GAME_DEPLOYMENT_URL}"style="width:100%; height:600px; border:none;"></iframe>
|
| 58 |
"""
|
| 59 |
)
|
| 60 |
# Right Column: Chat interface (smaller column)
|
smol.py
CHANGED
|
@@ -3,7 +3,7 @@ import shutil
|
|
| 3 |
|
| 4 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel
|
| 5 |
from dotenv import load_dotenv, dotenv_values
|
| 6 |
-
from tool import FindFilesTool, GitPushTool, FileReplaceTool, ProcessFlowIdentifierTool, GetImageDimensionsTool, FileModifyTool
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
@@ -28,6 +28,7 @@ step 4: run appropriate tool to accomplish task
|
|
| 28 |
step 5: upload changes to github
|
| 29 |
"""
|
| 30 |
|
|
|
|
| 31 |
find_files_tool = FindFilesTool()
|
| 32 |
file_replace_tool = FileReplaceTool()
|
| 33 |
process_identifier_tool= ProcessFlowIdentifierTool()
|
|
@@ -37,33 +38,38 @@ file_modify_tool = FileModifyTool()
|
|
| 37 |
def ChatAgent(userPrompt):
|
| 38 |
#Identify purpose of prompt
|
| 39 |
promptCleanerAgent = CodeAgent(tools=[], model=model)
|
|
|
|
| 40 |
instructions = promptCleanerAgent.run(f"""
|
| 41 |
determine the purpose of the following string "{userPrompt}" if it is one of the following: [asset_change, script_update, conversation].
|
| 42 |
asset_change: The user wants to change an asset in the game.
|
| 43 |
script_update: The user wants to update the game script.
|
| 44 |
conversation: The user wants to have a conversation with the AI by asking general questions or greetings.
|
| 45 |
""")
|
| 46 |
-
|
| 47 |
appDescription = """
|
| 48 |
-
This is
|
|
|
|
|
|
|
| 49 |
uses typescript and sandpack. The folder components/sandpack-examples.tsx file contains the game logic and scripts.
|
| 50 |
"""
|
|
|
|
| 51 |
if(instructions == "conversation"):
|
| 52 |
contextPrompt = f"""
|
| 53 |
User prompt '{userPrompt}'
|
| 54 |
1) Reply to the user as a friendly ai agent.
|
| 55 |
-
2) Do not use any tools to modify files.
|
| 56 |
3) End process after replying to the user.
|
| 57 |
"""
|
| 58 |
else:
|
| 59 |
contextPrompt = f'using process_identifier_tool look for the appropriate instructions for "{instructions}" and apply it to the user prompt after this'
|
| 60 |
|
| 61 |
-
agent = CodeAgent(tools=[find_files_tool, process_identifier_tool, image_generation_tool, file_modify_tool, get_image_dimensions_tool, file_replace_tool], model=model)
|
|
|
|
| 62 |
if instructions == "conversation":
|
| 63 |
response = agent.run(f"{contextPrompt}")
|
| 64 |
else:
|
| 65 |
response = agent.run(f"{appDescription} {contextPrompt} {userPrompt} ")
|
| 66 |
-
|
| 67 |
# Step 1: Prompt reception
|
| 68 |
print(f"Response made: {response}")
|
| 69 |
return response
|
|
|
|
| 3 |
|
| 4 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel
|
| 5 |
from dotenv import load_dotenv, dotenv_values
|
| 6 |
+
from tool import GetGameFileNamesTool, FindFilesTool, GitPushTool, FileReplaceTool, ProcessFlowIdentifierTool, GetImageDimensionsTool, FileModifyTool
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
| 28 |
step 5: upload changes to github
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
get_game_file_names_tool = GetGameFileNamesTool()
|
| 32 |
find_files_tool = FindFilesTool()
|
| 33 |
file_replace_tool = FileReplaceTool()
|
| 34 |
process_identifier_tool= ProcessFlowIdentifierTool()
|
|
|
|
| 38 |
def ChatAgent(userPrompt):
|
| 39 |
#Identify purpose of prompt
|
| 40 |
promptCleanerAgent = CodeAgent(tools=[], model=model)
|
| 41 |
+
|
| 42 |
instructions = promptCleanerAgent.run(f"""
|
| 43 |
determine the purpose of the following string "{userPrompt}" if it is one of the following: [asset_change, script_update, conversation].
|
| 44 |
asset_change: The user wants to change an asset in the game.
|
| 45 |
script_update: The user wants to update the game script.
|
| 46 |
conversation: The user wants to have a conversation with the AI by asking general questions or greetings.
|
| 47 |
""")
|
| 48 |
+
|
| 49 |
appDescription = """
|
| 50 |
+
This is an ai python app that iframes a nextjs 2d platformer game. The goal of this app is to have the user
|
| 51 |
+
chat with an ai agent to modify the nextjs game. The user can ask the ai agent to modify the game assets, scripts, or have a conversation.
|
| 52 |
+
The game is a 2d platformer game where the player controls a ball that bounces off platforms which falling down. The nextjs app
|
| 53 |
uses typescript and sandpack. The folder components/sandpack-examples.tsx file contains the game logic and scripts.
|
| 54 |
"""
|
| 55 |
+
|
| 56 |
if(instructions == "conversation"):
|
| 57 |
contextPrompt = f"""
|
| 58 |
User prompt '{userPrompt}'
|
| 59 |
1) Reply to the user as a friendly ai agent.
|
| 60 |
+
2) Do not use any tools to modify files but you can use get_game_file_names_tool to check for game directories.
|
| 61 |
3) End process after replying to the user.
|
| 62 |
"""
|
| 63 |
else:
|
| 64 |
contextPrompt = f'using process_identifier_tool look for the appropriate instructions for "{instructions}" and apply it to the user prompt after this'
|
| 65 |
|
| 66 |
+
agent = CodeAgent(tools=[get_game_file_names_tool, find_files_tool, process_identifier_tool, image_generation_tool, file_modify_tool, get_image_dimensions_tool, file_replace_tool], model=model)
|
| 67 |
+
|
| 68 |
if instructions == "conversation":
|
| 69 |
response = agent.run(f"{contextPrompt}")
|
| 70 |
else:
|
| 71 |
response = agent.run(f"{appDescription} {contextPrompt} {userPrompt} ")
|
| 72 |
+
|
| 73 |
# Step 1: Prompt reception
|
| 74 |
print(f"Response made: {response}")
|
| 75 |
return response
|
tool.py
CHANGED
|
@@ -1,6 +1,13 @@
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
class FileReaderTool(Tool):
|
| 5 |
name = "file_reader_tool"
|
| 6 |
description = """
|
|
@@ -57,10 +64,6 @@ class FileModifyTool(Tool):
|
|
| 57 |
output_type = "string"
|
| 58 |
|
| 59 |
def forward(self,file_location, prompt) -> str:
|
| 60 |
-
load_dotenv()
|
| 61 |
-
|
| 62 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 63 |
-
|
| 64 |
file_reader_tool = FileReaderTool()
|
| 65 |
file_write_tool = FileWriteTool()
|
| 66 |
|
|
@@ -218,6 +221,25 @@ class GitPushTool(Tool):
|
|
| 218 |
except subprocess.CalledProcessError as e:
|
| 219 |
return print(f"An error occurred while performing Git operations: {e}")
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
class FindFilesTool(Tool):
|
| 222 |
name = "find_files"
|
| 223 |
description = "Find files with a given extension in a directory and its subdirectories"
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, Tool, HfApiModel
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
NOODLE_PUBLIC_API_URL = os.getenv("NOODLE_PUBLIC_API_URL")
|
| 10 |
+
|
| 11 |
class FileReaderTool(Tool):
|
| 12 |
name = "file_reader_tool"
|
| 13 |
description = """
|
|
|
|
| 64 |
output_type = "string"
|
| 65 |
|
| 66 |
def forward(self,file_location, prompt) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
file_reader_tool = FileReaderTool()
|
| 68 |
file_write_tool = FileWriteTool()
|
| 69 |
|
|
|
|
| 221 |
except subprocess.CalledProcessError as e:
|
| 222 |
return print(f"An error occurred while performing Git operations: {e}")
|
| 223 |
|
| 224 |
+
class GetGameFileNamesTool(Tool):
|
| 225 |
+
name="get_game_file_names_tool"
|
| 226 |
+
description="""
|
| 227 |
+
Use this tool to get the game directories of files related to the game. The output of this is an array of file paths.
|
| 228 |
+
Return only the array of file paths returned by the api call.
|
| 229 |
+
"""
|
| 230 |
+
inputs = {}
|
| 231 |
+
output_type = "string"
|
| 232 |
+
def forward(self) -> str:
|
| 233 |
+
get_files_api = NOODLE_PUBLIC_API_URL + "/api/files/"
|
| 234 |
+
|
| 235 |
+
response = requests.get(get_files_api)
|
| 236 |
+
if response.status_code == 200:
|
| 237 |
+
print(response.json()['content'])
|
| 238 |
+
else:
|
| 239 |
+
print("Failed to get file content")
|
| 240 |
+
return response.json()['content']
|
| 241 |
+
|
| 242 |
+
|
| 243 |
class FindFilesTool(Tool):
|
| 244 |
name = "find_files"
|
| 245 |
description = "Find files with a given extension in a directory and its subdirectories"
|