File size: 23,416 Bytes
e1834e2
7547d3d
b20408a
 
 
fd7d7b0
 
 
 
 
 
 
 
 
b20408a
fd7d7b0
 
 
b20408a
 
fd7d7b0
b20408a
fd7d7b0
 
 
 
996864d
fd7d7b0
b8ddc1d
 
 
996864d
24df928
 
 
 
996864d
 
 
 
 
 
 
 
 
 
5496e8b
24df928
5496e8b
de8bfa9
24df928
 
7f7e00b
 
24df928
 
 
 
7daa200
5496e8b
298d1b7
a3d6cea
fd7d7b0
 
 
 
 
5dae906
050899a
21fbb3f
fd7d7b0
b20408a
5dae906
 
fd7d7b0
9a103c5
 
050899a
5dae906
fd7d7b0
 
 
 
 
 
 
 
 
 
 
f114505
 
36cb3bb
fd47974
 
 
04e0cb5
fd47974
7f7e00b
fd47974
 
 
 
de8bfa9
fd47974
84f4297
 
 
 
 
 
de8bfa9
 
84f4297
 
 
 
753ed68
84f4297
 
 
 
de8bfa9
84f4297
 
 
 
de8bfa9
 
 
 
 
 
 
 
 
 
 
 
 
 
84f4297
 
 
 
 
 
f114505
84f4297
 
 
 
 
 
de8bfa9
 
84f4297
 
 
 
753ed68
84f4297
 
 
 
de8bfa9
84f4297
 
 
 
de8bfa9
 
 
 
 
 
 
 
 
 
 
 
 
 
84f4297
 
 
 
 
 
fd47974
84f4297
 
 
 
 
fd47974
36cb3bb
fd47974
 
 
 
 
 
de8bfa9
36cb3bb
de8bfa9
 
 
 
 
 
753ed68
de8bfa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd47974
 
 
 
 
 
de8bfa9
4195198
 
 
 
753ed68
4195198
753ed68
4195198
 
 
 
 
 
 
 
753ed68
4195198
 
 
753ed68
 
 
 
4195198
 
 
 
 
 
 
 
 
de8bfa9
fd47974
7b16cb8
1f68202
 
569a4bf
de8bfa9
 
 
 
 
 
 
 
 
 
 
 
1f68202
 
569a4bf
 
1f68202
 
 
 
 
569a4bf
 
 
49af576
 
569a4bf
 
 
 
 
 
 
 
4195198
 
 
 
 
 
 
 
d3fbd27
4195198
 
d3fbd27
4195198
 
 
 
d3fbd27
76cfaea
4195198
 
 
 
 
569a4bf
7b16cb8
 
de8bfa9
 
7b16cb8
 
 
 
 
 
 
9c3db7a
 
c81be02
9c3db7a
 
7b16cb8
9c3db7a
 
 
 
 
7b16cb8
fa072ac
7b16cb8
 
84701cb
9f83aae
 
 
84701cb
 
6c3e9d6
 
 
84701cb
70d1374
e1834e2
 
70d1374
e1834e2
 
 
70d1374
e1834e2
36cb3bb
70d1374
7b16cb8
de8bfa9
 
 
4195198
de8bfa9
 
 
 
 
 
 
 
 
 
 
753ed68
de8bfa9
 
 
 
 
 
 
49af576
de8bfa9
 
 
 
 
 
 
753ed68
de8bfa9
 
4195198
49af576
4195198
7b16cb8
de8bfa9
7b16cb8
9c3db7a
 
 
389922e
 
 
7b16cb8
9c3db7a
 
 
 
389922e
9c3db7a
 
 
 
 
143a130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff526aa
143a130
ff526aa
143a130
5496e8b
 
 
 
 
 
be9f36c
5496e8b
cfe0a62
 
 
5496e8b
de8bfa9
5496e8b
be9f36c
5496e8b
 
1f68202
be9f36c
 
 
 
 
 
 
 
0652dbc
 
 
a398fcf
 
0652dbc
 
 
dba1cca
be9f36c
 
