nicholasKluge commited on
Commit
9b402c2
·
1 Parent(s): e77d01e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -14
app.py CHANGED
@@ -1,26 +1,61 @@
1
  import time
2
  import torch
 
3
  import gradio as gr
 
 
 
4
  from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification
5
 
6
- model_id = "nicholasKluge/Aira-2-124M" # "nicholasKluge/Aira-Instruct-124M"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  rewardmodel_id = "nicholasKluge/RewardModel"
8
  toxicitymodel_id = "nicholasKluge/ToxicityModel"
9
 
10
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
11
 
 
12
  model = AutoModelForCausalLM.from_pretrained(model_id)
13
  rewardModel = AutoModelForSequenceClassification.from_pretrained(rewardmodel_id)
14
  toxicityModel = AutoModelForSequenceClassification.from_pretrained(toxicitymodel_id)
15
 
 
16
  model.eval()
17
  rewardModel.eval()
18
  toxicityModel.eval()
19
 
20
- model.to("cpu")
 
21
  rewardModel.to(device)
22
  toxicityModel.to(device)
23
 
 
24
  tokenizer = AutoTokenizer.from_pretrained(model_id)
25
  rewardTokenizer = AutoTokenizer.from_pretrained(rewardmodel_id)
26
  toxiciyTokenizer = AutoTokenizer.from_pretrained(toxicitymodel_id)
@@ -28,29 +63,27 @@ toxiciyTokenizer = AutoTokenizer.from_pretrained(toxicitymodel_id)
28
 
29
  intro = """
30
  ## What is `Aira`?
31
-
32
  [`Aira`](https://huggingface.co/nicholasKluge/Aira-Instruct-124M) is a `chatbot` designed to simulate the way a human (expert) would behave during a round of questions and answers (Q&A). `Aira` has many iterations, from a closed-domain chatbot based on pre-defined rules to an open-domain chatbot achieved via instruction-tuning.
33
-
34
  ## Limitations
35
-
36
  We developed our open-domain conversational chatbots via conditional text generation/instruction-tuning. This approach has a lot of limitations. Even though we can make a chatbot that can answer questions about anything, forcing the model to produce good-quality responses is hard. And by good, we mean **factual** and **nontoxic** text. This leads us to two of the most common problems with generative models used in conversational applications:
37
-
38
  🤥 Generative models can perpetuate the generation of pseudo-informative content, that is, false information that may appear truthful.
39
-
40
  🤬 In certain types of tasks, generative models can produce harmful and discriminatory content inspired by historical stereotypes.
41
-
42
  ## Intended Use
43
-
44
  `Aira` is intended only for academic research. For more information, read our [model card](https://huggingface.co/nicholasKluge/Aira-2-124M) to see how we developed `Aira`.
45
-
46
  ## How this demo works?
47
-
48
  For this demo, we use the lighter model we have trained (`Aira-2-124M`). This demo employs a [`reward model`](https://huggingface.co/nicholasKluge/RewardModel) and a [`toxicity model`](https://huggingface.co/nicholasKluge/ToxicityModel) to evaluate the score of each candidate's response, considering its alignment with the user's message and its level of toxicity. The generation function arranges the candidate responses in order of their reward scores and eliminates any responses deemed toxic or harmful. Subsequently, the generation function returns the candidate response with the highest score that surpasses the safety threshold, or a default message if no safe candidates are identified.
49
  """
50
 
 
 
 
 
 
 
 
 
51
  disclaimer = """
52
  **Disclaimer:** You should use this demo for research purposes only. Moderators do not censor the model output, and the authors do not endorse the opinions generated by this model.
53
-
54
  If you would like to complain about any message produced by `Aira`, please contact [[email protected]](mailto:[email protected]).
55
  """
56
 
@@ -62,6 +95,7 @@ with gr.Blocks(theme='freddyaboulton/dracula_revamped') as demo:
62
  chatbot = gr.Chatbot(label="Aira").style(height=500)
