File size: 10,433 Bytes
0d215ca
 
 
e532db6
0a53800
0d215ca
c694998
40403b0
b2d556d
f203ba6
fa25719
7e6795e
 
 
 
2ab9fff
7e6795e
 
 
c5556d8
7e6795e
b2d556d
7e6795e
3442116
af1594d
3442116
7e6795e
 
 
 
652d8ad
c5556d8
7e6795e
 
0d215ca
5ebf2f1
7e6795e
 
 
652d8ad
c5556d8
40403b0
7e6795e
f79758c
663656c
 
f203ba6
af1594d
0a53800
 
 
 
 
c5556d8
0a53800
f203ba6
0a53800
4bde7c0
036a7be
4bde7c0
 
 
0a53800
 
7e6795e
 
de0b08b
7e6795e
 
 
 
5fcdcad
7e6795e
de0b08b
0d215ca
de0b08b
 
7e6795e
de0b08b
 
7e6795e
de0b08b
 
 
 
 
 
7e6795e
 
de0b08b
 
0d215ca
de0b08b
0d215ca
de0b08b
 
 
 
 
 
 
 
7e6795e
de0b08b
 
 
 
 
 
67570b7
de0b08b
 
5e48162
de0b08b
 
 
 
 
 
 
 
 
7e6795e
de0b08b
 
7e6795e
de0b08b
 
7e6795e
de0b08b
 
 
 
 
 
 
 
 
 
 
 
a531b86
761013a
de0b08b
 
 
 
 
761013a
de0b08b
 
 
7e6795e
de0b08b
 
 
 
 
 
 
7e6795e
de0b08b
 
 
 
 
 
06a3cd9
de0b08b
 
 
 
 
 
 
 
7e6795e
de0b08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fcdcad
de0b08b
5fcdcad
de0b08b
7e6795e
de0b08b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5556d8
de0b08b
 
7e6795e
 
0ea48b0
 
 
7e6795e
 
0ea48b0
 
 
 
 
 
 
 
 
 
 
 
 
 
5fcdcad
7e6795e
 
 
 
 
c5556d8
de0b08b
7e6795e
 
 
0ea48b0
7e6795e
5fcdcad
 
7e6795e
 
fa25719
de0b08b
7e6795e
 
 
c694998
7e6795e
 
 
 
c694998
7e6795e
df71374
c5556d8
 
7e6795e
40403b0
7e6795e
 
4bde7c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import streamlit as st
import requests
import time
from ast import literal_eval
from datetime import datetime

def to_md(text):
    # return text.replace("\n", "<br />")
    return text.replace("\n", "<br />")

# @st.cache
def infer(
    prompt, 
    model_name, 
    max_new_tokens=10, 
    temperature=0.1, 
    top_p=1.0,
    top_k=40,
    num_completions=1,
    repetition_penalty=1.0,
    seed=42,
    stop="\n"
):
    model_name_map = {
        "GPT-JT-6B-v1": "togethercomputer/GPT-JT-6B-v1",
    }
    max_new_tokens = int(max_new_tokens)
    num_completions = int(num_completions)
    temperature = float(temperature)
    top_p = float(top_p)
    top_k = int(top_k)
    repetition_penalty = float(repetition_penalty)
    stop = stop.split(";")
    seed = seed
    
    assert 1 <= max_new_tokens <= 256
    assert 1 <= num_completions <= 5
    assert 0.0 <= temperature <= 10.0
    assert 0.0 <= top_p <= 1.0
    assert 1 <= top_k <= 1000
    assert 0.9 <= repetition_penalty <= 3.0

    if temperature == 0.0:
        temperature = 0.01
    if prompt=="":
        prompt = " "
    my_post_dict = {
        "model": "togethercomputer/GPT-JT-6B-v1",
        "prompt": prompt,
        "top_p": top_p,
        "top_k": top_k,
        "temperature": temperature,
        "max_tokens": max_new_tokens,
        "repetition_penalty": repetition_penalty,
        "stop": stop,
    }
    print(f"send: {datetime.now()}")
    headers = {
        'Authorization': "Bearer " + st.secrets["TOGETHER_API_KEY"],
        'User-Agent': 'GPT-JT HuggingFace Space'
    }
    response = requests.get("https://staging.together.xyz/api/inference", params=my_post_dict, headers=headers).json()
    generated_text = response['output']['choices'][0]['text']
    print(f"recv: {datetime.now()}")
    
    for stop_word in stop:
        if stop_word != '' and stop_word in generated_text:
            generated_text = generated_text[:generated_text.find(stop_word)]
    
    return generated_text


def set_preset():
    if st.session_state.preset == "Question Answering":
        
        st.session_state.prompt = '''
Please answer the following question:

Question: What is the capital of Canada?
Answer: Ottawa

Question: What is the currency of Switzerland?
Answer: Swiss franc

Question: In which country is Wisconsin located?
Answer:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "5"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Sentiment Analysis":
        
        st.session_state.prompt = '''
Label the tweets as either "positive", "negative", "mixed", or "neutral":