0652dbc
5496e8b
0652dbc
 
 
dba1cca
 
 
 
 
 
 
0652dbc
5496e8b
7a53840
9c3db7a
88efd8e
 
 
9c3db7a
 
 
 
 
feec46b
9c3db7a
2772476
91c7951
6b695ad
2329a6b
ca77042
2329a6b
589ac10
8029c32
 
4195198
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# externalID (from database) and PROLIFIC_PID (from URL parameters as query parameter)

# Original code from https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat
# Modified for trust game purposes

import gradio as gr
import time
import random
import json
import mysql.connector
import os
import csv
import spaces
import torch

from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
from typing import Iterator
from huggingface_hub import Repository, hf_hub_download
from datetime import datetime

# for fetch_personalized_data
import mysql.connector
import urllib.parse
import urllib.request

# for saving chat history as JSON - not used 
import atexit
import os
from huggingface_hub import HfApi, HfFolder

# for saving chat history as dataset - not used
import huggingface_hub
from huggingface_hub import Repository
from datetime import datetime

# for saving chat history as dataset - used 
import sqlite3
import huggingface_hub
import gradio as gr
import pandas as pd
import shutil
import os
import datetime
from apscheduler.schedulers.background import BackgroundScheduler


DATASET_REPO_URL = "https://huggingface.co/datasets/botsi/trust-game-llama-2-chat-history"
DATA_DIRECTORY = "data"  # Separate directory for storing data files
DATA_FILENAME = "7B.csv"  # Default filename
DATA_FILE = os.path.join("data", DATA_FILENAME)

DB_PASSWORD = os.environ.get("DB_PASSWORD")

HF_TOKEN = os.environ.get("HF_TOKEN")
print("is none?", HF_TOKEN is None)
print("hfh", huggingface_hub.__version__)

repo = Repository(
    local_dir=DATA_DIRECTORY, clone_from=DATASET_REPO_URL
)

MAX_MAX_NEW_TOKENS = 2048
DEFAULT_MAX_NEW_TOKENS = 1024
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))

DESCRIPTION = """\
# This is your personal space to chat. 
You can ask anything: From discussing strategic game tactics to enjoying casual conversation. 
For example you could ask, what happened in the last round, what is your probability to win when you invest amount xy, what is my current balance etc.
"""

# License and Acceptable Use Policy by Meta  
LICENSE = """
<p/>

---
This demo is governed by the [original license](https://ai.meta.com/llama/license/) and [acceptable use policy](https://ai.meta.com/llama/use-policy/). 
The most recent copy of this policy can be found at ai.meta.com/llama/use-policy.
"""

if not torch.cuda.is_available():
    DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"


if torch.cuda.is_available():
    model_id = "meta-llama/Llama-2-7b-chat-hf"
    model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    tokenizer.use_default_system_prompt = False


