# 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 import atexit import os from huggingface_hub import HfApi, HfFolder # for saving chat history as dataset import huggingface_hub from huggingface_hub import Repository from datetime import datetime DATASET_REPO_URL = "https://huggingface.co/datasets/botsi/trust-game-llama-2-chat-history" DATA_FILENAME = "data.csv" DATA_FILE = os.path.join("data", DATA_FILENAME) HF_TOKEN = os.environ.get("HF_TOKEN") print("is none?", HF_TOKEN is None) print("hfh", huggingface_hub.__version__) repo = Repository( local_dir="data", 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. """ # License and Acceptable Use Policy by Meta LICENSE = """
--- 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 += "\nRunning on CPU 🥶 This demo does not work on CPU.
" 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(session_index): try: # Connect to the database with mysql.connector.connect( host="18.153.94.89", user="root", password="N12RXMKtKxRj", database="lionessdb" ) as conn: # Create a cursor object with conn.cursor() as cursor: # Query to fetch relevant data from both tables based on session_index query = """ SELECT e5390g37504_core.playerNr, e5390g37504_core.groupNrStart, e5390g37504_core.subjectNr, e5390g37504_core.onPage, e5390g37504_decisions.session_index, e5390g37504_decisions.transfer1, e5390g37504_decisions.tripledAmount1, e5390g37504_decisions.keptForSelf1, e5390g37504_decisions.returned1, e5390g37504_decisions.newCreditRound2, e5390g37504_decisions.transfer2, e5390g37504_decisions.tripledAmount2, e5390g37504_decisions.keptForSelf2, e5390g37504_decisions.returned2, e5390g37504_decisions.results2rounds, e5390g37504_decisions.newCreditRound3, e5390g37504_decisions.transfer3, e5390g37504_decisions.tripledAmount3, e5390g37504_decisions.keptForSelf3, e5390g37504_decisions.returned3, e5390g37504_decisions.results3rounds FROM e5390g37504_core JOIN e5390g37504_decisions ON e5390g37504_core.playerNr = e5390g37504_decisions.playerNr WHERE e5390g37504_decisions.session_index = %s UNION ALL SELECT e5390g37504_core.playerNr, e5390g37504_core.groupNrStart, e5390g37504_core.subjectNr, e5390g37504_core.onPage, e5390g37504_decisions.session_index, e5390g37504_decisions.transfer1, e5390g37504_decisions.tripledAmount1, e5390g37504_decisions.keptForSelf1, e5390g37504_decisions.returned1, e5390g37504_decisions.newCreditRound2, e5390g37504_decisions.transfer2, e5390g37504_decisions.tripledAmount2, e5390g37504_decisions.keptForSelf2, e5390g37504_decisions.returned2, e5390g37504_decisions.results2rounds, e5390g37504_decisions.newCreditRound3, e5390g37504_decisions.transfer3, e5390g37504_decisions.tripledAmount3, e5390g37504_decisions.keptForSelf3, e5390g37504_decisions.returned3, e5390g37504_decisions.results3rounds FROM e5390g37504_core JOIN e5390g37504_decisions ON e5390g37504_core.playerNr = e5390g37504_decisions.playerNr WHERE e5390g37504_core.groupNrStart IN ( SELECT DISTINCT groupNrStart FROM e5390g37504_core JOIN e5390g37504_decisions ON e5390g37504_core.playerNr = e5390g37504_decisions.playerNr WHERE e5390g37504_decisions.session_index = %s ) AND e5390g37504_decisions.session_index != %s """ cursor.execute(query,(session_index, session_index, session_index)) # Fetch data row by row data = [{ 'playerNr': row[0], 'groupNrStart': row[1], 'subjectNr': row[2], 'onPage': row[3], 'session_index': row[4], 'transfer1': row[5], 'tripledAmount1': row[6], 'keptForSelf1': row[7], 'returned1': row[8], 'newCreditRound2': row[9], 'transfer2': row[10], 'tripledAmount2': row[11], 'keptForSelf2': row[12], 'returned2': row[13], 'results2rounds': row[14], 'newCreditRound3': row[15], 'transfer3': row[16], 'tripledAmount3': row[17], 'keptForSelf3': row[18], 'returned3': row[19], 'results3rounds': row[20] } for row in cursor] print(data) return data except mysql.connector.Error as err: print(f"Error: {err}") return None ## trust-game-llama-2-7b-chat # app.py def get_default_system_prompt(personalized_data): #BOS, EOS = "