lewtun HF staff commited on
Commit
8d1bd17
β€’
1 Parent(s): f0cf02f
Files changed (3) hide show
  1. .gitignore +4 -1
  2. README.md +12 -1
  3. app.py +45 -19
.gitignore CHANGED
@@ -157,4 +157,7 @@ cython_debug/
157
  # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
  # and can be added to the global gitignore or merged into this file. For a more nuclear
159
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
- #.idea/
 
 
 
 
157
  # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
  # and can be added to the global gitignore or merged into this file. For a more nuclear
159
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ # Local development
163
+ data/
README.md CHANGED
@@ -14,6 +14,7 @@ A basic example of an RLHF interface with a Gradio app.
14
  **Instructions for someone to use for their own project:**
15
 
16
  *Setting up the Space*
 
17
  1. Clone this repo and deploy it on your own Hugging Face space.
18
  2. Add the following secrets to your space:
19
  - `HF_TOKEN`: One of your Hugging Face tokens.
@@ -24,11 +25,21 @@ A basic example of an RLHF interface with a Gradio app.
24
  huggingface.co, the app will use your token to automatically store new HITs
25
  in your dataset. Setting `FORCE_PUSH` to "yes" ensures that your repo will
26
  force push changes to the dataset during data collection. Otherwise,
27
- accidental manual changes to your dataset could result in your space gettin
28
  merge conflicts as it automatically tries to push the dataset to the hub. For
29
  local development, add these three keys to a `.env` file, and consider setting
30
  `FORCE_PUSH` to "no".
 
 
 
 
 
 
 
 
 
31
  *Running Data Collection*
 
32
  1. On your local repo that you pulled, create a copy of `config.py.example`,
33
  just called `config.py`. Now, put keys from your AWS account in `config.py`.
34
  These keys should be for an AWS account that has the
 
14
  **Instructions for someone to use for their own project:**
15
 
16
  *Setting up the Space*
17
+
18
  1. Clone this repo and deploy it on your own Hugging Face space.
19
  2. Add the following secrets to your space:
20
  - `HF_TOKEN`: One of your Hugging Face tokens.
 
25
  huggingface.co, the app will use your token to automatically store new HITs
26
  in your dataset. Setting `FORCE_PUSH` to "yes" ensures that your repo will
27
  force push changes to the dataset during data collection. Otherwise,
28
+ accidental manual changes to your dataset could result in your space getting
29
  merge conflicts as it automatically tries to push the dataset to the hub. For
30
  local development, add these three keys to a `.env` file, and consider setting
31
  `FORCE_PUSH` to "no".
32
+
33
+ To launch the Space locally, run:
34
+
35
+ ```bash
36
+ python app.py
37
+ ```
38
+
39
+ The app will then be available at http://127.0.0.1:7860
40
+
41
  *Running Data Collection*
42
+
43
  1. On your local repo that you pulled, create a copy of `config.py.example`,
44
  just called `config.py`. Now, put keys from your AWS account in `config.py`.
45
  These keys should be for an AWS account that has the
app.py CHANGED
@@ -1,20 +1,21 @@
1
  # Basic example for doing model-in-the-loop dynamic adversarial data collection
2
  # using Gradio Blocks.
 
3
  import os
 
4
  import uuid
 
5
  from urllib.parse import parse_qs
 
6
  import gradio as gr
7
- from huggingface_hub import Repository
8
  from dotenv import load_dotenv
9
- from pathlib import Path
10
- import json
11
- from utils import force_git_push
12
- import threading
13
-
14
- from langchain.prompts import load_prompt
15
- from langchain import LLMChain, PromptTemplate
16
- from langchain.llms import HuggingFaceHub
17
  from langchain.chains.conversation.memory import ConversationBufferMemory
 
 
 
 
18
 
19
  # These variables are for storing the mturk HITs in a Hugging Face dataset.
20
  if Path(".env").is_file():
@@ -22,10 +23,8 @@ if Path(".env").is_file():
22
  DATASET_REPO_URL = os.getenv("DATASET_REPO_URL")