def fetch_personalized_data(externalID):
    try:
        # Connect to the database
        with mysql.connector.connect(
            host="3.125.179.74",
            user="root",
            password=DB_PASSWORD,
            database="lionessdb"
        ) as conn:
            # Create a cursor object
            with conn.cursor() as cursor:
                # Query to fetch relevant data from both tables based on externalID = externalID  
                query = """
                    SELECT e5390g37899_core.playerNr, 
                           e5390g37899_core.groupNrStart, 
                           e5390g37899_core.subjectNr, 
                           e5390g37899_core.onPage, 
                           e5390g37899_core.role,
                           e5390g37899_session.externalID, 
                           e5390g37899_decisions.initialCredit,
                           e5390g37899_decisions.part,
                           e5390g37899_decisions.transfer1,
                           e5390g37899_decisions.tripledAmount1,
                           e5390g37899_decisions.keptForSelf1,
                           e5390g37899_decisions.returned1,
                           e5390g37899_decisions.totalRound1, 
                           e5390g37899_decisions.transfer2,
                           e5390g37899_decisions.tripledAmount2,
                           e5390g37899_decisions.keptForSelf2,
                           e5390g37899_decisions.returned2,
                           e5390g37899_decisions.totalRound2, 
                           e5390g37899_decisions.transfer3, 
                           e5390g37899_decisions.tripledAmount3, 
                           e5390g37899_decisions.keptForSelf3, 
                           e5390g37899_decisions.returned3, 
                           e5390g37899_decisions.totalRound3, 
                           e5390g37899_decisions.transfer4, 
                           e5390g37899_decisions.tripledAmount4, 
                           e5390g37899_decisions.keptForSelf4, 
                           e5390g37899_decisions.returned4,
                           e5390g37899_decisions.totalRound4, 
                           e5390g37899_decisions.transfer5, 
                           e5390g37899_decisions.tripledAmount5, 
                           e5390g37899_decisions.keptForSelf5, 
                           e5390g37899_decisions.returned5, 
                           e5390g37899_decisions.totalRound5, 
                           e5390g37899_decisions.transfer6, 
                           e5390g37899_decisions.tripledAmount6, 
                           e5390g37899_decisions.keptForSelf6
                    FROM e5390g37899_core
                    JOIN e5390g37899_session ON 
                         e5390g37899_core.playerNr = e5390g37899_session.playerNr
                    JOIN e5390g37899_decisions ON 
                         e5390g37899_core.playerNr = e5390g37899_decisions.playerNr
                    WHERE e5390g37899_session.externalID = %s
                    UNION ALL
                    SELECT e5390g37899_core.playerNr, 
                           e5390g37899_core.groupNrStart, 
                           e5390g37899_core.subjectNr, 
                           e5390g37899_core.onPage, 
                           e5390g37899_core.role,
                           e5390g37899_session.externalID, 
                           e5390g37899_decisions.initialCredit,
                           e5390g37899_decisions.part,
                           e5390g37899_decisions.transfer1,
                           e5390g37899_decisions.tripledAmount1,
                           e5390g37899_decisions.keptForSelf1,
                           e5390g37899_decisions.returned1,
                           e5390g37899_decisions.totalRound1, 
                           e5390g37899_decisions.transfer2,
                           e5390g37899_decisions.tripledAmount2,
                           e5390g37899_decisions.keptForSelf2,
                           e5390g37899_decisions.returned2,
                           e5390g37899_decisions.totalRound2, 
                           e5390g37899_decisions.transfer3, 
                           e5390g37899_decisions.tripledAmount3, 
                           e5390g37899_decisions.keptForSelf3, 
                           e5390g37899_decisions.returned3, 
                           e5390g37899_decisions.totalRound3, 
                           e5390g37899_decisions.transfer4, 
                           e5390g37899_decisions.tripledAmount4, 
                           e5390g37899_decisions.keptForSelf4, 
                           e5390g37899_decisions.returned4,
                           e5390g37899_decisions.totalRound4, 
                           e5390g37899_decisions.transfer5, 
                           e5390g37899_decisions.tripledAmount5, 
                           e5390g37899_decisions.keptForSelf5, 
                           e5390g37899_decisions.returned5, 
                           e5390g37899_decisions.totalRound5, 
                           e5390g37899_decisions.transfer6, 
                           e5390g37899_decisions.tripledAmount6, 
                           e5390g37899_decisions.keptForSelf6
                    FROM e5390g37899_core
                    JOIN e5390g37899_session ON 
                        e5390g37899_core.playerNr = e5390g37899_session.playerNr
                    JOIN e5390g37899_decisions 
                        ON e5390g37899_core.playerNr = e5390g37899_decisions.playerNr
                    WHERE e5390g37899_core.groupNrStart IN (
                        SELECT DISTINCT groupNrStart
                        FROM e5390g37899_core
                        JOIN e5390g37899_session 
                            ON e5390g37899_core.playerNr = e5390g37899_session.playerNr
                        WHERE e5390g37899_session.externalID = %s
                    ) AND e5390g37899_session.externalID != %s
                """
                cursor.execute(query,(externalID, externalID, externalID))
                # Fetch data row by row
                data = [{
                    'playerNr': row[0], 
                    'groupNrStart': row[1], 
                    'subjectNr': row[2], 
                    'onPage': row[3],
                    'role': row[4],
                    'externalID': row[5],
                    'initialCredit': row[6],
                    'part': row[7],
                    'transfer1': row[8],
                    'tripledAmount1': row[9],
                    'keptForSelf1': row[10],
                    'returned1': row[11],
                    'totalRound1': row[12],
                    'transfer2': row[13],
                    'tripledAmount2': row[14],
                    'keptForSelf2': row[15],
                    'returned2': row[16],
                    'totalRound2': row[17],
                    'transfer3': row[18],
                    'tripledAmount3': row[19],
                    'keptForSelf3': row[20],
                    'returned3': row[21],
                    'totalRound3': row[22],
                    'transfer4': row[23],
                    'tripledAmount4': row[24],
                    'keptForSelf4': row[25],
                    'returned4': row[26],
                    'totalRound4': row[27],
                    'transfer5': row[28],
                    'tripledAmount5': row[29],
                    'keptForSelf5': row[30],
                    'returned5': row[31],
                    'totalRound5': row[32],
                    'transfer6': row[33],
                    'tripledAmount6': row[34],
                    'keptForSelf6': row[35]
                } for row in cursor]
                print(data)
                return data
    except mysql.connector.Error as err:
        print(f"Error: {err}")
        return None
    
