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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# murder_mystery.py
|
| 2 |
+
|
| 3 |
+
import random
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 10 |
+
client = InferenceClient(model_name)
|
| 11 |
+
|
| 12 |
+
def llm_inference(messages):
|
| 13 |
+
eos_token = "<|endoftext|>"
|
| 14 |
+
output = client.chat.completions.create(
|
| 15 |
+
messages=messages,
|
| 16 |
+
stream=False,
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
top_p=0.1,
|
| 19 |
+
max_tokens=412,
|
| 20 |
+
stop=[eos_token]
|
| 21 |
+
)
|
| 22 |
+
response = ''
|
| 23 |
+
for choice in output.choices:
|
| 24 |
+
response += choice['message']['content']
|
| 25 |
+
return response
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
suspects = {
|
| 29 |
+
"Colonel Mustard": {
|
| 30 |
+
"personality": "",
|
| 31 |
+
"knowledge": "",
|
| 32 |
+
"is_murderer": False
|
| 33 |
+
},
|
| 34 |
+
"Miss Scarlett": {
|
| 35 |
+
"personality": "",
|
| 36 |
+
"knowledge": "",
|
| 37 |
+
"is_murderer": False
|
| 38 |
+
},
|
| 39 |
+
"Professor Plum": {
|
| 40 |
+
"personality": "",
|
| 41 |
+
"knowledge": "",
|
| 42 |
+
"is_murderer": False
|
| 43 |
+
},
|
| 44 |
+
"Mrs. Peacock": {
|
| 45 |
+
"personality": "",
|
| 46 |
+
"knowledge": "",
|
| 47 |
+
"is_murderer": False
|
| 48 |
+
},
|
| 49 |
+
"Mr. Green": {
|
| 50 |
+
"personality": "",
|
| 51 |
+
"knowledge": "",
|
| 52 |
+
"is_murderer": False
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
weapons = ["Candlestick", "Dagger", "Lead Pipe", "Revolver", "Rope", "Wrench"]
|
| 57 |
+
locations = ["Kitchen", "Ballroom", "Conservatory", "Dining Room", "Library", "Study"]
|
| 58 |
+
|
| 59 |
+
# Possible personalities
|
| 60 |
+
possible_personalities = [
|
| 61 |
+
"stern and suspicious",
|
| 62 |
+
"charming but evasive",
|
| 63 |
+
"intellectual and nervous",
|
| 64 |
+
"gracious but secretive",
|
| 65 |
+
"amiable yet deceptive",
|
| 66 |
+
"hot-headed and impulsive",
|
| 67 |
+
"calm and collected",
|
| 68 |
+
"mysterious and aloof",
|
| 69 |
+
"jovial but cunning",
|
| 70 |
+
"nervous and jittery"
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
# Game state
|
| 74 |
+
game_state = {
|
| 75 |
+
"murderer": "",
|
| 76 |
+
"weapon": "",
|
| 77 |
+
"location": "",
|
| 78 |
+
"is_game_over": False,
|
| 79 |
+
"clues": [],
|
| 80 |
+
"history": []
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def initialize_game():
|
| 85 |
+
|
| 86 |
+
game_state["murderer"] = random.choice(list(suspects.keys()))
|
| 87 |
+
game_state["weapon"] = random.choice(weapons)
|
| 88 |
+
game_state["location"] = random.choice(locations)
|
| 89 |
+
game_state["is_game_over"] = False
|
| 90 |
+
game_state["clues"] = []
|
| 91 |
+
game_state["history"] = []
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
personalities_copy = possible_personalities.copy()
|
| 95 |
+
random.shuffle(personalities_copy)
|
| 96 |
+
|
| 97 |
+
for i, suspect in enumerate(suspects):
|
| 98 |
+
suspects[suspect]["is_murderer"] = (suspect == game_state["murderer"])
|
| 99 |
+
# Assign a random personality
|
| 100 |
+
suspects[suspect]["personality"] = personalities_copy[i % len(personalities_copy)]
|
| 101 |
+
# Generate knowledge
|
| 102 |
+
suspects[suspect]["knowledge"] = generate_knowledge(suspect)
|
| 103 |
+
|
| 104 |
+
print(f"Debug: Murderer is {game_state['murderer']}, Weapon is {game_state['weapon']}, Location is {game_state['location']}")
|
| 105 |
+
for suspect in suspects:
|
| 106 |
+
print(f"Debug: {suspect}'s personality is {suspects[suspect]['personality']}")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def generate_knowledge(suspect_name):
|
| 110 |
+
knowledge = []
|
| 111 |
+
if suspects[suspect_name]["is_murderer"]:
|
| 112 |
+
knowledge.append("You are the murderer. Deflect suspicion subtly.")
|
| 113 |
+
else:
|
| 114 |
+
|
| 115 |
+
known_elements = []
|
| 116 |
+
if random.choice([True, False]):
|
| 117 |
+
known_elements.append(f"The weapon is {game_state['weapon']}.")
|
| 118 |
+
if random.choice([True, False]):
|
| 119 |
+
known_elements.append(f"The location is {game_state['location']}.")
|
| 120 |
+
if random.choice([True, False]):
|
| 121 |
+
|
| 122 |
+
not_murderer = random.choice([s for s in suspects if s != suspect_name and s != game_state["murderer"]])
|
| 123 |
+
known_elements.append(f"The murderer is not {not_murderer}.")
|
| 124 |
+
if not known_elements:
|
| 125 |
+
known_elements.append("You don't have any specific knowledge about the murder.")
|
| 126 |
+
knowledge.extend(known_elements)
|
| 127 |
+
return ' '.join(knowledge)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def get_ai_response(suspect_name, player_input):
|
| 131 |
+
personality = suspects[suspect_name]["personality"]
|
| 132 |
+
knowledge = suspects[suspect_name]["knowledge"]
|
| 133 |
+
system_prompt = f"You are {suspect_name}, who is {personality}. {knowledge}"
|
| 134 |
+
user_message = f"The detective asks: \"{player_input}\" As {suspect_name}, respond in first person, staying in character."
|
| 135 |
+
messages = [
|
| 136 |
+
{"role": "system", "content": system_prompt},
|
| 137 |
+
{"role": "user", "content": user_message}
|
| 138 |
+
]
|
| 139 |
+
response = llm_inference(messages)
|
| 140 |
+
return response.strip()
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def process_input(player_input):
|
| 144 |
+
if game_state["is_game_over"]:
|
| 145 |
+
return "The game is over. Please restart to play again.", game_state["history"]
|
| 146 |
+
|
| 147 |
+
game_state["history"].append(("Detective", player_input))
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
if "accuse" in player_input.lower():
|
| 151 |
+
result = handle_accusation(player_input)
|
| 152 |
+
game_state["history"].append(("System", result))
|
| 153 |
+
return result, game_state["history"]
|
| 154 |
+
|
| 155 |
+
elif "was it" in player_input.lower() or "suggest" in player_input.lower():
|
| 156 |
+
result = handle_suggestion(player_input)
|
| 157 |
+
game_state["history"].append(("System", result))
|
| 158 |
+
return result, game_state["history"]
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
else:
|
| 162 |
+
for suspect in suspects:
|
| 163 |
+
if suspect.lower() in player_input.lower():
|
| 164 |
+
ai_response = get_ai_response(suspect, player_input)
|
| 165 |
+
game_state["history"].append((suspect, ai_response))
|
| 166 |
+
return ai_response, game_state["history"]
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
return "Please direct your question to a suspect.", game_state["history"]
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def handle_suggestion(player_input):
|
| 173 |
+
|
| 174 |
+
suspect_guess = None
|
| 175 |
+
weapon_guess = None
|
| 176 |
+
location_guess = None
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
for suspect in suspects:
|
| 180 |
+
if suspect.lower() in player_input.lower():
|
| 181 |
+
suspect_guess = suspect
|
| 182 |
+
break
|
| 183 |
+
for weapon in weapons:
|
| 184 |
+
if weapon.lower() in player_input.lower():
|
| 185 |
+
weapon_guess = weapon
|
| 186 |
+
break
|
| 187 |
+
for location in locations:
|
| 188 |
+
if location.lower() in player_input.lower():
|
| 189 |
+
location_guess = location
|
| 190 |
+
break
|
| 191 |
+
|
| 192 |
+
feedback = []
|
| 193 |
+
|
| 194 |
+
if suspect_guess == game_state["murderer"]:
|
| 195 |
+
feedback.append("The suspect is correct.")
|
| 196 |
+
elif suspect_guess:
|
| 197 |
+
feedback.append("The suspect is incorrect.")
|
| 198 |
+
|
| 199 |
+
if weapon_guess == game_state["weapon"]:
|
| 200 |
+
feedback.append("The weapon is correct.")
|
| 201 |
+
elif weapon_guess:
|
| 202 |
+
feedback.append("The weapon is incorrect.")
|
| 203 |
+
|
| 204 |
+
if location_guess == game_state["location"]:
|
| 205 |
+
feedback.append("The location is correct.")
|
| 206 |
+
elif location_guess:
|
| 207 |
+
feedback.append("The location is incorrect.")
|
| 208 |
+
|
| 209 |
+
if not feedback:
|
| 210 |
+
return "Your suggestion could not be understood. Please specify a suspect, weapon, and location."
|
| 211 |
+
|
| 212 |
+
clue = ' '.join(feedback)
|
| 213 |
+
game_state["clues"].append(clue)
|
| 214 |
+
return clue
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def handle_accusation(player_input):
|
| 218 |
+
|
| 219 |
+
suspect_guess = None
|
| 220 |
+
weapon_guess = None
|
| 221 |
+
location_guess = None
|
| 222 |
+
|
| 223 |
+
for suspect in suspects:
|
| 224 |
+
if suspect.lower() in player_input.lower():
|
| 225 |
+
suspect_guess = suspect
|
| 226 |
+
break
|
| 227 |
+
for weapon in weapons:
|
| 228 |
+
if weapon.lower() in player_input.lower():
|
| 229 |
+
weapon_guess = weapon
|
| 230 |
+
break
|
| 231 |
+
for location in locations:
|
| 232 |
+
if location.lower() in player_input.lower():
|
| 233 |
+
location_guess = location
|
| 234 |
+
break
|
| 235 |
+
|
| 236 |
+
if (suspect_guess == game_state["murderer"] and
|
| 237 |
+
weapon_guess == game_state["weapon"] and
|
| 238 |
+
location_guess == game_state["location"]):
|
| 239 |
+
game_state["is_game_over"] = True
|
| 240 |
+
return f"Congratulations! You solved the case. It was {suspect_guess} with the {weapon_guess} in the {location_guess}."
|
| 241 |
+
|
| 242 |
+
else:
|
| 243 |
+
game_state["is_game_over"] = True
|
| 244 |
+
return f"Incorrect accusation! The real murderer was {game_state['murderer']} with the {game_state['weapon']} in the {game_state['location']}."
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def chat_interface(player_input):
|
| 248 |
+
response, history = process_input(player_input)
|
| 249 |
+
chat_history = ""
|
| 250 |
+
for speaker, text in history:
|
| 251 |
+
chat_history += f"**{speaker}:** {text}\n\n"
|
| 252 |
+
clues_display = "\n".join(game_state["clues"])
|
| 253 |
+
return chat_history, clues_display
|
| 254 |
+
|
| 255 |
+
def restart_game():
|
| 256 |
+
initialize_game()
|
| 257 |
+
return "", ""
|
| 258 |
+
|
| 259 |
+
with gr.Blocks() as demo:
|
| 260 |
+
gr.Markdown("# Murder Mystery Game")
|
| 261 |
+
with gr.Row():
|
| 262 |
+
with gr.Column():
|
| 263 |
+
chatbox = gr.Textbox(lines=15, label="Chat History", interactive=False)
|
| 264 |
+
player_input = gr.Textbox(lines=1, label="Your Input")
|
| 265 |
+
send_button = gr.Button("Send")
|
| 266 |
+
restart_button = gr.Button("Restart Game")
|
| 267 |
+
with gr.Column():
|
| 268 |
+
clues = gr.Textbox(lines=15, label="Discovered Clues", interactive=False)
|
| 269 |
+
|
| 270 |
+
def send_message(player_input):
|
| 271 |
+
chat_history, clues_display = chat_interface(player_input)
|
| 272 |
+
return chat_history, "", clues_display
|
| 273 |
+
|
| 274 |
+
send_button.click(send_message, inputs=player_input, outputs=[chatbox, player_input, clues])
|
| 275 |
+
restart_button.click(fn=restart_game, inputs=None, outputs=[chatbox, clues])
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
initialize_game()
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
demo.launch()
|