Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,7 @@ def randomly_select_from_json(world_data):
|
|
| 48 |
"character": {"name": character_name, "description": character_description},
|
| 49 |
}
|
| 50 |
|
| 51 |
-
#
|
| 52 |
def initialize_game_state():
|
| 53 |
random_state = randomly_select_from_json(world_data)
|
| 54 |
world = random_state["world"]
|
|
@@ -93,13 +93,20 @@ def initialize_game_state():
|
|
| 93 |
"start": start,
|
| 94 |
}
|
| 95 |
|
| 96 |
-
#
|
| 97 |
game_state = initialize_game_state()
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
| 100 |
if message.lower() == "start game":
|
| 101 |
return game_state["start"]
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
system_prompt = """You are an AI Game master. Your job is to write what \
|
| 104 |
happens next in a player's adventure game.\
|
| 105 |
Instructions: \
|
|
@@ -114,6 +121,7 @@ def run_action(message, history, game_state):
|
|
| 114 |
Town: {game_state['town']}
|
| 115 |
Your Character: {game_state['character']}"""
|
| 116 |
|
|
|
|
| 117 |
messages = [
|
| 118 |
{"role": "system", "content": system_prompt},
|
| 119 |
{"role": "user", "content": world_info},
|
|
@@ -124,8 +132,10 @@ def run_action(message, history, game_state):
|
|
| 124 |
messages.append({"role": "assistant", "content": action[0]})
|
| 125 |
messages.append({"role": "user", "content": action[1]})
|
| 126 |
|
|
|
|
| 127 |
messages.append({"role": "user", "content": message})
|
| 128 |
|
|
|
|
| 129 |
model_output = client.chat.completions.create(
|
| 130 |
model="meta-llama/Llama-3-70b-chat-hf",
|
| 131 |
messages=messages,
|
|
@@ -133,27 +143,25 @@ def run_action(message, history, game_state):
|
|
| 133 |
|
| 134 |
return model_output.choices[0].message.content
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
chatbot
|
| 140 |
-
|
| 141 |
-
placeholder="Type 'start game' to begin or
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
msg_input.submit(handle_message, [msg_input, chatbot], chatbot)
|
| 156 |
-
start_again.click(restart_game, inputs=[], outputs=chatbot)
|
| 157 |
|
| 158 |
# Launch the game
|
| 159 |
demo.launch(share=True, server_name="0.0.0.0")
|
|
|
|
|
|
| 48 |
"character": {"name": character_name, "description": character_description},
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# Function to initialize or reinitialize the game state
|
| 52 |
def initialize_game_state():
|
| 53 |
random_state = randomly_select_from_json(world_data)
|
| 54 |
world = random_state["world"]
|
|
|
|
| 93 |
"start": start,
|
| 94 |
}
|
| 95 |
|
| 96 |
+
# Initialize the game state
|
| 97 |
game_state = initialize_game_state()
|
| 98 |
|
| 99 |
+
# Function to process user input and actions
|
| 100 |
+
def run_action(message, history):
|
| 101 |
+
global game_state # Access the global game state
|
| 102 |
+
|
| 103 |
if message.lower() == "start game":
|
| 104 |
return game_state["start"]
|
| 105 |
|
| 106 |
+
if message.lower() == "start again":
|
| 107 |
+
game_state = initialize_game_state()
|
| 108 |
+
return "Game restarted! " + game_state["start"]
|
| 109 |
+
|
| 110 |
system_prompt = """You are an AI Game master. Your job is to write what \
|
| 111 |
happens next in a player's adventure game.\
|
| 112 |
Instructions: \
|
|
|
|
| 121 |
Town: {game_state['town']}
|
| 122 |
Your Character: {game_state['character']}"""
|
| 123 |
|
| 124 |
+
# Build the context for the conversation
|
| 125 |
messages = [
|
| 126 |
{"role": "system", "content": system_prompt},
|
| 127 |
{"role": "user", "content": world_info},
|
|
|
|
| 132 |
messages.append({"role": "assistant", "content": action[0]})
|
| 133 |
messages.append({"role": "user", "content": action[1]})
|
| 134 |
|
| 135 |
+
# Add the user's current action
|
| 136 |
messages.append({"role": "user", "content": message})
|
| 137 |
|
| 138 |
+
# Get the model's response
|
| 139 |
model_output = client.chat.completions.create(
|
| 140 |
model="meta-llama/Llama-3-70b-chat-hf",
|
| 141 |
messages=messages,
|
|
|
|
| 143 |
|
| 144 |
return model_output.choices[0].message.content
|
| 145 |
|
| 146 |
+
# Gradio ChatInterface with the custom "start again" logic
|
| 147 |
+
demo = gr.ChatInterface(
|
| 148 |
+
run_action,
|
| 149 |
+
chatbot=gr.Chatbot(
|
| 150 |
+
height=250,
|
| 151 |
+
placeholder="Type 'start game' to begin or 'start again' to restart the adventure",
|
| 152 |
+
type="messages",
|
| 153 |
+
),
|
| 154 |
+
textbox=gr.Textbox(
|
| 155 |
+
placeholder="What do you do next?",
|
| 156 |
+
container=False,
|
| 157 |
+
scale=7,
|
| 158 |
+
),
|
| 159 |
+
title="AI RPG",
|
| 160 |
+
theme="soft",
|
| 161 |
+
examples=["Look around", "Continue the story"],
|
| 162 |
+
cache_examples=False,
|
| 163 |
+
)
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# Launch the game
|
| 166 |
demo.launch(share=True, server_name="0.0.0.0")
|
| 167 |
+
|