def extract_variables(all_personalized_data, part):
    extracted_data = {}

    if part == "1":
        rounds = range(1, 4)  # Rounds 1-3 for part 1
    elif part == "2":
        rounds = range(4, 7)  # Rounds 4-6 for part 2 
    else:
        print("No data for the particular part found")
        return None

    for data in all_personalized_data:
        role = map_role(str(data.get('role', 'unknown')))  # Get the role description
        player_data = {}  # Store data for the current player
        for round_num in rounds:
            round_key = f'round{round_num - 3 if part == "2" else round_num}'  # Adjusting round numbers if part is 2
            player_data[round_key] = {}
            for var in ['transfer', 'tripledAmount', 'keptForSelf', 'returned', 'totalRound']:
                var_name = f'{var}{round_num}'
                if role == 'The Dealer' and var == 'tripledAmount':
                    continue  # Skip adding 'tripledAmount' for the Dealer
                if role == 'The Investor' and var == 'keptForSelf':
                    continue  # Skip adding 'keptForSelf' for the Investor
                if data.get(var_name) is not None:
                    player_data[round_key][var] = data[var_name]

        # Update extracted_data with role prompt as key
        if role in extracted_data:
            extracted_data[role].update(player_data)
        else:
            extracted_data[role] = player_data

    return extracted_data


def map_onPage(onPage):
    # Define the mapping of onPage values to onPage_filename and onPage_prompt
    onPage_mapping_dict = {
        "stage411228.php": ("stage 6", "Round 1: Investor’s turn"),
        "stage411229.php": ("stage 7", "Round 1: Dealer’s turn"),
        "stage411230.php": ("stage 8", "Round 2: Investor’s turn"),
        "stage411231.php": ("stage 9", "Round 2: Investor’s turn"),
        "stage411232.php": ("stage 10", "Round 3: Investor’s turn"),
        "stage411233.php": ("stage 11", "Round 3: Dealer’s turn"),
        "stage411235.php": ("stage 13", "Round 1: Investor’s turn"),
        "stage411236.php": ("stage 14", "Round 1: Dealer’s turn"),
        "stage411237.php": ("stage 15", "Round 2: Investor’s turn"),
        "stage411238.php": ("stage 16", "Round 2: Investor’s turn"),
        "stage411239.php": ("stage 17", "Round 3: Investor’s turn"),
        "stage411240.php": ("stage 18", "Round 3: Dealer’s turn"),
    }
    # Check if onPage is in the mapping
    if onPage in onPage_mapping_dict:
        onPage_filename, onPage_prompt = onPage_mapping_dict[onPage]
    else:
        # If onPage is not in the mapping, set onPage_filename and onPage_prompt to "unknown"
        onPage_filename, onPage_prompt = "unknown", "unknown"
    return onPage_filename, onPage_prompt

def map_role(role):
    # Define the mapping of role numbers to role descriptions
    role_mapping_dict = {
        "1": "The Investor",
        "2": "The Dealer"
    }
    # Check if the role is in the mapping
    if role in role_mapping_dict:
        role_prompt = role_mapping_dict[role]
    else:
        # If the role is not in the mapping, set role_prompt to "unknown"
        role_prompt = "unknown"
    return role_prompt
            
