clam004 commited on
Commit
a605ac7
1 Parent(s): 0f93377

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +264 -6
app.py CHANGED
@@ -1,9 +1,267 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
 
 
3
 
4
- pipe = pipeline('sentiment-analysis')
5
- text = st.text_area('enter some text!')
 
6
 
7
- if text:
8
- out = pipe(text)
9
- st.json(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import requests
3
+ import time
4
+ from ast import literal_eval
5
+ from datetime import datetime
6
 
7
+ def to_md(text):
8
+ # return text.replace("\n", "<br />")
9
+ return text.replace("\n", "<br />")
10
 
11
+ # @st.cache
12
+ def infer(
13
+ prompt,
14
+ model_name,
15
+ max_new_tokens=10,
16
+ temperature=0.1,
17
+ top_p=1.0,
18
+ top_k=40,
19
+ num_completions=1,
20
+ repetition_penalty=1.0,
21
+ seed=42,
22
+ stop="\n"
23
+ ):
24
+ model_name_map = {
25
+ "GPT-JT-6B-v1": "togethercomputer/GPT-JT-6B-v1",
26
+ }
27
+ max_new_tokens = int(max_new_tokens)
28
+ num_completions = int(num_completions)
29
+ temperature = float(temperature)
30
+ top_p = float(top_p)
31
+ top_k = int(top_k)
32
+ repetition_penalty = float(repetition_penalty)
33
+ stop = stop.split(";")
34
+ seed = seed
35
+
36
+ assert 1 <= max_new_tokens <= 256
37
+ assert 1 <= num_completions <= 5
38
+ assert 0.0 <= temperature <= 10.0
39
+ assert 0.0 <= top_p <= 1.0
40
+ assert 1 <= top_k <= 1000
41
+ assert 0.9 <= repetition_penalty <= 3.0
42
+
43
+ if temperature == 0.0:
44
+ temperature = 0.01
45
+ if prompt=="":
46
+ prompt = " "
47
+ my_post_dict = {
48
+ "model": "togethercomputer/GPT-JT-6B-v1",
49
+ "prompt": prompt,
50
+ "top_p": top_p,
51
+ "top_k": top_k,
52
+ "temperature": temperature,
53
+ "max_tokens": max_new_tokens,
54
+ "repetition_penalty": repetition_penalty,
55
+ "stop": stop,
56
+ }
57
+ print(f"send: {datetime.now()}")
58
+ headers = {
59
+ 'Authorization': "Bearer " + st.secrets["TOGETHER_API_KEY"],
60
+ 'User-Agent': 'GPT-JT HuggingFace Space'
61
+ }
62
+ response = requests.get("https://staging.together.xyz/api/inference", params=my_post_dict, headers=headers).json()
63
+ generated_text = response['output']['choices'][0]['text']
64
+ print(f"recv: {datetime.now()}")
65
+
66
+ for stop_word in stop:
67
+ if stop_word != '' and stop_word in generated_text:
68
+ generated_text = generated_text[:generated_text.find(stop_word)]
69
+
70
+ return generated_text
71
+
72
+
73
+ def set_preset():
74
+ if st.session_state.preset == "Question Answering":
75
+
76
+ st.session_state.prompt = '''
77
+ Please answer the following question:
78
+ Question: What is the capital of Canada?
79
+ Answer: Ottawa
80
+ Question: What is the currency of Switzerland?
81
+ Answer: Swiss franc
82
+ Question: In which country is Wisconsin located?
83
+ Answer:
84
+ '''.strip()
85
+ st.session_state.temperature = "0.0"
86
+ st.session_state.top_p = "1.0"
87
+ st.session_state.max_new_tokens = "5"
88
+ st.session_state.stop = r'\n'
89
+
90
+ elif st.session_state.preset == "Sentiment Analysis":
91
+
92
+ st.session_state.prompt = '''
93
+ Label the tweets as either "positive", "negative", "mixed", or "neutral":
94
+ Tweet: I can say that there isn't anything I would change.
95
+ Label: positive
96
+ Tweet: I'm not sure about this.
97
+ Label: neutral
98
+ Tweet: I liked some parts but I didn't like other parts.
99
+ Label: mixed
100
+ Tweet: I think the background image could have been better.
101
+ Label: negative
102
+ Tweet: I really like it.
103
+ Label:
104
+ '''.strip()
105
+ st.session_state.temperature = "0.0"
106
+ st.session_state.top_p = "1.0"
107
+ st.session_state.max_new_tokens = "2"
108
+ st.session_state.stop = r'\n'
109
+
110
+ elif st.session_state.preset == "Topic Classification":
111
+
112
+ st.session_state.prompt = '''
113
+ Given a news article, classify its topic.
114
+ Possible labels: 1. World 2. Sports 3. Business 4. Sci/Tech
115
+ Article: A nearby star thought to harbor comets and asteroids now appears to be home to planets, too.
116
+ Label: Sci/Tech
117
+ Article: Soaring crude prices plus worries about the economy and the outlook for earnings are expected to hang over the stock market next week during the depth of the summer doldrums.
118
+ Label: Business
119
+ Article: Murtagh a stickler for success Northeastern field hockey coach Cheryl Murtagh doesn't want the glare of the spotlight that shines on her to detract from a team that has been the America East champion for the past three years and has been to the NCAA tournament 13 times.
120
+ Label:
121
+ '''.strip()
122
+ st.session_state.temperature = "0.0"
123
+ st.session_state.top_p = "1.0"
124
+ st.session_state.max_new_tokens = "5"
125
+ st.session_state.stop = r'\n'
126
+
127
+ elif st.session_state.preset == "Paraphrasing":
128
+
129
+ st.session_state.prompt = '''
130
+ Paraphrase the given sentence into a different sentence.
131
+ Input: Can you recommend some upscale restaurants in New York?
132
+ Output: What upscale restaurants do you recommend in New York?
133
+ Input: What are the famous places we should not miss in Paris?
134
+ Output: Recommend some of the best places to visit in Paris?
135
+ Input: Could you recommend some hotels that have cheap price in Zurich?
136
+ Output:
137
+ '''.strip()
138
+ st.session_state.temperature = "0.8"
139
+ st.session_state.top_p = "1.0"
140
+ st.session_state.max_new_tokens = "20"
141
+ st.session_state.stop = r'\n'
142
+
143
+ elif st.session_state.preset == "Text Summarization":
144
+
145
+ st.session_state.prompt = '''
146
+ Given a review from Amazon's food products, the task is to generate a short summary of the given review in the input.
147
+ Input: I have bought several of the Vitality canned dog food products and have found them all to be of good quality. The product looks more like a stew than a processed meat and it smells better. My Labrador is finicky and she appreciates this product better than most.
148
+ Output: Good Quality Dog Food
149
+ Input: Product arrived labeled as Jumbo Salted Peanuts...the peanuts were actually small sized unsalted. Not sure if this was an error or if the vendor intended to represent the product as 'Jumbo'.
150
+ Output: Not as Advertised
151
+ Input: My toddler loves this game to a point where he asks for it. That's a big thing for me. Secondly, no glitching unlike one of their competitors (PlayShifu). Any tech I don’t have to reach out to support for help is a good tech for me. I even enjoy some of the games and activities in this. Overall, this is a product that shows that the developers took their time and made sure people would not be asking for refund. I’ve become bias regarding this product and honestly I look forward to buying more of this company’s stuff. Please keep up the great work.
152
+ Output:
153
+ '''.strip()
154
+ st.session_state.temperature = "0.0"
155
+ st.session_state.top_p = "1.0"
156
+ st.session_state.max_new_tokens = "10"
157
+ st.session_state.stop = r'\n'
158
+
159
+ elif st.session_state.preset == "Word Sense Disambiguation":
160
+
161
+ st.session_state.prompt = '''
162
+ Identify which sense of a word is meant in a given context.
163
+ Context: The river overflowed the bank.
164
+ Word: bank
165
+ Sense: river bank
166
+ Context: A mouse takes much more room than a trackball.
167
+ Word: mouse
168
+ Sense: computer mouse
169
+ Context: The bank will not be accepting cash on Saturdays.
170
+ Word: bank
171
+ Sense: commercial (finance) banks
172
+ Context: Bill killed the project
173
+ Word: kill
174
+ Sense:
175
+ '''.strip()
176
+ st.session_state.temperature = "0.0"
177
+ st.session_state.top_p = "1.0"
178
+ st.session_state.max_new_tokens = "10"
179
+ st.session_state.stop = r'\n'
180
+
181
+ elif st.session_state.preset == "Natural Language Inference":
182
+
183
+ st.session_state.prompt = '''
184
+ Given a pair of sentences, choose whether the two sentences agree (entailment)/disagree (contradiction) with each other.
185
+ Possible labels: 1. entailment 2. contradiction
186
+ Sentence 1: The skier was on the edge of the ramp. Sentence 2: The skier was dressed in winter clothes.
187
+ Label: entailment
188
+ Sentence 1: The boy skated down the staircase railing. Sentence 2: The boy is a newbie skater.
189
+ Label: contradiction
190
+ Sentence 1: Two middle-aged people stand by a golf hole. Sentence 2: A couple riding in a golf cart.
191
+ Label:
192
+ '''.strip()
193
+ st.session_state.temperature = "0.0"
194
+ st.session_state.top_p = "1.0"
195
+ st.session_state.max_new_tokens = "2"
196
+ st.session_state.stop = r'\n'
197
+
198
+ else:
199
+ pass
200
+
201
+
202
+ def main():
203
+
204
+ if 'preset' not in st.session_state:
205
+ st.session_state.preset = "Sentiment Analysis"
206
+ st.session_state.top_k = "40"
207
+ st.session_state.repetition_penalty = "1.0"
208
+ st.session_state.stop = r'\n'
209
+ set_preset()
210
+
211
+ st.title("GPT-JT")
212
+
213
+ col1, col2 = st.columns([1, 2])
214
+
215
+ with col1:
216
+ model_name = st.selectbox("Model", ["GPT-JT-6B-v1"])
217
+
218
+ with col2:
219
+ preset = st.selectbox(
220
+ label="Examples",
221
+ options=('Question Answering', 'Sentiment Analysis',
222
+ "Topic Classification", "Paraphrasing", "Text Summarization",
223
+ "Word Sense Disambiguation", "Natural Language Inference"),
224
+ on_change=set_preset,
225
+ key="preset",
226
+ )
227
+
228
+ col3, col4 = st.columns([1, 5])
229
+
230
+ with col3:
231
+ max_new_tokens = st.text_input('Max new tokens', st.session_state.max_new_tokens)
232
+ temperature = st.text_input('temperature', st.session_state.temperature)
233
+ top_k = st.text_input('top_k', st.session_state.top_k)
234
+ top_p = st.text_input('top_p', st.session_state.top_p)
235
+ # num_completions = st.text_input('num_completions (only the best one will be returend)', "1")
236
+ num_completions = "1"
237
+ repetition_penalty = st.text_input('repetition_penalty', st.session_state.repetition_penalty)
238
+ stop = st.text_input('stop, split by;', st.session_state.stop)
239
+ # seed = st.text_input('seed', "42")
240
+ seed = "42"
241
+
242
+ with col4:
243
+
244
+ prompt_area = st.empty()
245
+ prompt = prompt_area.text_area(
246
+ "Prompt",
247
+ value=st.session_state.prompt,
248
+ max_chars=8000,
249
+ height=500,
250
+ )
251
+
252
+ generated_area = st.empty()
253
+ generated_area.markdown("(Generate here)")
254
+
255
+ button_submit = st.button("Submit")
256
+
257
+ if button_submit:
258
+ generated_area.markdown("<b>" + to_md(prompt) + "</b>", unsafe_allow_html=True)
259
+ report_text = infer(
260
+ prompt, model_name=model_name, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, top_k=top_k,
261
+ num_completions=num_completions, repetition_penalty=repetition_penalty,
262
+ seed=seed, stop=literal_eval("'''"+stop+"'''"),
263
+ )
264
+ generated_area.markdown("<b>" + to_md(prompt) + "</b><mark style='background-color: #cbeacd'>" + to_md(report_text)+"</mark>", unsafe_allow_html=True)
265
+
266
+ if __name__ == '__main__':
267
+ main()