Spaces:
Sleeping
Sleeping
loic.ledreck
commited on
Commit
·
78484fd
1
Parent(s):
eb3a7ce
feat: trump sends first message
Browse files- app.py +12 -7
- helper_functions.py +20 -10
- static/index.html +50 -6
app.py
CHANGED
|
@@ -50,7 +50,7 @@ async def get_chat_history():
|
|
| 50 |
game_number = len(os.listdir('games/'))
|
| 51 |
|
| 52 |
chat_history = load_chat_history(f'games/game_{game_number}')
|
| 53 |
-
|
| 54 |
return {"chat_history": chat_history}
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=str(e))
|
|
@@ -70,10 +70,9 @@ async def send_message(message: Message):
|
|
| 70 |
|
| 71 |
# Load existing chat history
|
| 72 |
chat_history = load_chat_history(f'games/game_{game_number}')
|
| 73 |
-
|
| 74 |
-
|
| 75 |
#If we're at the beginning of a round
|
| 76 |
-
if
|
| 77 |
idea, concern, advisor_full, events, consequences = generate_round_context(game_number)
|
| 78 |
round_context = {
|
| 79 |
"idea": idea,
|
|
@@ -101,7 +100,10 @@ async def send_message(message: Message):
|
|
| 101 |
|
| 102 |
# Add user message to history
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
# Format the prompt
|
| 106 |
formatted_prompt = instruction_prompt.format(
|
| 107 |
hints=hints,
|
|
@@ -123,9 +125,10 @@ async def send_message(message: Message):
|
|
| 123 |
"user": "user",
|
| 124 |
"trump": "assistant" # Mapping 'trump' to 'assistant'
|
| 125 |
}
|
|
|
|
| 126 |
for interaction in chat_history:
|
| 127 |
for key, value in interaction.items():
|
| 128 |
-
user_message = value['user']['message']
|
| 129 |
trump_message = value['trump']['message'] if value['trump'] else None
|
| 130 |
|
| 131 |
dynamic_history.append({
|
|
@@ -142,12 +145,14 @@ async def send_message(message: Message):
|
|
| 142 |
|
| 143 |
messages = system + dynamic_history
|
| 144 |
|
|
|
|
|
|
|
| 145 |
chat_response = client.chat.complete(
|
| 146 |
model=model,
|
| 147 |
messages=messages
|
| 148 |
)
|
| 149 |
|
| 150 |
-
trump_response = chat_response.choices[0].message.content
|
| 151 |
|
| 152 |
# Add Trump's response to history
|
| 153 |
chat_history = update_chat_history(chat_history, trump_message=trump_response)
|
|
|
|
| 50 |
game_number = len(os.listdir('games/'))
|
| 51 |
|
| 52 |
chat_history = load_chat_history(f'games/game_{game_number}')
|
| 53 |
+
|
| 54 |
return {"chat_history": chat_history}
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 70 |
|
| 71 |
# Load existing chat history
|
| 72 |
chat_history = load_chat_history(f'games/game_{game_number}')
|
| 73 |
+
|
|
|
|
| 74 |
#If we're at the beginning of a round
|
| 75 |
+
if message.message == "":
|
| 76 |
idea, concern, advisor_full, events, consequences = generate_round_context(game_number)
|
| 77 |
round_context = {
|
| 78 |
"idea": idea,
|
|
|
|
| 100 |
|
| 101 |
# Add user message to history
|
| 102 |
|
| 103 |
+
if message.message != "":
|
| 104 |
+
chat_history = update_chat_history(chat_history, user_message=message.message)
|
| 105 |
+
else:
|
| 106 |
+
chat_history = []
|
| 107 |
# Format the prompt
|
| 108 |
formatted_prompt = instruction_prompt.format(
|
| 109 |
hints=hints,
|
|
|
|
| 125 |
"user": "user",
|
| 126 |
"trump": "assistant" # Mapping 'trump' to 'assistant'
|
| 127 |
}
|
| 128 |
+
print(chat_history)
|
| 129 |
for interaction in chat_history:
|
| 130 |
for key, value in interaction.items():
|
| 131 |
+
user_message = value['user']['message'] if 'user' in value else "..."
|
| 132 |
trump_message = value['trump']['message'] if value['trump'] else None
|
| 133 |
|
| 134 |
dynamic_history.append({
|
|
|
|
| 145 |
|
| 146 |
messages = system + dynamic_history
|
| 147 |
|
| 148 |
+
print("messages", messages)
|
| 149 |
+
print("model", model)
|
| 150 |
chat_response = client.chat.complete(
|
| 151 |
model=model,
|
| 152 |
messages=messages
|
| 153 |
)
|
| 154 |
|
| 155 |
+
trump_response = chat_response.choices[0].message.content
|
| 156 |
|
| 157 |
# Add Trump's response to history
|
| 158 |
chat_history = update_chat_history(chat_history, trump_message=trump_response)
|
helper_functions.py
CHANGED
|
@@ -6,7 +6,7 @@ from random import choice
|
|
| 6 |
def load_character_data():
|
| 7 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
json_path = os.path.join(current_dir, 'original_setup/trump.character.json')
|
| 9 |
-
|
| 10 |
with open(json_path, 'r',encoding='utf-8') as file:
|
| 11 |
return json.load(file)
|
| 12 |
|
|
@@ -21,7 +21,7 @@ def load_chat_history(game_root):
|
|
| 21 |
def update_chat_history(chat_history, user_message=None, trump_message=None):
|
| 22 |
# If this is a new interaction, create a new interaction number
|
| 23 |
interaction_number = len(chat_history) + 1
|
| 24 |
-
|
| 25 |
# If we're starting a new interaction with a user message
|
| 26 |
if user_message and not trump_message:
|
| 27 |
interaction_key = f"interaction_{interaction_number}"
|
|
@@ -32,19 +32,29 @@ def update_chat_history(chat_history, user_message=None, trump_message=None):
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
chat_history.append(new_interaction)
|
| 35 |
-
|
| 36 |
# If we're adding Trump's response to an existing interaction
|
| 37 |
-
elif trump_message:
|
| 38 |
# Get the last interaction number (current one)
|
| 39 |
interaction_key = f"interaction_{len(chat_history)}"
|
| 40 |
current_interaction = chat_history[-1][interaction_key]
|
| 41 |
current_interaction["trump"] = {"role": "Trump", "message": trump_message}
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return chat_history
|
| 44 |
|
| 45 |
def save_chat_history(history, game_root):
|
| 46 |
history_path = os.path.join(game_root, 'chat_history.json')
|
| 47 |
-
|
| 48 |
with open(history_path, 'w',encoding='utf-8') as file:
|
| 49 |
json.dump(history, file, indent=2)
|
| 50 |
|
|
@@ -114,11 +124,11 @@ def process_ending(idea_is_accepted, game_number, idea):
|
|
| 114 |
|
| 115 |
with open(f'games/game_{game_number}/round_consequences.json', 'r',encoding='utf-8') as f:
|
| 116 |
consequences = json.load(f)
|
| 117 |
-
country = consequences['country']
|
| 118 |
delta_USA = int(consequences['delta_USA'])
|
| 119 |
delta_country = int(consequences['delta_country'])
|
| 120 |
-
delta_friendliness = int(consequences['delta_friendliness'])
|
| 121 |
-
|
| 122 |
GDP = world_graph.update_world(country, delta_USA, delta_country, delta_friendliness, game_number)
|
| 123 |
|
| 124 |
-
return GDP
|
|
|
|
| 6 |
def load_character_data():
|
| 7 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
json_path = os.path.join(current_dir, 'original_setup/trump.character.json')
|
| 9 |
+
|
| 10 |
with open(json_path, 'r',encoding='utf-8') as file:
|
| 11 |
return json.load(file)
|
| 12 |
|
|
|
|
| 21 |
def update_chat_history(chat_history, user_message=None, trump_message=None):
|
| 22 |
# If this is a new interaction, create a new interaction number
|
| 23 |
interaction_number = len(chat_history) + 1
|
| 24 |
+
|
| 25 |
# If we're starting a new interaction with a user message
|
| 26 |
if user_message and not trump_message:
|
| 27 |
interaction_key = f"interaction_{interaction_number}"
|
|
|
|
| 32 |
}
|
| 33 |
}
|
| 34 |
chat_history.append(new_interaction)
|
| 35 |
+
|
| 36 |
# If we're adding Trump's response to an existing interaction
|
| 37 |
+
elif trump_message and chat_history:
|
| 38 |
# Get the last interaction number (current one)
|
| 39 |
interaction_key = f"interaction_{len(chat_history)}"
|
| 40 |
current_interaction = chat_history[-1][interaction_key]
|
| 41 |
current_interaction["trump"] = {"role": "Trump", "message": trump_message}
|
| 42 |
+
# Init
|
| 43 |
+
elif not chat_history and trump_message and not user_message:
|
| 44 |
+
interaction_key = f"interaction_{interaction_number}"
|
| 45 |
+
new_interaction = {
|
| 46 |
+
interaction_key: {
|
| 47 |
+
"trump": {"role": "Trump", "message": trump_message},
|
| 48 |
+
"user": {"role": "user", "message": "..."},
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
chat_history.append(new_interaction)
|
| 52 |
+
|
| 53 |
return chat_history
|
| 54 |
|
| 55 |
def save_chat_history(history, game_root):
|
| 56 |
history_path = os.path.join(game_root, 'chat_history.json')
|
| 57 |
+
|
| 58 |
with open(history_path, 'w',encoding='utf-8') as file:
|
| 59 |
json.dump(history, file, indent=2)
|
| 60 |
|
|
|
|
| 124 |
|
| 125 |
with open(f'games/game_{game_number}/round_consequences.json', 'r',encoding='utf-8') as f:
|
| 126 |
consequences = json.load(f)
|
| 127 |
+
country = consequences['country']
|
| 128 |
delta_USA = int(consequences['delta_USA'])
|
| 129 |
delta_country = int(consequences['delta_country'])
|
| 130 |
+
delta_friendliness = int(consequences['delta_friendliness'])
|
| 131 |
+
|
| 132 |
GDP = world_graph.update_world(country, delta_USA, delta_country, delta_friendliness, game_number)
|
| 133 |
|
| 134 |
+
return GDP
|
static/index.html
CHANGED
|
@@ -157,6 +157,53 @@
|
|
| 157 |
addMessageToChat('Error: Failed to get response', false);
|
| 158 |
});
|
| 159 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
// Trigger on button click
|
| 162 |
document.getElementById('sendButton').addEventListener('click', sendMessage);
|
|
@@ -210,6 +257,7 @@
|
|
| 210 |
});
|
| 211 |
|
| 212 |
okBtn.addEventListener('click', () => {
|
|
|
|
| 213 |
popup.classList.add('minimized');
|
| 214 |
});
|
| 215 |
|
|
@@ -965,12 +1013,8 @@ const regionData = {
|
|
| 965 |
"Greenland": { "money": 10, "friendliness": 1 }
|
| 966 |
};
|
| 967 |
|
| 968 |
-
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
// Détermine la couleur de chaque région (vert si au moins un pays est "friendless: Yes", rouge sinon)
|
| 973 |
-
const regionColors = Object.keys(regionData).reduce((acc, region) => {
|
| 974 |
const friendliness = regionData[region].friendliness;
|
| 975 |
// Convertir `friendliness` en une couleur allant de rouge vif (-3) à vert vif (3)
|
| 976 |
const green = Math.max(0, 255 + friendliness * 85);
|
|
|
|
| 157 |
addMessageToChat('Error: Failed to get response', false);
|
| 158 |
});
|
| 159 |
}
|
| 160 |
+
let receivedFirstMessage = false
|
| 161 |
+
function receiveFirstMessage() {
|
| 162 |
+
console.log("in receive first")
|
| 163 |
+
if (receivedFirstMessage) {
|
| 164 |
+
return
|
| 165 |
+
}
|
| 166 |
+
console.log("iiiin receive first")
|
| 167 |
+
|
| 168 |
+
receivedFirstMessage = true
|
| 169 |
+
|
| 170 |
+
const trumpMessageDiv = addMessageToChat("Donald Trump is typing", false);
|
| 171 |
+
let dots = 0;
|
| 172 |
+
|
| 173 |
+
// Animate "Donald Trump is typing..."
|
| 174 |
+
const typingInterval = setInterval(() => {
|
| 175 |
+
dots = (dots + 1) % 4; // Cycle through 0, 1, 2, 3 dots
|
| 176 |
+
const dotsText = '.'.repeat(dots); // Generate dots
|
| 177 |
+
updateChatMessage(trumpMessageDiv, `Donald Trump is typing${dotsText}`);
|
| 178 |
+
}, 200); // Update every 200ms
|
| 179 |
+
|
| 180 |
+
// Simulate fetching the first message from the server
|
| 181 |
+
fetch('/api/generate-text', {
|
| 182 |
+
method: 'POST',
|
| 183 |
+
headers: {
|
| 184 |
+
'Content-Type': 'application/json'
|
| 185 |
+
},
|
| 186 |
+
body: JSON.stringify({ message: "" })
|
| 187 |
+
})
|
| 188 |
+
.then(response => response.json())
|
| 189 |
+
.then(data => {
|
| 190 |
+
// Stop the typing animation
|
| 191 |
+
clearInterval(typingInterval);
|
| 192 |
+
|
| 193 |
+
// Replace "Donald Trump is typing..." with the actual message
|
| 194 |
+
updateChatMessage(trumpMessageDiv, "");
|
| 195 |
+
|
| 196 |
+
// Add the first message from Donald Trump
|
| 197 |
+
typeMessage(trumpMessageDiv, data.character_response);
|
| 198 |
+
})
|
| 199 |
+
.catch(error => {
|
| 200 |
+
console.error('Error:', error);
|
| 201 |
+
|
| 202 |
+
// Stop typing animation and display error message
|
| 203 |
+
clearInterval(typingInterval);
|
| 204 |
+
updateChatMessage(trumpMessageDiv, "Error: Failed to get the first message");
|
| 205 |
+
});
|
| 206 |
+
}
|
| 207 |
|
| 208 |
// Trigger on button click
|
| 209 |
document.getElementById('sendButton').addEventListener('click', sendMessage);
|
|
|
|
| 257 |
});
|
| 258 |
|
| 259 |
okBtn.addEventListener('click', () => {
|
| 260 |
+
receiveFirstMessage()
|
| 261 |
popup.classList.add('minimized');
|
| 262 |
});
|
| 263 |
|
|
|
|
| 1013 |
"Greenland": { "money": 10, "friendliness": 1 }
|
| 1014 |
};
|
| 1015 |
|
| 1016 |
+
// Détermine la couleur de chaque région (vert si au moins un pays est "friendless: Yes", rouge sinon)
|
| 1017 |
+
const regionColors = Object.keys(regionData).reduce((acc, region) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
const friendliness = regionData[region].friendliness;
|
| 1019 |
// Convertir `friendliness` en une couleur allant de rouge vif (-3) à vert vif (3)
|
| 1020 |
const green = Math.max(0, 255 + friendliness * 85);
|