botsi commited on
Commit
e08cb4c
·
verified ·
1 Parent(s): e9d44a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -71
app.py CHANGED
@@ -68,14 +68,16 @@ if torch.cuda.is_available():
68
  tokenizer.use_default_system_prompt = False
69
 
70
 
71
- ## gradio-chatbot-read-query-param
72
- get_window_session_index = """
73
- function() {
74
- const urlParams = new URLSearchParams(window.location.search);
75
- const session_index = urlParams.get('session_index');
76
- return session_index;
77
- }
78
- """
 
 
79
 
80
  def fetch_personalized_data(session_index):
81
  # Connect to the database
@@ -116,23 +118,13 @@ def fetch_personalized_data(session_index):
116
  # return [[str(row[0]), str(row[1]), str(row[2])] for row in rows] # Convert each row to a list
117
  # Convert the rows to a list of dictionaries
118
  data = [{'playerNr': row[0], 'groupNr': row[1], 'subjectNr': row[2]} for row in rows]
 
119
  return data
120
 
121
  except mysql.connector.Error as err:
122
  print(f"Error: {err}")
123
  return None
124
 
125
-
126
- ## gradio-chatbot-read-query-param
127
- def get_window_url_params():
128
- return """
129
- function() {
130
- const params = new URLSearchParams(window.location.search);
131
- const url_params = Object.fromEntries(params);
132
- return url_params;
133
- }
134
- """
135
-
136
  ## trust-game-llama-2-7b-chat
137
  # app.py
138
  def construct_input_prompt(chat_history, message, personalized_data):
@@ -145,56 +137,6 @@ def construct_input_prompt(chat_history, message, personalized_data):
145
 
146
  return input_prompt
147
 
148
- with gr.Blocks(css="style.css") as demo:
149
- #gr.Markdown(DESCRIPTION)
150
- #gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
151
- ## gradio-chatbot-read-query-param
152
- url_params = gr.JSON({}, visible=False, label="URL Params")
153
- # Execute the JavaScript function to obtain the URL parameters as a dictionary
154
- url_params = gr.javascript.execute(url_params)
155
-
156
- ## gradio-chatbot-read-query-param
157
- def get_session_index(history, url_params):
158
- personalized_data = None # Initialize with None
159
- if history and bool(history[-1][0].strip()):
160
- session_index = url_params.get('session_index')
161
- print(session_index)
162
- # Fetch personalized data
163
- personalized_data = fetch_personalized_data(session_index)
164
- print(personalized_data)
165
- return personalized_data
166
-
167
- ## trust-game-llama-2-7b-chat
168
- # app.py
169
- def get_default_system_prompt(personalized_data):
170
- #BOS, EOS = "<s>", "</s>"
171
- #BINST, EINST = "[INST]", "[/INST]"
172
- BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
173
-
174
- DEFAULT_SYSTEM_PROMPT = f""" You are an intelligent and fair game guide in a 2-player trust game, assisting players in making decisions to win.
175
- Answer in a consistent style. Answer per question should be maximum 2 sentences long. The players are called The Investor and The Dealer and keep their role throughout the whole game.
176
- Both start with 10€ in round 1. The game consists of 3 rounds. In round 1, The Investor invests between 0€ and 10€.
177
- This amount is tripled automatically, and The Dealer can then distribute the tripled amount. After that, round 1 is over.
178
- 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.
179
- The Dealer with 10€ plus what he kept from the tripled amount.
180
- You will receive a JSON with information on who trusted whom with how much money after each round as context.
181
- Your goal is to guide players through the game, providing clear instructions and explanations.
182
- If any question or action seems unclear, explain it rather than providing inaccurate information.
183
- If you're unsure about an answer, it's better not to guess.
184
-
185
- Example JSON context after a round: {personalized_data}
186
-
187
- Few-shot training examples
188
- {BSYS} Give an overview of the trust game. {ESYS}
189
- {BSYS} Explain how trust amounts are calculated. {ESYS}
190
- {BSYS} What happens if a player doesn't trust in a round? {ESYS}
191
- """
192
- print(DEFAULT_SYSTEM_PROMPT)
193
- return DEFAULT_SYSTEM_PROMPT
194
-
195
- chat_interface.render()
196
- #gr.Markdown(LICENSE)
197
-
198
  ## trust-game-llama-2-7b-chat
199
  # app.py
200
  @spaces.GPU