63
  msg = gr.Textbox(label="Write a question or instruction to Aira ...", placeholder="What is the capital of Brazil?")
64
 
 
65
  with gr.Accordion(label="Parameters ⚙️", open=True):
66
  safety = gr.Radio(["On", "Off"], label="Guard Rail 🛡️", value="On", info="Helps prevent the model from generating toxic/harmful content.")
67
  top_k = gr.Slider(minimum=10, maximum=100, value=30, step=5, interactive=True, label="Top-k", info="Controls the number of highest probability tokens to consider for each step.")
@@ -72,12 +106,36 @@ with gr.Blocks(theme='freddyaboulton/dracula_revamped') as demo:
72
  smaple_from = gr.Slider(minimum=2, maximum=10, value=2, step=1, interactive=True, label="Sample From", info="Controls the number of generations that the reward model will sample from.")
73
 
74
  clear = gr.Button("Clear Conversation 🧹")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  gr.Markdown(disclaimer)
76
 
77
  def user(user_message, chat_history):
 
 
 
78
  return gr.update(value=user_message, interactive=True), chat_history + [["👤 " + user_message, None]]
79
 
80
  def generate_response(user_msg, top_p, temperature, top_k, max_length, smaple_from, repetition_penalty, safety, chat_history):
 
 
 
81
 
82
  inputs = tokenizer(tokenizer.bos_token + user_msg + tokenizer.sep_token, return_tensors="pt").to(model.device)
83
 
@@ -146,12 +204,36 @@ with gr.Blocks(theme='freddyaboulton/dracula_revamped') as demo:
146
  time.sleep(0.005)
147
  yield chat_history
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
150
  generate_response, [msg, top_p, temperature, top_k, max_length, smaple_from, repetition_penalty, safety, chatbot], chatbot
151
  )
152
  response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
153
  msg.submit(lambda x: gr.update(value=''), None,[msg])
154
  clear.click(lambda: None, None, chatbot, queue=False)
 
155
 
156
  demo.queue()
157
- demo.launch()
 
1
  import time
2
  import torch
3
+ import joblib
4
  import gradio as gr
5
+ from datasets import load_dataset
6
+ from sklearn.metrics.pairwise import cosine_similarity
7
+ from sklearn.feature_extraction.text import TfidfVectorizer
8
  from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSequenceClassification
9
 
10
+
11
+ # download the instruct-aira-dataset
12
+ dataset = load_dataset("nicholasKluge/instruct-aira-dataset", split='english')
13
+
14
+ # convert the dataset to a pandas dataframe
15
+ df = dataset.to_pandas()
16
+
17
+ # rename the columns
18
+ df.columns = ['Prompt', 'Completion']
19
+
20
+ # add a column to store the cosine similarity
21
+ df['Cosine Similarity'] = None
22
+
23
+ # Load the saved prompt TfidfVectorizer
24
+ prompt_tfidf_vectorizer = joblib.load('prompt_vectorizer.pkl')
25
+
26
+ # load the prompt tfidf_matrix
27
+ prompt_tfidf_matrix = joblib.load('prompt_tfidf_matrix.pkl')
28
+
29
+ # Load the saved completion TfidfVectorizer
30
+ completion_tfidf_vectorizer = joblib.load('completion_vectorizer.pkl')
31
+
32
+ # load the completion tfidf_matrix
33
+ completion_tfidf_matrix = joblib.load('completion_tfidf_matrix.pkl')
34
+
35
+ # specify the model's ids
36
+ model_id = "nicholasKluge/Aira-2-124M"
37
  rewardmodel_id = "nicholasKluge/RewardModel"
38
  toxicitymodel_id = "nicholasKluge/ToxicityModel"
39
 
40
+ # specify the device (cuda if available)
41
+ device = torch.device("cpu") # "cuda" if torch.cuda.is_available() else "cpu"
42
 