23
  FORCE_PUSH = os.getenv("FORCE_PUSH")
24
  HF_TOKEN = os.getenv("HF_TOKEN")
25
-
26
  PROMPT_TEMPLATES = Path("prompt_templates")
27
-
28
- # Set env variable for langchain
29
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
30
 
31
  DATA_FILENAME = "data.jsonl"
@@ -59,26 +58,45 @@ asynchronous_push(f_stop)
59
  # Now let's run the app!
60
  prompt = load_prompt(PROMPT_TEMPLATES / "openai_chatgpt.json")
61
 
62
- chatbot_1 = LLMChain(
63
  llm=HuggingFaceHub(
64
  repo_id="google/flan-t5-xl",
65
- model_kwargs={"temperature": 1, "do_sample":True, "top_p":"0.8"}
66
  ),
67
  prompt=prompt,
68
  verbose=False,
69
  memory=ConversationBufferMemory(ai_prefix="Assistant"),
70
  )
71
 
72
- chatbot_2 = LLMChain(
73
  llm=HuggingFaceHub(
74
  repo_id="bigscience/bloom",
75
- model_kwargs={"temperature": 1, "do_sample":True, "top_p":"0.8"}
76
  ),
77
  prompt=prompt,
78
  verbose=False,
79
  memory=ConversationBufferMemory(ai_prefix="Assistant"),
80
  )
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  demo = gr.Blocks()
84
 
@@ -94,6 +112,8 @@ with demo:
94
  "generated_responses": [],
95
  "response_1": "",
96
  "response_2": "",
 
 
97
  }
98
  state = gr.JSON(state_dict, visible=False)
99
 
@@ -104,21 +124,27 @@ with demo:
104
 
105
  # Generate model prediction
106
  def _predict(txt, state):
107
- response2model = {}
108
  response_1 = chatbot_1.predict(input=txt)
109
  response_2 = chatbot_2.predict(input=txt)
 
 
 
 
110
  response2model[response_1] = chatbot_1.llm.repo_id
111
  response2model[response_2] = chatbot_2.llm.repo_id
 
 
112
 
113
  state["cnt"] += 1
114
 
115
  new_state_md = f"Inputs remaining in HIT: {state['cnt']}/{TOTAL_CNT}"
116
 
117
- state["data"].append({"cnt": state["cnt"], "text": txt, "response_1": response_1, "response_2": response_2, "response2model": response2model})
118
  state["past_user_inputs"].append(txt)
119
 
120
  past_conversation_string = "<br />".join(["<br />".join(["πŸ˜ƒ: " + user_input, "πŸ€–: " + model_response]) for user_input, model_response in zip(state["past_user_inputs"], state["generated_responses"] + [""])])
121
- return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True, choices=[response_1, response_2], interactive=True, value=response_1), gr.update(value=past_conversation_string), state, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), new_state_md, dummy
122
 
123
  def _select_response(selected_response, state, dummy):
124
  done = state["cnt"] == TOTAL_CNT
 
1
  # Basic example for doing model-in-the-loop dynamic adversarial data collection
2
  # using Gradio Blocks.
3
+ import json
4
  import os
5
+ import threading
6
  import uuid
7
+ from pathlib import Path
8
  from urllib.parse import parse_qs
9
+
10
  import gradio as gr
 
11
  from dotenv import load_dotenv
12
+ from huggingface_hub import Repository
13
+ from langchain import ConversationChain
 
 
 
 
 
 
14
  from langchain.chains.conversation.memory import ConversationBufferMemory
15
+ from langchain.llms import HuggingFaceHub
16
+ from langchain.prompts import load_prompt
17
+
18
+ from utils import force_git_push
19
 
20
  # These variables are for storing the mturk HITs in a Hugging Face dataset.
21
  if Path(".env").is_file():
 
23
  DATASET_REPO_URL = os.getenv("DATASET_REPO_URL")
