Spaces:
Sleeping
Sleeping
Entire codebase
Browse files
helper.py
CHANGED
|
@@ -91,4 +91,144 @@ def start_game(main_loop, share=False):
|
|
| 91 |
undo_btn="Undo",
|
| 92 |
clear_btn="Clear",
|
| 93 |
)
|
| 94 |
-
demo.launch(share=share, server_name="0.0.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
undo_btn="Undo",
|
| 92 |
clear_btn="Clear",
|
| 93 |
)
|
| 94 |
+
demo.launch(share=share, server_name="0.0.0.0")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def get_game_state(inventory={}):
|
| 110 |
+
world = load_world('../shared_data/Kyropeia.json')
|
| 111 |
+
kingdom = world['kingdoms']['Eldrida']
|
| 112 |
+
town = kingdom['towns']["Luminaria"]
|
| 113 |
+
character = town['npcs']['Elwyn Stormbringer']
|
| 114 |
+
start = world['start']
|
| 115 |
+
|
| 116 |
+
game_state = {
|
| 117 |
+
"world": world['description'],
|
| 118 |
+
"kingdom": kingdom['description'],
|
| 119 |
+
"town": town['description'],
|
| 120 |
+
"character": character['description'],
|
| 121 |
+
"start": start,
|
| 122 |
+
"inventory": inventory
|
| 123 |
+
}
|
| 124 |
+
return game_state
|
| 125 |
+
|
| 126 |
+
def run_action(message, history, game_state):
|
| 127 |
+
|
| 128 |
+
if(message == 'start game'):
|
| 129 |
+
return game_state['start']
|
| 130 |
+
|
| 131 |
+
system_prompt = """You are an AI Game master. Your job is to write what \
|
| 132 |
+
happens next in a player's adventure game.\
|
| 133 |
+
Instructions: \
|
| 134 |
+
You must on only write 1-3 sentences in response. \
|
| 135 |
+
Always write in second person present tense. \
|
| 136 |
+
Ex. (You look north and see...)"""
|
| 137 |
+
|
| 138 |
+
world_info = f"""
|
| 139 |
+
World: {game_state['world']}
|
| 140 |
+
Kingdom: {game_state['kingdom']}
|
| 141 |
+
Town: {game_state['town']}
|
| 142 |
+
Your Character: {game_state['character']}"""
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
messages = [
|
| 146 |
+
{"role": "system", "content": system_prompt},
|
| 147 |
+
{"role": "user", "content": world_info}
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
for action in history:
|
| 151 |
+
messages.append({"role": "assistant", "content": action[0]})
|
| 152 |
+
messages.append({"role": "user", "content": action[1]})
|
| 153 |
+
|
| 154 |
+
messages.append({"role": "user", "content": message})
|
| 155 |
+
client = Together(api_key=get_together_api_key())
|
| 156 |
+
model_output = client.chat.completions.create(
|
| 157 |
+
model="meta-llama/Llama-3-70b-chat-hf",
|
| 158 |
+
messages=messages
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
result = model_output.choices[0].message.content
|
| 162 |
+
return result
|
| 163 |
+
|
| 164 |
+
def start_game(main_loop, share=False):
|
| 165 |
+
demo = gr.ChatInterface(
|
| 166 |
+
main_loop,
|
| 167 |
+
chatbot=gr.Chatbot(height=250, placeholder="Type 'start game' to begin"),
|
| 168 |
+
textbox=gr.Textbox(placeholder="What do you do next?", container=False, scale=7),
|
| 169 |
+
title="AI RPG",
|
| 170 |
+
# description="Ask Yes Man any question",
|
| 171 |
+
theme="soft",
|
| 172 |
+
examples=["Look around", "Continue the story"],
|
| 173 |
+
cache_examples=False,
|
| 174 |
+
retry_btn="Retry",
|
| 175 |
+
undo_btn="Undo",
|
| 176 |
+
clear_btn="Clear",
|
| 177 |
+
)
|
| 178 |
+
demo.launch(share=share, server_name="0.0.0.0")
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
everyone_content_policy = {
|
| 182 |
+
"policy": """O1: Violence and Hate.
|
| 183 |
+
Should not
|
| 184 |
+
- Express hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics.
|
| 185 |
+
- Use slurs based on personal characteristics
|
| 186 |
+
- Write gruesome / graphic violence.
|
| 187 |
+
Can
|
| 188 |
+
- Write tame fantasy violence
|
| 189 |
+
O2: Sexual Content.
|
| 190 |
+
Should not
|
| 191 |
+
- Write any somewhat sexual content that mentions sexual functions or sexual body parts.
|
| 192 |
+
- Write any content that would be inappropriate for kids.
|
| 193 |
+
Can
|
| 194 |
+
- Write romantic content (including kissing, flirting etc...)
|
| 195 |
+
O3: Self-Harm.
|
| 196 |
+
Should not
|
| 197 |
+
- Encourage people to harm themselves.
|
| 198 |
+
- Romanticize or condone self-harm or suicide.
|
| 199 |
+
- Write story depicting suicide.
|
| 200 |
+
O4: Profanity.
|
| 201 |
+
Should not
|
| 202 |
+
- Write with any profane language that wouldn't be appropriate for kids.
|
| 203 |
+
Can
|
| 204 |
+
- Write with kid appropriate language
|
| 205 |
+
O5: Regulated or Controlled Substances.
|
| 206 |
+
Should not
|
| 207 |
+
- Write stories that depicts, glorifies or encourages drug use."""
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
def is_safe(message):
|
| 211 |
+
# Build the prompt with embedded values
|
| 212 |
+
prompt = f"""[INST] Task: Check if there is unsafe content in 'user' messages in conversations according to our safety policy with the below categories.
|
| 213 |
+
|
| 214 |
+
<BEGIN UNSAFE CONTENT CATEGORIES>
|
| 215 |
+
{everyone_content_policy['policy']}
|
| 216 |
+
<END UNSAFE CONTENT CATEGORIES>
|
| 217 |
+
|
| 218 |
+
<BEGIN CONVERSATION>
|
| 219 |
+
user: {message}
|
| 220 |
+
|
| 221 |
+
Provide your safety assessment for user in the above conversation:
|
| 222 |
+
- First line must read 'safe' or 'unsafe'.
|
| 223 |
+
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST]"""
|
| 224 |
+
|
| 225 |
+
client = Together(api_key=get_together_api_key())
|
| 226 |
+
|
| 227 |
+
response = client.completions.create(
|
| 228 |
+
model="Meta-Llama/LlamaGuard-2-8b",
|
| 229 |
+
prompt=prompt,
|
| 230 |
+
)
|
| 231 |
+
result = response.choices[0].text
|
| 232 |
+
return result.strip() == 'safe'
|
| 233 |
+
import json
|
| 234 |
+
|