Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
# murder_mystery.py
|
2 |
-
|
3 |
import random
|
4 |
import gradio as gr
|
5 |
from huggingface_hub import InferenceClient
|
@@ -54,7 +52,8 @@ game_state = {
|
|
54 |
"turns": 0,
|
55 |
"searched_locations": [],
|
56 |
"eavesdropped": False,
|
57 |
-
"bluffed": False
|
|
|
58 |
}
|
59 |
|
60 |
def initialize_game():
|
@@ -69,6 +68,7 @@ def initialize_game():
|
|
69 |
game_state["searched_locations"] = []
|
70 |
game_state["eavesdropped"] = False
|
71 |
game_state["bluffed"] = False
|
|
|
72 |
random.shuffle(possible_personalities)
|
73 |
alibi_locations = random.sample(locations, len(suspect_names))
|
74 |
motives = ["inheritance", "jealousy", "revenge", "secret affair", "business rivalry", "blackmail"]
|
@@ -86,7 +86,9 @@ def initialize_game():
|
|
86 |
"alibi_with": [],
|
87 |
"knowledge": {},
|
88 |
"motive": random.choice(motives),
|
89 |
-
"relationships": {}
|
|
|
|
|
90 |
}
|
91 |
suspects[suspect_name] = suspect
|
92 |
for suspect in suspects.values():
|
@@ -96,19 +98,20 @@ def initialize_game():
|
|
96 |
other_suspects = [s["name"] for s in suspects.values() if s["name"] != suspect["name"]]
|
97 |
for other in other_suspects:
|
98 |
suspect["relationships"][other] = random.choice(relationships)
|
99 |
-
|
|
|
100 |
suspect["knowledge"]["others_locations"] = generate_others_locations_knowledge(suspect)
|
101 |
for suspect in suspects.values():
|
102 |
suspect["knowledge"]["self"] = generate_knowledge(suspect)
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
|
107 |
def generate_knowledge(suspect):
|
108 |
-
details = f"You have a motive of {suspect['motive']}."
|
109 |
relationship_details = ". ".join([f"You are {relation} with {other}" for other, relation in suspect["relationships"].items()])
|
110 |
if suspect["is_murderer"]:
|
111 |
-
# Murderer lies about their alibi
|
112 |
suspect["alibi_location"] = random.choice([loc for loc in locations if loc != game_state["location"]])
|
113 |
suspect["alibi_with"] = []
|
114 |
knowledge = f"You are the murderer. Lie about your alibi and deflect suspicion. {details} {relationship_details}"
|
@@ -127,13 +130,10 @@ def generate_others_locations_knowledge(suspect):
|
|
127 |
continue
|
128 |
chance = random.random()
|
129 |
if chance < 0.5:
|
130 |
-
# They know where the other was
|
131 |
knowledge[other["name"]] = f"was in the {other['alibi_location']}."
|
132 |
elif chance < 0.75:
|
133 |
-
# They are unsure
|
134 |
knowledge[other["name"]] = "you are not sure where they were."
|
135 |
else:
|
136 |
-
# They don't know
|
137 |
knowledge[other["name"]] = "you have no idea where they were."
|
138 |
return knowledge
|
139 |
|
@@ -147,6 +147,7 @@ def analyze_tone(player_input):
|
|
147 |
def get_ai_response(suspect_name, player_input):
|
148 |
suspect = suspects[suspect_name]
|
149 |
game_state["turns"] += 1
|
|
|
150 |
tone = analyze_tone(player_input)
|
151 |
if "accuse" in player_input.lower() or any(word in player_input.lower() for word in ["murderer", "kill", "guilty"]):
|
152 |
suspect["trust_level"] -= 1
|
@@ -158,16 +159,20 @@ def get_ai_response(suspect_name, player_input):
|
|
158 |
suspect["trust_level"] -= 1
|
159 |
else:
|
160 |
suspect["anger_level"] += 1
|
|
|
|
|
161 |
personality = suspect["personality"]
|
162 |
knowledge = suspect["knowledge"]["self"]
|
163 |
others_knowledge = suspect["knowledge"]["others_locations"]
|
164 |
anger_level = suspect["anger_level"]
|
165 |
hot_headed = suspect["hot_headed"]
|
166 |
trust_level = suspect["trust_level"]
|
|
|
|
|
167 |
system_prompt = f"You are {suspect_name}, who is {personality}. {knowledge}"
|
168 |
-
# Include knowledge about others
|
169 |
for other_name, info in others_knowledge.items():
|
170 |
system_prompt += f" You know that {other_name} {info}"
|
|
|
171 |
if hot_headed and anger_level >= suspect["anger_threshold"]:
|
172 |
system_prompt += f" You are extremely angry and may respond aggressively."
|
173 |
if suspect["is_murderer"]:
|
@@ -176,7 +181,8 @@ def get_ai_response(suspect_name, player_input):
|
|
176 |
system_prompt += " You trust the detective and are willing to share information."
|
177 |
elif trust_level <= 3:
|
178 |
system_prompt += " You do not trust the detective and may withhold information."
|
179 |
-
|
|
|
180 |
messages = [
|
181 |
{"role": "system", "content": system_prompt},
|
182 |
{"role": "user", "content": user_message}
|
@@ -274,7 +280,7 @@ def eavesdrop():
|
|
274 |
game_state["eavesdropped"] = True
|
275 |
game_state["turns"] += 1
|
276 |
suspect1, suspect2 = random.sample(suspect_names, 2)
|
277 |
-
conversation = f"{suspect1} whispers to {suspect2}: 'I
|
278 |
extract_clues(conversation)
|
279 |
return f"You overhear a conversation: {conversation}"
|
280 |
|
@@ -295,7 +301,6 @@ def bluff(player_input):
|
|
295 |
|
296 |
def analyze_response():
|
297 |
game_state["turns"] += 1
|
298 |
-
# For simplicity, we'll simulate the outcome
|
299 |
success = random.choice([True, False])
|
300 |
if success:
|
301 |
return "You successfully detect that the suspect is lying!"
|
@@ -366,7 +371,9 @@ def get_debug_info():
|
|
366 |
info += f"Alibi Location: {suspect['alibi_location']}\n"
|
367 |
info += f"Alibi With: {', '.join(suspect['alibi_with'])}\n"
|
368 |
info += f"Motive: {suspect['motive']}\n"
|
|
|
369 |
info += f"Relationships: {suspect['relationships']}\n"
|
|
|
370 |
info += f"Knowledge: {suspect['knowledge']}\n\n"
|
371 |
debug_info += info
|
372 |
return debug_info
|
@@ -382,14 +389,14 @@ with gr.Blocks() as demo:
|
|
382 |
with gr.Row():
|
383 |
with gr.Column():
|
384 |
gr.Markdown("## Commands")
|
385 |
-
gr.Markdown("- **Ask a question to a suspect**: Include the suspect's name in your question
|
386 |
-
gr.Markdown("- **Interrogate multiple suspects**: Include multiple suspects' names in your question
|
387 |
-
gr.Markdown("- **Search a location**: Type *\"Search [Location]\"
|
388 |
gr.Markdown("- **Eavesdrop on suspects**: Type *\"Eavesdrop\"*")
|
389 |
gr.Markdown("- **Bluff a suspect**: Type *\"Bluff [Suspect]\"*")
|
390 |
gr.Markdown("- **Analyze a response**: Type *\"Analyze\"*")
|
391 |
gr.Markdown("- **Reveal your deductions**: Type *\"Reveal: [Your deductions]\"*")
|
392 |
-
gr.Markdown("- **Make an accusation**: Include the word *\"accuse\"
|
393 |
player_input = gr.Textbox(lines=1, label="Your Input")
|
394 |
send_button = gr.Button("Send")
|
395 |
restart_button = gr.Button("Restart Game")
|
|
|
|
|
|
|
1 |
import random
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
|
|
52 |
"turns": 0,
|
53 |
"searched_locations": [],
|
54 |
"eavesdropped": False,
|
55 |
+
"bluffed": False,
|
56 |
+
"player_questions": []
|
57 |
}
|
58 |
|
59 |
def initialize_game():
|
|
|
68 |
game_state["searched_locations"] = []
|
69 |
game_state["eavesdropped"] = False
|
70 |
game_state["bluffed"] = False
|
71 |
+
game_state["player_questions"] = []
|
72 |
random.shuffle(possible_personalities)
|
73 |
alibi_locations = random.sample(locations, len(suspect_names))
|
74 |
motives = ["inheritance", "jealousy", "revenge", "secret affair", "business rivalry", "blackmail"]
|
|
|
86 |
"alibi_with": [],
|
87 |
"knowledge": {},
|
88 |
"motive": random.choice(motives),
|
89 |
+
"relationships": {},
|
90 |
+
"backstory": "",
|
91 |
+
"suspects": None
|
92 |
}
|
93 |
suspects[suspect_name] = suspect
|
94 |
for suspect in suspects.values():
|
|
|
98 |
other_suspects = [s["name"] for s in suspects.values() if s["name"] != suspect["name"]]
|
99 |
for other in other_suspects:
|
100 |
suspect["relationships"][other] = random.choice(relationships)
|
101 |
+
suspect["suspects"] = random.choice([s for s in other_suspects if s not in suspect["alibi_with"]])
|
102 |
+
suspect["backstory"] = generate_backstory(suspect)
|
103 |
suspect["knowledge"]["others_locations"] = generate_others_locations_knowledge(suspect)
|
104 |
for suspect in suspects.values():
|
105 |
suspect["knowledge"]["self"] = generate_knowledge(suspect)
|
106 |
+
|
107 |
+
def generate_backstory(suspect):
|
108 |
+
backstory = f"{suspect['name']} has a backstory involving {suspect['motive']}."
|
109 |
+
return backstory
|
110 |
|
111 |
def generate_knowledge(suspect):
|
112 |
+
details = f"You have a motive of {suspect['motive']}. {suspect['backstory']}"
|
113 |
relationship_details = ". ".join([f"You are {relation} with {other}" for other, relation in suspect["relationships"].items()])
|
114 |
if suspect["is_murderer"]:
|
|
|
115 |
suspect["alibi_location"] = random.choice([loc for loc in locations if loc != game_state["location"]])
|
116 |
suspect["alibi_with"] = []
|
117 |
knowledge = f"You are the murderer. Lie about your alibi and deflect suspicion. {details} {relationship_details}"
|
|
|
130 |
continue
|
131 |
chance = random.random()
|
132 |
if chance < 0.5:
|
|
|
133 |
knowledge[other["name"]] = f"was in the {other['alibi_location']}."
|
134 |
elif chance < 0.75:
|
|
|
135 |
knowledge[other["name"]] = "you are not sure where they were."
|
136 |
else:
|
|
|
137 |
knowledge[other["name"]] = "you have no idea where they were."
|
138 |
return knowledge
|
139 |
|
|
|
147 |
def get_ai_response(suspect_name, player_input):
|
148 |
suspect = suspects[suspect_name]
|
149 |
game_state["turns"] += 1
|
150 |
+
game_state["player_questions"].append(player_input)
|
151 |
tone = analyze_tone(player_input)
|
152 |
if "accuse" in player_input.lower() or any(word in player_input.lower() for word in ["murderer", "kill", "guilty"]):
|
153 |
suspect["trust_level"] -= 1
|
|
|
159 |
suspect["trust_level"] -= 1
|
160 |
else:
|
161 |
suspect["anger_level"] += 1
|
162 |
+
if suspect["anger_level"] >= suspect["anger_threshold"]:
|
163 |
+
suspect["personality"] = "agitated and defensive"
|
164 |
personality = suspect["personality"]
|
165 |
knowledge = suspect["knowledge"]["self"]
|
166 |
others_knowledge = suspect["knowledge"]["others_locations"]
|
167 |
anger_level = suspect["anger_level"]
|
168 |
hot_headed = suspect["hot_headed"]
|
169 |
trust_level = suspect["trust_level"]
|
170 |
+
previous_questions = " ".join(game_state["player_questions"][-3:])
|
171 |
+
clues_found = " ".join(game_state["clues"])
|
172 |
system_prompt = f"You are {suspect_name}, who is {personality}. {knowledge}"
|
|
|
173 |
for other_name, info in others_knowledge.items():
|
174 |
system_prompt += f" You know that {other_name} {info}"
|
175 |
+
system_prompt += f" You suspect that {suspect['suspects']} might be involved due to {suspect['motive']}."
|
176 |
if hot_headed and anger_level >= suspect["anger_threshold"]:
|
177 |
system_prompt += f" You are extremely angry and may respond aggressively."
|
178 |
if suspect["is_murderer"]:
|
|
|
181 |
system_prompt += " You trust the detective and are willing to share information."
|
182 |
elif trust_level <= 3:
|
183 |
system_prompt += " You do not trust the detective and may withhold information."
|
184 |
+
system_prompt += f" Previously, the detective asked: {previous_questions}"
|
185 |
+
user_message = f"The detective asks: \"{player_input}\" As {suspect_name}, respond in first person, staying in character. Provide a detailed response, and consider any previous interactions. If you lie, include subtle hints like hesitations or contradictions."
|
186 |
messages = [
|
187 |
{"role": "system", "content": system_prompt},
|
188 |
{"role": "user", "content": user_message}
|
|
|
280 |
game_state["eavesdropped"] = True
|
281 |
game_state["turns"] += 1
|
282 |
suspect1, suspect2 = random.sample(suspect_names, 2)
|
283 |
+
conversation = f"{suspect1} whispers to {suspect2}: 'I think {suspects[suspect1]['suspects']} might be involved. They had a motive because of {suspects[suspects[suspect1]['suspects']]['motive']}.'"
|
284 |
extract_clues(conversation)
|
285 |
return f"You overhear a conversation: {conversation}"
|
286 |
|
|
|
301 |
|
302 |
def analyze_response():
|
303 |
game_state["turns"] += 1
|
|
|
304 |
success = random.choice([True, False])
|
305 |
if success:
|
306 |
return "You successfully detect that the suspect is lying!"
|
|
|
371 |
info += f"Alibi Location: {suspect['alibi_location']}\n"
|
372 |
info += f"Alibi With: {', '.join(suspect['alibi_with'])}\n"
|
373 |
info += f"Motive: {suspect['motive']}\n"
|
374 |
+
info += f"Suspects: {suspect['suspects']}\n"
|
375 |
info += f"Relationships: {suspect['relationships']}\n"
|
376 |
+
info += f"Backstory: {suspect['backstory']}\n"
|
377 |
info += f"Knowledge: {suspect['knowledge']}\n\n"
|
378 |
debug_info += info
|
379 |
return debug_info
|
|
|
389 |
with gr.Row():
|
390 |
with gr.Column():
|
391 |
gr.Markdown("## Commands")
|
392 |
+
gr.Markdown("- **Ask a question to a suspect**: Include the suspect's name in your question.")
|
393 |
+
gr.Markdown("- **Interrogate multiple suspects**: Include multiple suspects' names in your question.")
|
394 |
+
gr.Markdown("- **Search a location**: Type *\"Search [Location]\"*.")
|
395 |
gr.Markdown("- **Eavesdrop on suspects**: Type *\"Eavesdrop\"*")
|
396 |
gr.Markdown("- **Bluff a suspect**: Type *\"Bluff [Suspect]\"*")
|
397 |
gr.Markdown("- **Analyze a response**: Type *\"Analyze\"*")
|
398 |
gr.Markdown("- **Reveal your deductions**: Type *\"Reveal: [Your deductions]\"*")
|
399 |
+
gr.Markdown("- **Make an accusation**: Include the word *\"accuse\"*.")
|
400 |
player_input = gr.Textbox(lines=1, label="Your Input")
|
401 |
send_button = gr.Button("Send")
|
402 |
restart_button = gr.Button("Restart Game")
|