orzhan commited on
Commit
eb91b0e
·
1 Parent(s): c6eaf06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -2
app.py CHANGED
@@ -1,4 +1,67 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
1
  import streamlit as st
2
+ import transformers
3
+ from transformers import AutoTokenizer, AutoModelWithLMHead
4
+
5
+ model_name = "orzhan/rut5-base-detox"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ @st.cache
8
+ def load_model(model_name):
9
+ model = AutoModelWithLMHead.from_pretrained(model_name)
10
+ return model
11
+
12
+ model = load_model(model_name)
13
+
14
+
15
+ def infer(input_ids):
16
+
17
+ output_sequences = model.generate(
18
+ input_ids=input_ids,
19
+ max_length=60,
20
+ do_sample=False,
21
+ num_return_sequences=1,
22
+ num_beams=32,
23
+ length_penalty=2.0,
24
+ no_repeat_ngram_size=4
25
+ )
26
+
27
+ return output_sequences
28
+ default_value = "А ну иди сюда, придурок"
29
+
30
+ #prompts
31
+ st.title("Дело детоксификации на ruT5")
32
+ sent = st.text_area("Text", default_value, height = 275)
33
+
34
+
35
+
36
+ encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
37
+ if encoded_prompt.size()[-1] == 0:
38
+ input_ids = None
39
+ else:
40
+ input_ids = encoded_prompt
41
+
42
+
43
+ output_sequences = infer(input_ids)
44
+
45
+
46
+
47
+ for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
48
+ print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
49
+ generated_sequences = generated_sequence.tolist()
50
+
51
+ # Decode text
52
+ text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
53
+
54
+ # Remove all text after the stop token
55
+ #text = text[: text.find(args.stop_token) if args.stop_token else None]
56
+
57
+ # Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
58
+ total_sequence = (
59
+ sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
60
+ )
61
+
62
+ generated_sequences.append(total_sequence)
63
+ print(total_sequence)
64
+
65
+
66
+ st.write(generated_sequences[-1])
67