RamAnanth1/human_preference
5
1import gradio as gr2import os3 4import pandas as pd5from datasets import load_dataset6 7 8from transformers import T5ForConditionalGeneration, T5Tokenizer9device = 'cpu' # if you have a GPU10 11tokenizer = T5Tokenizer.from_pretrained('stanfordnlp/SteamSHP-flan-t5-large')12model = T5ForConditionalGeneration.from_pretrained('stanfordnlp/SteamSHP-flan-t5-large').to(device)13 14model_list = [15 'google/flan-t5-xxl',16'bigscience/bloomz-7b1',17'facebook/opt-iml-max-30b',18'allenai/tk-instruct-11b-def-pos']19 20HF_TOKEN = os.getenv("HF_TOKEN")21 22OUTPUTS_DATASET = "HuggingFaceH4/instruction-pilot-outputs-filtered"23 24ds = load_dataset(OUTPUTS_DATASET, split="train", use_auth_token=HF_TOKEN)25 26def process(model_A, model_B):27 sample_ds = ds.shuffle().select(range(1))28 sample = sample_ds[0]29 prompt = sample["prompt"]30 31 df = pd.DataFrame.from_records(sample["filtered_outputs"])32 response_A_df = df[df['model']==model_A]["output"]33 response_B_df= df[df['model']==model_B]["output"]34 35 response_A = response_A_df.values[0]36 response_B = response_B_df.values[0]37 print(response_A)38 39 40 input_text = "POST: "+ prompt+ "\n\n RESPONSE A: "+response_A+"\n\n RESPONSE B: "+response_B+"\n\n Which response is better? RESPONSE"41 x = tokenizer([input_text], return_tensors='pt').input_ids.to(device)42 y = model.generate(x, max_new_tokens=1)43 prefered = tokenizer.batch_decode(y, skip_special_tokens=True)[0]44 result = model_A if prefered == 'A' else model_B45 return prompt,df[df['model'].isin([model_A, model_B])], result46 47title = "Compare Instruction Models to see which one is more helpful"48description = "This app compares the outputs of various open-source, instruction-trained models from a [dataset](https://huggingface.co/datasets/{OUTPUTS_DATASET}) of human demonstrations using the SteamSHP reward model trained on the [Stanford Human Preferences Dataset (SHP)](https://huggingface.co/datasets/stanfordnlp/SHP). Hit the button below to view a few random samples from the generated outputs"49interface = gr.Interface(fn=process, 50 inputs=[gr.Dropdown(choices=model_list, value=model_list[0], label='Model A'),51 gr.Dropdown(choices=model_list, value=model_list[1], label='Model B')],52 outputs=[53 gr.Textbox(label = "Prompt"),54 gr.DataFrame(label = "Model Responses"),55 gr.Textbox(label = "Preferred Option"),56 57 ],58 title=title,59 description = description60 61 )62 63interface.launch(debug=True)