## trust-game-llama-2-7b-chat
# app.py 
def get_default_system_prompt(extracted_data, onPage_prompt, role_prompt):
    #BOS, EOS = "<s>", "</s>" 
    #BINST, EINST = "[INST]", "[/INST]"
    BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"

    DEFAULT_SYSTEM_PROMPT = f""" You are a smart game assistant for a Trust Game outside of this chat. 
    Trust Game rules: Two players, The Investor and The Dealer, each play to maximize their own earnings. 
    There are 3 rounds. Every round follows the same pattern. 
    - First, each player gets a virtual starting credit of 10 coins. 
    - Investor's turn: The Investor decides how much they want to investo into a shared pot. The shared pot is tripled automatically before the Dealer's turn.
    - Dealer's turn: The Dealer can keep and return as much of the tripled amount as they like. Their virtual starting credit remains untouched.
    Earnings from each round are not transferred to the next round. Each or your answers should be maximum 2 sentences long. 
    Answer in a consistent style. If you are unsure about an answer, do not guess. 
    Currently it is {role_prompt}’s turn so you are assisting {role_prompt}. Answer directly to the player. The currency is coins.
    The game is currently in {onPage_prompt}.
    This is what happened in the last rounds: {extracted_data}. 
    """
    print(DEFAULT_SYSTEM_PROMPT)
    return DEFAULT_SYSTEM_PROMPT


## trust-game-llama-2-7b-chat
# app.py
def construct_input_prompt(chat_history, message, extracted_data, onPage_prompt, role_prompt):
    input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt(extracted_data, onPage_prompt, role_prompt)}\n<</SYS>>\n\n "
    for user, assistant in chat_history:
        input_prompt += f"{user} [/INST] {assistant} <s>[INST] "
    input_prompt += f"{message} [/INST] "
    return input_prompt