@@ -208,8 +150,7 @@ def generate(
208
  top_k: int = 50,
209
  repetition_penalty: float = 1.2,
210
  ) -> Iterator[str]: # Change return type hint to Iterator[str]
211
-
212
-
213
  # Fetch personalized data
214
  url_params = get_window_url_params()
215
  session_index = get_session_index(chat_history, url_params)
@@ -297,6 +238,47 @@ chat_interface = gr.ChatInterface(
297
  ],
298
  )
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  if __name__ == "__main__":
301
  #demo.queue(max_size=20).launch()
302
  demo.queue(max_size=20)
 
68
  tokenizer.use_default_system_prompt = False
69
 
70
 
71
+
72
+ ## gradio-chatbot-read-query-param
73
+ def get_window_url_params():
74
+ return """
75
+ function() {
76
+ const params = new URLSearchParams(window.location.search);
77
+ const url_params = Object.fromEntries(params);
78
+ return url_params;
79
+ }
80
+ """
81
 
82
  def fetch_personalized_data(session_index):
83
  # Connect to the database
 
118
  # return [[str(row[0]), str(row[1]), str(row[2])] for row in rows] # Convert each row to a list
119
  # Convert the rows to a list of dictionaries
120
  data = [{'playerNr': row[0], 'groupNr': row[1], 'subjectNr': row[2]} for row in rows]
121
+ print(data)
122
  return data
123
 
124
  except mysql.connector.Error as err:
125
  print(f"Error: {err}")
126
  return None
127
 
 
 
 
 
 
 
 
 
 
 
 
128
  ## trust-game-llama-2-7b-chat
129
  # app.py
130
  def construct_input_prompt(chat_history, message, personalized_data):
 
137
 
138
  return input_prompt
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  ## trust-game-llama-2-7b-chat
141
  # app.py
142
  @spaces.GPU
 
150
  top_k: int = 50,
151
  repetition_penalty: float = 1.2,
152
  ) -> Iterator[str]: # Change return type hint to Iterator[str]
153
+
 
154
  # Fetch personalized data
155
  url_params = get_window_url_params()
156
  session_index = get_session_index(chat_history, url_params)
 
238
  ],
239
  )
240
 
241
+ with gr.Blocks(css="style.css") as demo:
242
+ #gr.Markdown(DESCRIPTION)
243
+ #gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
244
+ ## gradio-chatbot-read-query-param
245
+ def get_session_index(chat_history, url_params):
246
+ if chat_history and bool(chat_history[-1][0].strip()):
247
+ session_index = url_params.get('session_index')
248
+ print(session_index)
249
+ return session_index
250
+
251
+ ## trust-game-llama-2-7b-chat
252
+ # app.py
253
+ def get_default_system_prompt(personalized_data):
254
+ #BOS, EOS = "<s>", "</s>"
255
+ #BINST, EINST = "[INST]", "[/INST]"
256
+ BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
257
+
258
+ DEFAULT_SYSTEM_PROMPT = f""" You are an intelligent and fair game guide in a 2-player trust game, assisting players in making decisions to win.
259
+ Answer in a consistent style. Answer per question should be maximum 2 sentences long. The players are called The Investor and The Dealer and keep their role throughout the whole game.
260
+ Both start with 10€ in round 1. The game consists of 3 rounds. In round 1, The Investor invests between 0€ and 10€.
261
+ This amount is tripled automatically, and The Dealer can then distribute the tripled amount. After that, round 1 is over.
262
+ 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.
263
+ The Dealer with 10€ plus what he kept from the tripled amount.
264
+ You will receive a JSON with information on who trusted whom with how much money after each round as context.
265
+ Your goal is to guide players through the game, providing clear instructions and explanations.
266
+ If any question or action seems unclear, explain it rather than providing inaccurate information.
267
+ If you're unsure about an answer, it's better not to guess.
268
+
269
+ Example JSON context after a round: {personalized_data}
270
+
271
+ Few-shot training examples
272
+ {BSYS} Give an overview of the trust game. {ESYS}
273
+ {BSYS} Explain how trust amounts are calculated. {ESYS}
274
+ {BSYS} What happens if a player doesn't trust in a round? {ESYS}
275
+ """
276
+ print(DEFAULT_SYSTEM_PROMPT)
277
+ return DEFAULT_SYSTEM_PROMPT
278
+
279
+ chat_interface.render()
280
+ #gr.Markdown(LICENSE)
281
+
282
  if __name__ == "__main__":
283
  #demo.queue(max_size=20).launch()
284
  demo.queue(max_size=20)