Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,6 @@
|
|
| 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)
|
|
@@ -24,7 +22,6 @@ def llm_inference(messages):
|
|
| 24 |
response += choice['message']['content']
|
| 25 |
return response
|
| 26 |
|
| 27 |
-
|
| 28 |
suspects = {
|
| 29 |
"Colonel Mustard": {
|
| 30 |
"personality": "",
|
|
@@ -56,7 +53,6 @@ suspects = {
|
|
| 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",
|
|
@@ -70,7 +66,6 @@ possible_personalities = [
|
|
| 70 |
"nervous and jittery"
|
| 71 |
]
|
| 72 |
|
| 73 |
-
# Game state
|
| 74 |
game_state = {
|
| 75 |
"murderer": "",
|
| 76 |
"weapon": "",
|
|
@@ -80,45 +75,34 @@ game_state = {
|
|
| 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:
|
|
@@ -126,7 +110,6 @@ def generate_knowledge(suspect_name):
|
|
| 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"]
|
|
@@ -139,43 +122,30 @@ def get_ai_response(suspect_name, player_input):
|
|
| 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
|
|
@@ -188,38 +158,29 @@ def handle_suggestion(player_input):
|
|
| 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
|
|
@@ -232,18 +193,15 @@ def handle_accusation(player_input):
|
|
| 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 = ""
|
|
@@ -260,11 +218,21 @@ with gr.Blocks() as demo:
|
|
| 260 |
gr.Markdown("# Murder Mystery Game")
|
| 261 |
with gr.Row():
|
| 262 |
with gr.Column():
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
| 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):
|
|
@@ -274,8 +242,5 @@ with gr.Blocks() as demo:
|
|
| 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()
|
|
|
|
| 3 |
import random
|
| 4 |
import gradio as gr
|
| 5 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 6 |
|
| 7 |
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 8 |
client = InferenceClient(model_name)
|
|
|
|
| 22 |
response += choice['message']['content']
|
| 23 |
return response
|
| 24 |
|
|
|
|
| 25 |
suspects = {
|
| 26 |
"Colonel Mustard": {
|
| 27 |
"personality": "",
|
|
|
|
| 53 |
weapons = ["Candlestick", "Dagger", "Lead Pipe", "Revolver", "Rope", "Wrench"]
|
| 54 |
locations = ["Kitchen", "Ballroom", "Conservatory", "Dining Room", "Library", "Study"]
|
| 55 |
|
|
|
|
| 56 |
possible_personalities = [
|
| 57 |
"stern and suspicious",
|
| 58 |
"charming but evasive",
|
|
|
|
| 66 |
"nervous and jittery"
|
| 67 |
]
|
| 68 |
|
|
|
|
| 69 |
game_state = {
|
| 70 |
"murderer": "",
|
| 71 |
"weapon": "",
|
|
|
|
| 75 |
"history": []
|
| 76 |
}
|
| 77 |
|
|
|
|
| 78 |
def initialize_game():
|
|
|
|
| 79 |
game_state["murderer"] = random.choice(list(suspects.keys()))
|
| 80 |
game_state["weapon"] = random.choice(weapons)
|
| 81 |
game_state["location"] = random.choice(locations)
|
| 82 |
game_state["is_game_over"] = False
|
| 83 |
game_state["clues"] = []
|
| 84 |
game_state["history"] = []
|
|
|
|
|
|
|
| 85 |
personalities_copy = possible_personalities.copy()
|
| 86 |
random.shuffle(personalities_copy)
|
|
|
|
| 87 |
for i, suspect in enumerate(suspects):
|
| 88 |
suspects[suspect]["is_murderer"] = (suspect == game_state["murderer"])
|
|
|
|
| 89 |
suspects[suspect]["personality"] = personalities_copy[i % len(personalities_copy)]
|
|
|
|
| 90 |
suspects[suspect]["knowledge"] = generate_knowledge(suspect)
|
|
|
|
| 91 |
print(f"Debug: Murderer is {game_state['murderer']}, Weapon is {game_state['weapon']}, Location is {game_state['location']}")
|
| 92 |
for suspect in suspects:
|
| 93 |
print(f"Debug: {suspect}'s personality is {suspects[suspect]['personality']}")
|
| 94 |
|
|
|
|
| 95 |
def generate_knowledge(suspect_name):
|
| 96 |
knowledge = []
|
| 97 |
if suspects[suspect_name]["is_murderer"]:
|
| 98 |
knowledge.append("You are the murderer. Deflect suspicion subtly.")
|
| 99 |
else:
|
|
|
|
| 100 |
known_elements = []
|
| 101 |
if random.choice([True, False]):
|
| 102 |
known_elements.append(f"The weapon is {game_state['weapon']}.")
|
| 103 |
if random.choice([True, False]):
|
| 104 |
known_elements.append(f"The location is {game_state['location']}.")
|
| 105 |
if random.choice([True, False]):
|
|
|
|
| 106 |
not_murderer = random.choice([s for s in suspects if s != suspect_name and s != game_state["murderer"]])
|
| 107 |
known_elements.append(f"The murderer is not {not_murderer}.")
|
| 108 |
if not known_elements:
|
|
|
|
| 110 |
knowledge.extend(known_elements)
|
| 111 |
return ' '.join(knowledge)
|
| 112 |
|
|
|
|
| 113 |
def get_ai_response(suspect_name, player_input):
|
| 114 |
personality = suspects[suspect_name]["personality"]
|
| 115 |
knowledge = suspects[suspect_name]["knowledge"]
|
|
|
|
| 122 |
response = llm_inference(messages)
|
| 123 |
return response.strip()
|
| 124 |
|
|
|
|
| 125 |
def process_input(player_input):
|
| 126 |
if game_state["is_game_over"]:
|
| 127 |
return "The game is over. Please restart to play again.", game_state["history"]
|
|
|
|
| 128 |
game_state["history"].append(("Detective", player_input))
|
|
|
|
|
|
|
| 129 |
if "accuse" in player_input.lower():
|
| 130 |
result = handle_accusation(player_input)
|
| 131 |
game_state["history"].append(("System", result))
|
| 132 |
return result, game_state["history"]
|
|
|
|
| 133 |
elif "was it" in player_input.lower() or "suggest" in player_input.lower():
|
| 134 |
result = handle_suggestion(player_input)
|
| 135 |
game_state["history"].append(("System", result))
|
| 136 |
return result, game_state["history"]
|
|
|
|
|
|
|
| 137 |
else:
|
| 138 |
for suspect in suspects:
|
| 139 |
if suspect.lower() in player_input.lower():
|
| 140 |
ai_response = get_ai_response(suspect, player_input)
|
| 141 |
game_state["history"].append((suspect, ai_response))
|
| 142 |
return ai_response, game_state["history"]
|
|
|
|
|
|
|
| 143 |
return "Please direct your question to a suspect.", game_state["history"]
|
| 144 |
|
|
|
|
| 145 |
def handle_suggestion(player_input):
|
|
|
|
| 146 |
suspect_guess = None
|
| 147 |
weapon_guess = None
|
| 148 |
location_guess = None
|
|
|
|
|
|
|
| 149 |
for suspect in suspects:
|
| 150 |
if suspect.lower() in player_input.lower():
|
| 151 |
suspect_guess = suspect
|
|
|
|
| 158 |
if location.lower() in player_input.lower():
|
| 159 |
location_guess = location
|
| 160 |
break
|
|
|
|
| 161 |
feedback = []
|
|
|
|
| 162 |
if suspect_guess == game_state["murderer"]:
|
| 163 |
feedback.append("The suspect is correct.")
|
| 164 |
elif suspect_guess:
|
| 165 |
feedback.append("The suspect is incorrect.")
|
|
|
|
| 166 |
if weapon_guess == game_state["weapon"]:
|
| 167 |
feedback.append("The weapon is correct.")
|
| 168 |
elif weapon_guess:
|
| 169 |
feedback.append("The weapon is incorrect.")
|
|
|
|
| 170 |
if location_guess == game_state["location"]:
|
| 171 |
feedback.append("The location is correct.")
|
| 172 |
elif location_guess:
|
| 173 |
feedback.append("The location is incorrect.")
|
|
|
|
| 174 |
if not feedback:
|
| 175 |
return "Your suggestion could not be understood. Please specify a suspect, weapon, and location."
|
|
|
|
| 176 |
clue = ' '.join(feedback)
|
| 177 |
game_state["clues"].append(clue)
|
| 178 |
return clue
|
| 179 |
|
|
|
|
| 180 |
def handle_accusation(player_input):
|
|
|
|
| 181 |
suspect_guess = None
|
| 182 |
weapon_guess = None
|
| 183 |
location_guess = None
|
|
|
|
| 184 |
for suspect in suspects:
|
| 185 |
if suspect.lower() in player_input.lower():
|
| 186 |
suspect_guess = suspect
|
|
|
|
| 193 |
if location.lower() in player_input.lower():
|
| 194 |
location_guess = location
|
| 195 |
break
|
|
|
|
| 196 |
if (suspect_guess == game_state["murderer"] and
|
| 197 |
weapon_guess == game_state["weapon"] and
|
| 198 |
location_guess == game_state["location"]):
|
| 199 |
game_state["is_game_over"] = True
|
| 200 |
return f"Congratulations! You solved the case. It was {suspect_guess} with the {weapon_guess} in the {location_guess}."
|
|
|
|
| 201 |
else:
|
| 202 |
game_state["is_game_over"] = True
|
| 203 |
return f"Incorrect accusation! The real murderer was {game_state['murderer']} with the {game_state['weapon']} in the {game_state['location']}."
|
| 204 |
|
|
|
|
| 205 |
def chat_interface(player_input):
|
| 206 |
response, history = process_input(player_input)
|
| 207 |
chat_history = ""
|
|
|
|
| 218 |
gr.Markdown("# Murder Mystery Game")
|
| 219 |
with gr.Row():
|
| 220 |
with gr.Column():
|
| 221 |
+
gr.Markdown("## Commands")
|
| 222 |
+
gr.Markdown("- **Ask a question to a suspect**: Include the suspect's name in your question.\n Example: *\"Miss Scarlett, where were you during the murder?\"*")
|
| 223 |
+
gr.Markdown("- **Make a suggestion**: Use phrases like *\"Was it [Suspect] with the [Weapon] in the [Location]?\"*\n Example: *\"Was it Professor Plum with the Dagger in the Library?\"*")
|
| 224 |
+
gr.Markdown("- **Make an accusation**: Include the word *\"accuse\"*.\n Example: *\"I accuse Colonel Mustard with the Rope in the Study.\"*")
|
| 225 |
player_input = gr.Textbox(lines=1, label="Your Input")
|
| 226 |
send_button = gr.Button("Send")
|
| 227 |
restart_button = gr.Button("Restart Game")
|
| 228 |
+
chatbox = gr.Textbox(lines=15, label="Chat History", interactive=False)
|
| 229 |
with gr.Column():
|
| 230 |
+
gr.Markdown("## Suspects")
|
| 231 |
+
gr.Markdown(', '.join(suspects.keys()))
|
| 232 |
+
gr.Markdown("## Weapons")
|
| 233 |
+
gr.Markdown(', '.join(weapons))
|
| 234 |
+
gr.Markdown("## Locations")
|
| 235 |
+
gr.Markdown(', '.join(locations))
|
| 236 |
clues = gr.Textbox(lines=15, label="Discovered Clues", interactive=False)
|
| 237 |
|
| 238 |
def send_message(player_input):
|
|
|
|
| 242 |
send_button.click(send_message, inputs=player_input, outputs=[chatbox, player_input, clues])
|
| 243 |
restart_button.click(fn=restart_game, inputs=None, outputs=[chatbox, clues])
|
| 244 |
|
|
|
|
| 245 |
initialize_game()
|
|
|
|
|
|
|
| 246 |
demo.launch()
|