botsi commited on
Commit
fd47974
·
verified ·
1 Parent(s): 050899a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -142
app.py CHANGED
@@ -54,7 +54,148 @@ if torch.cuda.is_available():
54
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
55
  tokenizer = AutoTokenizer.from_pretrained(model_id)
56
  tokenizer.use_default_system_prompt = False
57
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  @spaces.GPU
59
  def generate(
60
  request: gr.Request, # To fetch query params
@@ -84,148 +225,8 @@ def generate(
84
  else:
85
  print("Session index parameter not found or has no value.")
86
 
87
- def fetch_personalized_data(session_index):
88
- try:
89
- # Connect to the database
90
- with mysql.connector.connect(
91
- host="18.153.94.89",
92
- user="root",
93
- password="N12RXMKtKxRj",
94
- database="lionessdb"
95
- ) as conn:
96
- # Create a cursor object
97
- with conn.cursor() as cursor:
98
- # Query to fetch relevant data from both tables based on session_index
99
- query = """
100
- SELECT e5390g37096_core.playerNr,
101
- e5390g37096_core.groupNrStart,
102
- e5390g37096_core.subjectNr,
103
- e5390g37096_core.onPage,
104
- e5390g37096_decisions.session_index,
105
- e5390g37096_decisions.transfer1,
106
- e5390g37096_decisions.tripledAmount1,
107
- e5390g37096_decisions.keptForSelf1,
108
- e5390g37096_decisions.returned1,
109
- e5390g37096_decisions.newCreditRound2,
110
- e5390g37096_decisions.transfer2,
111
- e5390g37096_decisions.tripledAmount2,
112
- e5390g37096_decisions.keptForSelf2,
113
- e5390g37096_decisions.returned2,
114
- e5390g37096_decisions.results2rounds,
115
- e5390g37096_decisions.newCreditRound3,
116
- e5390g37096_decisions.transfer3,
117
- e5390g37096_decisions.tripledAmount3,
118
- e5390g37096_decisions.keptForSelf3,
119
- e5390g37096_decisions.returned3,
120
- e5390g37096_decisions.results3rounds
121
- FROM e5390g37096_core
122
- JOIN e5390g37096_decisions ON
123
- e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
124
- WHERE e5390g37096_decisions.session_index = {session_index}
125
- UNION ALL
126
- SELECT e5390g37096_core.playerNr,
127
- e5390g37096_core.groupNrStart,
128
- e5390g37096_core.subjectNr,
129
- e5390g37096_core.onPage,
130
- e5390g37096_decisions.session_index,
131
- e5390g37096_decisions.transfer1,
132
- e5390g37096_decisions.tripledAmount1,
133
- e5390g37096_decisions.keptForSelf1,
134
- e5390g37096_decisions.returned1,
135
- e5390g37096_decisions.newCreditRound2,
136
- e5390g37096_decisions.transfer2,
137
- e5390g37096_decisions.tripledAmount2,
138
- e5390g37096_decisions.keptForSelf2,
139
- e5390g37096_decisions.returned2,
140
- e5390g37096_decisions.results2rounds,
141
- e5390g37096_decisions.newCreditRound3,
142
- e5390g37096_decisions.transfer3,
143
- e5390g37096_decisions.tripledAmount3,
144
- e5390g37096_decisions.keptForSelf3,
145
- e5390g37096_decisions.returned3,
146
- e5390g37096_decisions.results3rounds
147
- FROM e5390g37096_core
148
- JOIN e5390g37096_decisions
149
- ON e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
150
- WHERE e5390g37096_core.groupNrStart IN (
151
- SELECT DISTINCT groupNrStart
152
- FROM e5390g37096_core
153
- JOIN e5390g37096_decisions
154
- ON e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
155
- WHERE e5390g37096_decisions.session_index = {session_index}
156
- ) AND e5390g37096_decisions.session_index != {session_index}
157
- """
158
- cursor.execute(query, (session_index, session_index, session_index))
159
- # Fetch data row by row
160
- data = [{
161
- 'playerNr': row[0],
162
- 'groupNrStart': row[1],
163
- 'subjectNr': row[2],
164
- 'onPage': row[3],
165
- 'session_index': row[4],
166
- 'transfer1': row[5],
167
- 'tripledAmount1': row[6],
168
- 'keptForSelf1': row[7],
169
- 'returned1': row[8],
170
- 'newCreditRound2': row[9],
171
- 'transfer2': row[10],
172
- 'tripledAmount2': row[11],
173
- 'keptForSelf2': row[12],
174
- 'returned2': row[13],
175
- 'results2rounds': row[14],
176
- 'newCreditRound3': row[15],
177
- 'transfer3': row[16],
178
- 'tripledAmount3': row[17],
179
- 'keptForSelf3': row[18],
180
- 'returned3': row[19],
181
- 'results3rounds': row[20]
182
- } for row in cursor]
183
- print(data)
184
- return data
185
- except mysql.connector.Error as err:
186
- print(f"Error: {err}")
187
- return None
188
-
189
  personalized_data = fetch_personalized_data(session_index)
190
-
191
- ## trust-game-llama-2-7b-chat
192
- # app.py
193
- def get_default_system_prompt():
194
- #BOS, EOS = "<s>", "</s>"
195
- #BINST, EINST = "[INST]", "[/INST]"
196
- BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
197
-
198
- DEFAULT_SYSTEM_PROMPT = f""" You are an intelligent and fair game guide in a 2-player trust game.
199
- You are assisting players in making decisions to win.
200
- Answer in a consistent style. Each of your answers should be maximum 2 sentences long.
201
- The players are called The Investor and The Dealer and keep their role throughout the whole game.
202
- Both start with 10€ in round 1. The game consists of 3 rounds. In round 1, The Investor invests between 0€ and 10€.
203
- This amount is tripled automatically, and The Dealer can then distribute the tripled amount. After that, round 1 is over.
204
- Both go into the next round with their current asset: The Investor with 10€ minus what he invested plus what he received back from The Dealer.
205
- The Dealer with 10€ plus what he kept from the tripled amount.
206
- You will receive a JSON with information on who trusted whom with how much money after each round as context.
207
- Your goal is to guide players through the game, providing clear instructions and explanations.
208
- If any question or action seems unclear, explain it rather than providing inaccurate information.
209
- If you're unsure about an answer, it's better not to guess.
210
-
211
- Example JSON context after a round: {personalized_data}
212
-
213
- Few-shot training examples
214
- {BSYS} Give an overview of the trust game. {ESYS}
215
- {BSYS} Explain how trust amounts are calculated. {ESYS}
216
- {BSYS} What happens if a player doesn't trust in a round? {ESYS}
217
- """
218
- print(DEFAULT_SYSTEM_PROMPT)
219
- return DEFAULT_SYSTEM_PROMPT
220
-
221
- ## trust-game-llama-2-7b-chat
222
- # app.py
223
- def construct_input_prompt(chat_history, message):
224
- input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt()}\n<</SYS>>\n\n "
225
- for user, assistant in chat_history:
226
- input_prompt += f"{user} [/INST] {assistant} <s>[INST] "
227
- input_prompt += f"{message} [/INST] "
228
- return input_prompt
229
 
230
  # Construct the input prompt using the functions from the construct_input_prompt function
231
  input_prompt = construct_input_prompt(chat_history, message)
 
54
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
55
  tokenizer = AutoTokenizer.from_pretrained(model_id)
56
  tokenizer.use_default_system_prompt = False
57
+
58
+ def fetch_personalized_data(session_index):
59
+ try:
60
+ # Connect to the database
61
+ with mysql.connector.connect(
62
+ host="18.153.94.89",
63
+ user="root",
64
+ password="N12RXMKtKxRj",
65
+ database="lionessdb"
66
+ ) as conn:
67
+ # Create a cursor object
68
+ with conn.cursor() as cursor:
69
+ # Query to fetch relevant data from both tables based on session_index
70
+ query = """
71
+ SELECT e5390g37096_core.playerNr,
72
+ e5390g37096_core.groupNrStart,
73
+ e5390g37096_core.subjectNr,
74
+ e5390g37096_core.onPage,
75
+ e5390g37096_decisions.session_index,
76
+ e5390g37096_decisions.transfer1,
77
+ e5390g37096_decisions.tripledAmount1,
78
+ e5390g37096_decisions.keptForSelf1,
79
+ e5390g37096_decisions.returned1,
80
+ e5390g37096_decisions.newCreditRound2,
81
+ e5390g37096_decisions.transfer2,
82
+ e5390g37096_decisions.tripledAmount2,
83
+ e5390g37096_decisions.keptForSelf2,
84
+ e5390g37096_decisions.returned2,
85
+ e5390g37096_decisions.results2rounds,
86
+ e5390g37096_decisions.newCreditRound3,
87
+ e5390g37096_decisions.transfer3,
88
+ e5390g37096_decisions.tripledAmount3,
89
+ e5390g37096_decisions.keptForSelf3,
90
+ e5390g37096_decisions.returned3,
91
+ e5390g37096_decisions.results3rounds
92
+ FROM e5390g37096_core
93
+ JOIN e5390g37096_decisions ON
94
+ e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
95
+ WHERE e5390g37096_decisions.session_index = %s
96
+ UNION ALL
97
+ SELECT e5390g37096_core.playerNr,
98
+ e5390g37096_core.groupNrStart,
99
+ e5390g37096_core.subjectNr,
100
+ e5390g37096_core.onPage,
101
+ e5390g37096_decisions.session_index,
102
+ e5390g37096_decisions.transfer1,
103
+ e5390g37096_decisions.tripledAmount1,
104
+ e5390g37096_decisions.keptForSelf1,
105
+ e5390g37096_decisions.returned1,
106
+ e5390g37096_decisions.newCreditRound2,
107
+ e5390g37096_decisions.transfer2,
108
+ e5390g37096_decisions.tripledAmount2,
109
+ e5390g37096_decisions.keptForSelf2,
110
+ e5390g37096_decisions.returned2,
111
+ e5390g37096_decisions.results2rounds,
112
+ e5390g37096_decisions.newCreditRound3,
113
+ e5390g37096_decisions.transfer3,
114
+ e5390g37096_decisions.tripledAmount3,
115
+ e5390g37096_decisions.keptForSelf3,
116
+ e5390g37096_decisions.returned3,
117
+ e5390g37096_decisions.results3rounds
118
+ FROM e5390g37096_core
119
+ JOIN e5390g37096_decisions
120
+ ON e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
121
+ WHERE e5390g37096_core.groupNrStart IN (
122
+ SELECT DISTINCT groupNrStart
123
+ FROM e5390g37096_core
124
+ JOIN e5390g37096_decisions
125
+ ON e5390g37096_core.playerNr = e5390g37096_decisions.playerNr
126
+ WHERE e5390g37096_decisions.session_index = %s
127
+ ) AND e5390g37096_decisions.session_index != %s
128
+ """
129
+ cursor.execute(query,(session_index, session_index, session_index))
130
+ # Fetch data row by row
131
+ data = [{
132
+ 'playerNr': row[0],
133
+ 'groupNrStart': row[1],
134
+ 'subjectNr': row[2],
135
+ 'onPage': row[3],
136
+ 'session_index': row[4],
137
+ 'transfer1': row[5],
138
+ 'tripledAmount1': row[6],
139
+ 'keptForSelf1': row[7],
140
+ 'returned1': row[8],
141
+ 'newCreditRound2': row[9],
142
+ 'transfer2': row[10],
143
+ 'tripledAmount2': row[11],
144
+ 'keptForSelf2': row[12],
145
+ 'returned2': row[13],
146
+ 'results2rounds': row[14],
147
+ 'newCreditRound3': row[15],
148
+ 'transfer3': row[16],
149
+ 'tripledAmount3': row[17],
150
+ 'keptForSelf3': row[18],
151
+ 'returned3': row[19],
152
+ 'results3rounds': row[20]
153
+ } for row in cursor]
154
+ print(data)
155
+ return data
156
+ except mysql.connector.Error as err:
157
+ print(f"Error: {err}")
158
+ return None
159
+
160
+ ## trust-game-llama-2-7b-chat
161
+ # app.py
162
+ def get_default_system_prompt(personalized_data):
163
+ #BOS, EOS = "<s>", "</s>"
164
+ #BINST, EINST = "[INST]", "[/INST]"
165
+ BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
166
+
167
+ DEFAULT_SYSTEM_PROMPT = f""" You are an intelligent and fair game guide in a 2-player trust game.
168
+ You are assisting players in making decisions to win.
169
+ Answer in a consistent style. Each of your answers should be maximum 2 sentences long.
170
+ The players are called The Investor and The Dealer and keep their role throughout the whole game.
171
+ Both start with 10€ in round 1. The game consists of 3 rounds. In round 1, The Investor invests between 0€ and 10€.
172
+ This amount is tripled automatically, and The Dealer can then distribute the tripled amount. After that, round 1 is over.
173
+ Both go into the next round with their current asset: The Investor with 10€ minus what he invested plus what he received back from The Dealer.
174
+ The Dealer with 10€ plus what he kept from the tripled amount.
175
+ You will receive a JSON with information on who trusted whom with how much money after each round as context.
176
+ Your goal is to guide players through the game, providing clear instructions and explanations.
177
+ If any question or action seems unclear, explain it rather than providing inaccurate information.
178
+ If you're unsure about an answer, it's better not to guess.
179
+
180
+ Example JSON context after a round: {personalized_data}
181
+
182
+ Few-shot training examples
183
+ {BSYS} Give an overview of the trust game. {ESYS}
184
+ {BSYS} Explain how trust amounts are calculated. {ESYS}
185
+ {BSYS} What happens if a player doesn't trust in a round? {ESYS}
186
+ """
187
+ print(DEFAULT_SYSTEM_PROMPT)
188
+ return DEFAULT_SYSTEM_PROMPT
189
+
190
+ ## trust-game-llama-2-7b-chat
191
+ # app.py
192
+ def construct_input_prompt(chat_history, message):
193
+ input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt(personalized_data)}\n<</SYS>>\n\n "
194
+ for user, assistant in chat_history:
195
+ input_prompt += f"{user} [/INST] {assistant} <s>[INST] "
196
+ input_prompt += f"{message} [/INST] "
197
+ return input_prompt
198
+
199
  @spaces.GPU
200
  def generate(
201
  request: gr.Request, # To fetch query params
 
225
  else:
226
  print("Session index parameter not found or has no value.")
227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  personalized_data = fetch_personalized_data(session_index)
229
+ DEFAULT_SYSTEM_PROMPT = get_default_system_prompt(personalized_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
  # Construct the input prompt using the functions from the construct_input_prompt function
232
  input_prompt = construct_input_prompt(chat_history, message)