## trust-game-llama-2-7b-chat
# app.py 
@spaces.GPU
def generate(
    request: gr.Request, # To fetch query params
    message: str,
    chat_history: list[tuple[str, str]],
    # input_prompt: str,
    max_new_tokens: int = 1024,
    temperature: float = 0.6,
    top_p: float = 0.9,
    top_k: int = 50,
    repetition_penalty: float = 1.2,
) -> Iterator[str]: # Change return type hint to Iterator[str]

    conversation = []
    
    # Fetch query params - OLD with gradio sdk = 4.20.0 version 
    params = request.query_params
    print('those are the query params')
    print(params)

    # Fetch query params - NEW with gradio sdk = 4.25.0 version 
    #params = {key: value for key, value in request.query_params.items()}
    #print('those are the query params')
    #print(params)
    
    # Assuming params = request.query_params is the dictionary containing the query parameters    
    # Extract the value of the 'externalID' parameter   
    externalID = params.get('PROLIFIC_PID')
    
    # Check if externalID value is None or contains a value
    if externalID is not None:
        print("PROLIFIC_PID:", externalID)
    else:
        externalID = 'no_externalID'
        print("PROLIFIC_PID not found or has no value.")

    # Fetch personalized data
    #personalized_data = fetch_personalized_data(externalID)
    all_personalized_data = fetch_personalized_data(externalID)

    # Initialize onPage, playerNr, and groupNrStart variables
    onPage = playerNr = groupNrStart = role = part = None
    
    # Iterate over each dictionary in the list
    if all_personalized_data:
        for entry in all_personalized_data:
            # Check if the externalID matches the value in externalID variable (PROLIFIC_PID from the URL parameters)
            if entry['externalID'] == externalID:
                playerNr = entry.get('playerNr', "no_playerNr")  # Retrieve playerNr value
                groupNrStart = entry.get('groupNrStart', "no_groupNrStart")  # Retrieve groupNrStart value
                onPage = entry.get('onPage', "no_onPage")  # Retrieve onPage value
                role = entry.get('role', "no_role")  # Retrieve role value
                part = entry.get('part', "no_part")  # Retrieve part value
                break  # Break the loop since we found the desired entry
    
    # Print the values of onPage, playerNr, and groupNrStart and oart
    print("onPage:", onPage)
    print("playerNr:", playerNr)
    print("groupNrStart:", groupNrStart)
    print("role:", role)
    print("part:", part)
        
    # Print the onPage value
    onPage_filename, onPage_prompt = map_onPage(onPage)
    print("onPage_filename:", onPage_filename)
    print("onPage_prompt:", onPage_prompt)

    # Print the role value 
    role_prompt = map_role(str(role))
    print("role_prompt:", role_prompt)
    
    extracted_data = extract_variables(all_personalized_data, part)
    print(extracted_data)
        
    # Construct the input prompt using the functions from the system_prompt_config module
    input_prompt = construct_input_prompt(chat_history, message, extracted_data, onPage_prompt, role_prompt)

    # Move the condition here after the assignment
    if input_prompt:
        conversation.append({"role": "system", "content": input_prompt})

    # Convert input prompt to tensor
    input_ids = tokenizer(input_prompt, return_tensors="pt").to(model.device)

    for user, assistant in chat_history:
        conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
    conversation.append({"role": "user", "content": message})

    input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
    if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
        input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
        gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
    input_ids = input_ids.to(model.device)

    # Set up the TextIteratorStreamer
    streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
    
    # Set up the generation arguments
    generate_kwargs = dict(
        {"input_ids": input_ids},
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        num_beams=1,
        repetition_penalty=repetition_penalty,
    )

    # Start the model generation thread
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    # Yield generated text chunks
    outputs = []
    for text in streamer:
        outputs.append(text)
        yield "".join(outputs)

    # Fix bug that last answer is not recorded! 
    # Parse the outputs into a readable sentence and record them
    # Filter out empty strings and join the remaining strings with spaces
    readable_sentence = ' '.join(filter(lambda x: x.strip(), outputs))
    # Print the readable sentence
    print(readable_sentence)

    # Save chat history to .csv file on HuggingFace Hub 
    #pd.DataFrame(conversation).to_csv(DATA_FILE, index=False)
    #print("updating conversation")
    #repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
    #print(conversation)


    # Save chat history to .csv file on HuggingFace Hub 
    # Set the maximum column width to None to prevent truncation
    pd.set_option("display.max_colwidth", None)
    
    # Generate filename with bot id and session id
    filename = f"{groupNrStart}_{playerNr}_{externalID}_{onPage_filename}_{DATA_FILENAME}"
    data_file = os.path.join(DATA_DIRECTORY, filename)
    
    # Generate timestamp
    timestamp = datetime.datetime.now()
    
    # Check if the file already exists
    if os.path.exists(data_file):
        # If file exists, load existing data
        existing_data = pd.read_csv(data_file)
    else:
        # If file doesn't exist, set existing_data to None
        existing_data = None
    
    # Create a DataFrame for the current conversation turn
    turn_data = {
        "turn_id": len(existing_data) + 1 if existing_data is not None else 1,
        "question": message,
        "answer": readable_sentence,
        "timestamp": timestamp,
    }
    turn_df = pd.DataFrame([turn_data])

    # Check if existing_data is not None and concatenate the new conversation turn
    if existing_data is not None:
        updated_data = pd.concat([existing_data, turn_df], ignore_index=True)
    else:
        updated_data = turn_df
    
    # Write the updated data to the CSV file
    # updated_data.to_csv(data_file, index=False)

    # Write the updated data to the CSV file with no quoting
    # updated_data.to_csv(data_file, index=False, quoting=csv.QUOTE_NONE)

    # Write the updated data to the CSV file with all fields quoted
    updated_data.to_csv(data_file, index=False, quoting=csv.QUOTE_ALL)
    
    print("Updating .csv")
    repo.push_to_hub(blocking=False, commit_message=f"Updating data at {timestamp}")

css = """
share-button svelte-1lcyrx4 {visibility: hidden}
"""
chat_interface = gr.ChatInterface(
fn=generate,
retry_btn=None,
clear_btn=None,
undo_btn=None,
chatbot=gr.Chatbot(avatar_images=('user.png', 'bot.png'), bubble_full_width = True, elem_id = 'chatbot'), 
)

with gr.Blocks(css="style.css") as demo:
    #gr.Markdown(DESCRIPTION)
    chat_interface.render()
    gr.Markdown(LICENSE)
    
if __name__ == "__main__":
    demo.queue(max_size=20).launch()
    #demo.queue(max_size=20)
    demo.launch(share=True, debug=True)