ilyassh commited on
Commit
dcd204e
·
verified ·
1 Parent(s): f9082a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -53
app.py CHANGED
@@ -14,7 +14,7 @@ def llm_inference(messages):
14
  stream=False,
15
  temperature=0.7,
16
  top_p=0.1,
17
- max_tokens=412,
18
  stop=[eos_token]
19
  )
20
  response = ''
@@ -47,11 +47,16 @@ suspects = {
47
  "personality": "",
48
  "knowledge": "",
49
  "is_murderer": False
 
 
 
 
 
50
  }
51
  }
52
 
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",
@@ -63,7 +68,9 @@ possible_personalities = [
63
  "calm and collected",
64
  "mysterious and aloof",
65
  "jovial but cunning",
66
- "nervous and jittery"
 
 
67
  ]
68
 
69
  game_state = {
@@ -72,7 +79,8 @@ game_state = {
72
  "location": "",
73
  "is_game_over": False,
74
  "clues": [],
75
- "history": []
 
76
  }
77
 
78
  def initialize_game():
@@ -82,6 +90,7 @@ def initialize_game():
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):
@@ -98,22 +107,23 @@ def generate_knowledge(suspect_name):
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:
109
- known_elements.append("You don't have any specific knowledge about the murder.")
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"]
116
- system_prompt = f"You are {suspect_name}, who is {personality}. {knowledge}"
117
  user_message = f"The detective asks: \"{player_input}\" As {suspect_name}, respond in first person, staying in character."
118
  messages = [
119
  {"role": "system", "content": system_prompt},
@@ -125,57 +135,37 @@ def get_ai_response(suspect_name, player_input):
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
152
- break
153
  for weapon in weapons:
154
- if weapon.lower() in player_input.lower():
155
- weapon_guess = weapon
156
- break
157
  for location in locations:
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
@@ -200,7 +190,7 @@ def handle_accusation(player_input):
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)
@@ -219,8 +209,7 @@ with gr.Blocks() as demo:
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")
 
14
  stream=False,
15
  temperature=0.7,
16
  top_p=0.1,
17
+ max_tokens=512,
18
  stop=[eos_token]
19
  )
20
  response = ''
 
47
  "personality": "",
48
  "knowledge": "",
49
  "is_murderer": False
50
+ },
51
+ "Dr. Orchid": {
52
+ "personality": "",
53
+ "knowledge": "",
54
+ "is_murderer": False
55
  }
56
  }
57
 
58
+ weapons = ["Candlestick", "Dagger", "Lead Pipe", "Revolver", "Rope", "Wrench", "Poison"]
59
+ locations = ["Kitchen", "Ballroom", "Conservatory", "Dining Room", "Library", "Study", "Billiard Room", "Lounge"]
60
 
61
  possible_personalities = [
62
  "stern and suspicious",
 
68
  "calm and collected",
69
  "mysterious and aloof",
70
  "jovial but cunning",
71
+ "nervous and jittery",
72
+ "sarcastic and witty",
73
+ "arrogant and dismissive"
74
  ]
75
 
76
  game_state = {
 
79
  "location": "",
80
  "is_game_over": False,
81
  "clues": [],
82
+ "history": [],
83
+ "accused": False
84
  }
85
 
86
  def initialize_game():
 
90
  game_state["is_game_over"] = False
91
  game_state["clues"] = []
92
  game_state["history"] = []
93
+ game_state["accused"] = False
94
  personalities_copy = possible_personalities.copy()
95
  random.shuffle(personalities_copy)
96
  for i, suspect in enumerate(suspects):
 
107
  knowledge.append("You are the murderer. Deflect suspicion subtly.")
108
  else:
109
  known_elements = []
110
+ elements = ["weapon", "location", "murderer"]
111
+ random.shuffle(elements)
112
+ known_item = elements[0]
113
+ if known_item == "weapon":
114
+ known_elements.append(f"You know that the weapon is {game_state['weapon']}.")
115
+ elif known_item == "location":
116
+ known_elements.append(f"You know that the location is {game_state['location']}.")
117
+ elif known_item == "murderer":
118
  not_murderer = random.choice([s for s in suspects if s != suspect_name and s != game_state["murderer"]])
119
+ known_elements.append(f"You know that {not_murderer} is not the murderer.")
 
 
120
  knowledge.extend(known_elements)
121
  return ' '.join(knowledge)
122
 
123
  def get_ai_response(suspect_name, player_input):
124
  personality = suspects[suspect_name]["personality"]
125
  knowledge = suspects[suspect_name]["knowledge"]
126
+ system_prompt = f"You are {suspect_name}, who is {personality}. {knowledge} When answering questions, you may reveal hints subtly based on what you know."
127
  user_message = f"The detective asks: \"{player_input}\" As {suspect_name}, respond in first person, staying in character."
128
  messages = [
129
  {"role": "system", "content": system_prompt},
 
135
  def process_input(player_input):
136
  if game_state["is_game_over"]:
137
  return "The game is over. Please restart to play again.", game_state["history"]
138
+ if game_state["accused"]:
139
+ return "You have already made an accusation. Please restart to play again.", game_state["history"]
140
  game_state["history"].append(("Detective", player_input))
141
  if "accuse" in player_input.lower():
142
+ game_state["accused"] = True
143
  result = handle_accusation(player_input)
144
  game_state["history"].append(("System", result))
145
  return result, game_state["history"]
 
 
 
 
146
  else:
147
  for suspect in suspects:
148
  if suspect.lower() in player_input.lower():
149
  ai_response = get_ai_response(suspect, player_input)
150
  game_state["history"].append((suspect, ai_response))
151
+ extract_clues(ai_response)
152
  return ai_response, game_state["history"]
153
  return "Please direct your question to a suspect.", game_state["history"]
154
 
155
+ def extract_clues(ai_response):
156
+ clues = []
 
 
 
 
 
 
157
  for weapon in weapons:
158
+ if weapon.lower() in ai_response.lower() and weapon != game_state["weapon"]:
159
+ clues.append(f"The weapon is not the {weapon}.")
 
160
  for location in locations:
161
+ if location.lower() in ai_response.lower() and location != game_state["location"]:
162
+ clues.append(f"The location is not the {location}.")
163
+ for suspect in suspects:
164
+ if suspect.lower() in ai_response.lower() and suspect != game_state["murderer"]:
165
+ clues.append(f"{suspect} is not the murderer.")
166
+ for clue in clues:
167
+ if clue not in game_state["clues"]:
168
+ game_state["clues"].append(clue)
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  def handle_accusation(player_input):
171
  suspect_guess = None
 
190
  return f"Congratulations! You solved the case. It was {suspect_guess} with the {weapon_guess} in the {location_guess}."
191
  else:
192
  game_state["is_game_over"] = True
193
+ return f"Incorrect accusation! You lose. The real murderer was {game_state['murderer']} with the {game_state['weapon']} in the {game_state['location']}."
194
 
195
  def chat_interface(player_input):
196
  response, history = process_input(player_input)
 
209
  with gr.Row():
210
  with gr.Column():
211
  gr.Markdown("## Commands")
212
+ 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 evening?\"*")
 
213
  gr.Markdown("- **Make an accusation**: Include the word *\"accuse\"*.\n Example: *\"I accuse Colonel Mustard with the Rope in the Study.\"*")
214
  player_input = gr.Textbox(lines=1, label="Your Input")
215
  send_button = gr.Button("Send")