43
+ # load the models (chatbot, reward model, toxicity model)
44
  model = AutoModelForCausalLM.from_pretrained(model_id)
45
  rewardModel = AutoModelForSequenceClassification.from_pretrained(rewardmodel_id)
46
  toxicityModel = AutoModelForSequenceClassification.from_pretrained(toxicitymodel_id)
47
 
48
+ # set the models to evaluation mode
49
  model.eval()
50
  rewardModel.eval()
51
  toxicityModel.eval()
52
 
53
+ # set the models to the device
54
+ model.to(device)
55
  rewardModel.to(device)
56
  toxicityModel.to(device)
57
 
58
+ # load the tokenizers
59
  tokenizer = AutoTokenizer.from_pretrained(model_id)
60
  rewardTokenizer = AutoTokenizer.from_pretrained(rewardmodel_id)
61
  toxiciyTokenizer = AutoTokenizer.from_pretrained(toxicitymodel_id)
 
63
 
64
  intro = """
65
  ## What is `Aira`?
 
66
  [`Aira`](https://huggingface.co/nicholasKluge/Aira-Instruct-124M) is a `chatbot` designed to simulate the way a human (expert) would behave during a round of questions and answers (Q&A). `Aira` has many iterations, from a closed-domain chatbot based on pre-defined rules to an open-domain chatbot achieved via instruction-tuning.
 
67
  ## Limitations
 
68
  We developed our open-domain conversational chatbots via conditional text generation/instruction-tuning. This approach has a lot of limitations. Even though we can make a chatbot that can answer questions about anything, forcing the model to produce good-quality responses is hard. And by good, we mean **factual** and **nontoxic** text. This leads us to two of the most common problems with generative models used in conversational applications:
 
69
  🤥 Generative models can perpetuate the generation of pseudo-informative content, that is, false information that may appear truthful.
 
70
  🤬 In certain types of tasks, generative models can produce harmful and discriminatory content inspired by historical stereotypes.
 
71
  ## Intended Use
 
72
  `Aira` is intended only for academic research. For more information, read our [model card](https://huggingface.co/nicholasKluge/Aira-2-124M) to see how we developed `Aira`.
 
73
  ## How this demo works?
 
74
  For this demo, we use the lighter model we have trained (`Aira-2-124M`). This demo employs a [`reward model`](https://huggingface.co/nicholasKluge/RewardModel) and a [`toxicity model`](https://huggingface.co/nicholasKluge/ToxicityModel) to evaluate the score of each candidate's response, considering its alignment with the user's message and its level of toxicity. The generation function arranges the candidate responses in order of their reward scores and eliminates any responses deemed toxic or harmful. Subsequently, the generation function returns the candidate response with the highest score that surpasses the safety threshold, or a default message if no safe candidates are identified.
75
  """
76
 
77
+ search_intro ="""
78
+ <h2><center>Explore Aira's Dataset 🔍</h2></center>
79
+
80
+ Here, users can look for instances in Aira's fine-tuning dataset where a given prompt or completion resembles an instruction in its dataset. To enable a fast search, we use the Term Frequency-Inverse Document Frequency (TF-IDF) representation and cosine similarity to explore the dataset. The pre-trained TF-IDF vectorizers and corresponding TF-IDF matrices are available in this repository. Below, we present the top five most similar instances in Aira's dataset for every search query.
81
+
82
+ Users can use this to explore how the model interpolates on the fine-tuning data and if it is capable of following instructions that are out of the fine-tuning distribution.
83
+ """
84
+
85
  disclaimer = """
86
  **Disclaimer:** You should use this demo for research purposes only. Moderators do not censor the model output, and the authors do not endorse the opinions generated by this model.
 
87
  If you would like to complain about any message produced by `Aira`, please contact [[email protected]](mailto:[email protected]).
88
  """
89
 
 
95
  chatbot = gr.Chatbot(label="Aira").style(height=500)