Tweet: I can say that there isn't anything I would change.
Label: positive

Tweet: I'm not sure about this.
Label: neutral

Tweet: I liked some parts but I didn't like other parts.
Label: mixed

Tweet: I think the background image could have been better.
Label: negative

Tweet: I really like it.
Label:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "2"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Topic Classification":
        
        st.session_state.prompt = '''
Given a news article, classify its topic.
Possible labels: 1. World 2. Sports 3. Business 4. Sci/Tech

Article: A nearby star thought to harbor comets and asteroids now appears to be home to planets, too.
Label: Sci/Tech

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.
Label: Business

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.
Label:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "5"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Paraphrasing":
        
        st.session_state.prompt = '''
Paraphrase the given sentence into a different sentence.

Input: Can you recommend some upscale restaurants in New York?
Output: What upscale restaurants do you recommend in New York?

Input: What are the famous places we should not miss in Paris?
Output: Recommend some of the best places to visit in Paris?

Input: Could you recommend some hotels that have cheap price in Zurich?
Output:
'''.strip()
        st.session_state.temperature = "0.8"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "20"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Text Summarization":
        
        st.session_state.prompt = '''
Given a review from Amazon's food products, the task is to generate a short summary of the given review in the input.

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.
Output: Good Quality Dog Food

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'.
Output: Not as Advertised

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.
Output:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "10"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Word Sense Disambiguation":
        
        st.session_state.prompt = '''
Identify which sense of a word is meant in a given context.

Context: The river overflowed the bank.
Word: bank
Sense: river bank

Context: A mouse takes much more room than a trackball.
Word: mouse
Sense: computer mouse

Context: The bank will not be accepting cash on Saturdays.
Word: bank
Sense: commercial (finance) banks

Context: Bill killed the project
Word: kill
Sense:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "10"
        st.session_state.stop = r'\n'
        
    elif st.session_state.preset == "Natural Language Inference":
        
        st.session_state.prompt = '''
Given a pair of sentences, choose whether the two sentences agree (entailment)/disagree (contradiction) with each other.
Possible labels: 1. entailment 2. contradiction

Sentence 1: The skier was on the edge of the ramp. Sentence 2: The skier was dressed in winter clothes.
Label: entailment

Sentence 1: The boy skated down the staircase railing. Sentence 2: The boy is a newbie skater.
Label: contradiction

Sentence 1: Two middle-aged people stand by a golf hole. Sentence 2: A couple riding in a golf cart.
Label:
'''.strip()
        st.session_state.temperature = "0.0"
        st.session_state.top_p = "1.0"
        st.session_state.max_new_tokens = "2"
        st.session_state.stop = r'\n'
    
    else:
        pass
    
    
def main():

    if 'preset' not in st.session_state:
        st.session_state.preset = "Sentiment Analysis"
        st.session_state.top_k = "40"
        st.session_state.repetition_penalty = "1.0"
        st.session_state.stop = r'\n'
        set_preset()

    st.title("GPT-JT")
    
    col1, col2 = st.columns([1, 2])
    
    with col1:
        model_name = st.selectbox("Model", ["GPT-JT-6B-v1"])
        
    with col2:
        preset = st.selectbox(
            label="Examples", 
            options=('Question Answering', 'Sentiment Analysis', 
                     "Topic Classification", "Paraphrasing", "Text Summarization",
                     "Word Sense Disambiguation", "Natural Language Inference"), 
            on_change=set_preset,
            key="preset",
        )

    col3, col4 = st.columns([1, 5])

    with col3:
        max_new_tokens = st.text_input('Max new tokens', st.session_state.max_new_tokens)
        temperature = st.text_input('temperature', st.session_state.temperature)
        top_k = st.text_input('top_k', st.session_state.top_k)
        top_p = st.text_input('top_p', st.session_state.top_p)
        # num_completions = st.text_input('num_completions (only the best one will be returend)', "1")
        num_completions = "1"
        repetition_penalty = st.text_input('repetition_penalty', st.session_state.repetition_penalty)
        stop = st.text_input('stop, split by;', st.session_state.stop)
        # seed = st.text_input('seed', "42")
        seed = "42"

    with col4:

        prompt_area = st.empty()
        prompt = prompt_area.text_area(
            "Prompt",
            value=st.session_state.prompt,
            max_chars=8000,
            height=500,
        )

        generated_area = st.empty()
        generated_area.markdown("(Generate here)")

        button_submit = st.button("Submit")

        if button_submit:
            generated_area.markdown("<b>" + to_md(prompt) + "</b>", unsafe_allow_html=True)
            report_text = infer(
                prompt, model_name=model_name, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, top_k=top_k,
                num_completions=num_completions, repetition_penalty=repetition_penalty,
                seed=seed, stop=literal_eval("'''"+stop+"'''"),
            )
            generated_area.markdown("<b>" + to_md(prompt) + "</b><mark style='background-color: #cbeacd'>" + to_md(report_text)+"</mark>", unsafe_allow_html=True)
        
if __name__ == '__main__':
    main()