24
  FORCE_PUSH = os.getenv("FORCE_PUSH")
25
  HF_TOKEN = os.getenv("HF_TOKEN")
 
26
  PROMPT_TEMPLATES = Path("prompt_templates")
27
+ # Set env variable for langchain to communicate with Hugging Face Hub
 
28
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = HF_TOKEN
29
 
30
  DATA_FILENAME = "data.jsonl"
 
58
  # Now let's run the app!
59
  prompt = load_prompt(PROMPT_TEMPLATES / "openai_chatgpt.json")
60
 
61
+ chatbot_1 = ConversationChain(
62
  llm=HuggingFaceHub(
63
  repo_id="google/flan-t5-xl",
64
+ model_kwargs={"temperature": 1}
65
  ),
66
  prompt=prompt,
67
  verbose=False,
68
  memory=ConversationBufferMemory(ai_prefix="Assistant"),
69
  )
70
 
71
+ chatbot_2 = ConversationChain(
72
  llm=HuggingFaceHub(
73
  repo_id="bigscience/bloom",
74
+ model_kwargs={"temperature": 0.7}
75
  ),
76
  prompt=prompt,
77
  verbose=False,
78
  memory=ConversationBufferMemory(ai_prefix="Assistant"),
79
  )
80
 
81
+ chatbot_3 = ConversationChain(
82
+ llm=HuggingFaceHub(
83
+ repo_id="bigscience/T0_3B",
84
+ model_kwargs={"temperature": 1}
85
+ ),
86
+ prompt=prompt,
87
+ verbose=False,
88
+ memory=ConversationBufferMemory(ai_prefix="Assistant"),
89
+ )
90
+
91
+ chatbot_4 = ConversationChain(
92
+ llm=HuggingFaceHub(
93
+ repo_id="EleutherAI/gpt-j-6B",
94
+ model_kwargs={"temperature": 1}
95
+ ),
96
+ prompt=prompt,
97
+ verbose=False,
98
+ memory=ConversationBufferMemory(ai_prefix="Assistant"),
99
+ )
100
 
101
  demo = gr.Blocks()
102
 
 
112
  "generated_responses": [],
113
  "response_1": "",
114
  "response_2": "",
115
+ "response_3": "",
116
+ "response_4": "",
117
  }
118
  state = gr.JSON(state_dict, visible=False)
119
 
 
124
 
125
  # Generate model prediction
126
  def _predict(txt, state):
127
+ # TODO: parallelize this!
128
  response_1 = chatbot_1.predict(input=txt)
129
  response_2 = chatbot_2.predict(input=txt)
130
+ response_3 = chatbot_3.predict(input=txt)
131
+ response_4 = chatbot_4.predict(input=txt)
132
+
133
+ response2model = {}
134
  response2model[response_1] = chatbot_1.llm.repo_id
135
  response2model[response_2] = chatbot_2.llm.repo_id
136
+ response2model[response_3] = chatbot_3.llm.repo_id
137
+ response2model[response_4] = chatbot_4.llm.repo_id
138
 
139
  state["cnt"] += 1
140
 
141
  new_state_md = f"Inputs remaining in HIT: {state['cnt']}/{TOTAL_CNT}"
142
 
143
+ state["data"].append({"cnt": state["cnt"], "text": txt, "response_1": response_1, "response_2": response_2, "response_3": response_3, "response_4": response_4,"response2model": response2model})
144
  state["past_user_inputs"].append(txt)
145
 
146
  past_conversation_string = "<br />".join(["<br />".join(["πŸ˜ƒ: " + user_input, "πŸ€–: " + model_response]) for user_input, model_response in zip(state["past_user_inputs"], state["generated_responses"] + [""])])
147
+ return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True, choices=[response_1, response_2, response_3, response_4], interactive=True, value=response_1), gr.update(value=past_conversation_string), state, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), new_state_md, dummy
148
 
149
  def _select_response(selected_response, state, dummy):
150
  done = state["cnt"] == TOTAL_CNT