96
  msg = gr.Textbox(label="Write a question or instruction to Aira ...", placeholder="What is the capital of Brazil?")
97
 
98
+ # Parameters to control the generation
99
  with gr.Accordion(label="Parameters ⚙️", open=True):
100
  safety = gr.Radio(["On", "Off"], label="Guard Rail 🛡️", value="On", info="Helps prevent the model from generating toxic/harmful content.")
101
  top_k = gr.Slider(minimum=10, maximum=100, value=30, step=5, interactive=True, label="Top-k", info="Controls the number of highest probability tokens to consider for each step.")
 
106
  smaple_from = gr.Slider(minimum=2, maximum=10, value=2, step=1, interactive=True, label="Sample From", info="Controls the number of generations that the reward model will sample from.")
107
 
108
  clear = gr.Button("Clear Conversation 🧹")
109
+
110
+ gr.Markdown(search_intro)
111
+
112
+ search_input = gr.Textbox(label="Paste here the prompt or completion you would like to search ...", placeholder="What is your name?")
113
+ search_field = gr.Radio(['Prompt', 'Completion'], label="Dataset Column", value='Prompt')
114
+ submit = gr.Button(value="Search")
115
+
116
+ with gr.Row():
117
+ out_dataframe = gr.Dataframe(
118
+ headers=df.columns.tolist(),
119
+ datatype=["str", "str", "str"],
120
+ max_rows=5,
121
+ col_count=(3, "fixed"),
122
+ overflow_row_behaviour='paginate',
123
+ wrap=True,
124
+ interactive=False
125
+ )
126
+
127
  gr.Markdown(disclaimer)
128
 
129
  def user(user_message, chat_history):
130
+ """
131
+ Chatbot's user message handler.
132
+ """
133
  return gr.update(value=user_message, interactive=True), chat_history + [["👤 " + user_message, None]]
134
 
135
  def generate_response(user_msg, top_p, temperature, top_k, max_length, smaple_from, repetition_penalty, safety, chat_history):
136
+ """
137
+ Chatbot's response generator.
138
+ """
139
 
140
  inputs = tokenizer(tokenizer.bos_token + user_msg + tokenizer.sep_token, return_tensors="pt").to(model.device)
141
 
 
204
  time.sleep(0.005)
205
  yield chat_history
206
 
207
+ def search_in_datset(column_name, search_string):
208
+ """
209
+ Search in the dataset for the most similar instances.
210
+ """
211
+ print(column_name, search_string)
212
+ temp_df = df.copy()
213
+
214
+ if column_name == 'Prompt':
215
+ search_vector = prompt_tfidf_vectorizer.transform([search_string])
216
+ cosine_similarities = cosine_similarity(prompt_tfidf_matrix, search_vector)
217
+ temp_df['Cosine Similarity'] = cosine_similarities
218
+ temp_df.sort_values('Cosine Similarity', ascending=False, inplace=True)
219
+ return temp_df.head()
220
+
221
+ elif column_name == 'Completion':
222
+ search_vector = completion_tfidf_vectorizer.transform([search_string])
223
+ cosine_similarities = cosine_similarity(completion_tfidf_matrix, search_vector)
224
+ temp_df['Cosine Similarity'] = cosine_similarities
225
+ temp_df.sort_values('Cosine Similarity', ascending=False, inplace=True)
226
+ return temp_df.head()
227
+
228
+
229
+
230
  response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
231
  generate_response, [msg, top_p, temperature, top_k, max_length, smaple_from, repetition_penalty, safety, chatbot], chatbot
232
  )
233
  response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
234
  msg.submit(lambda x: gr.update(value=''), None,[msg])
235
  clear.click(lambda: None, None, chatbot, queue=False)
236
+ submit.click(fn=search_in_datset, inputs=[search_field, search_input], outputs=out_dataframe)
237
 
238
  demo.queue()
239
+ demo.launch()