codelion merve HF staff commited on
Commit
5666a5c
0 Parent(s):

Duplicate from merve/write-with-transformer

Browse files

Co-authored-by: Merve Noyan <merve@users.noreply.huggingface.co>

Files changed (4) hide show
  1. .gitattributes +27 -0
  2. README.md +38 -0
  3. app.py +71 -0
  4. requirements.txt +3 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bin.* filter=lfs diff=lfs merge=lfs -text
5
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.model filter=lfs diff=lfs merge=lfs -text
12
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
13
+ *.onnx filter=lfs diff=lfs merge=lfs -text
14
+ *.ot filter=lfs diff=lfs merge=lfs -text
15
+ *.parquet filter=lfs diff=lfs merge=lfs -text
16
+ *.pb filter=lfs diff=lfs merge=lfs -text
17
+ *.pt filter=lfs diff=lfs merge=lfs -text
18
+ *.pth filter=lfs diff=lfs merge=lfs -text
19
+ *.rar filter=lfs diff=lfs merge=lfs -text
20
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
21
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
22
+ *.tflite filter=lfs diff=lfs merge=lfs -text
23
+ *.tgz filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Write with Transformers
3
+ emoji: ❤️‍🔥
4
+ colorFrom: purple
5
+ colorTo: red
6
+ sdk: streamlit
7
+ app_file: app.py
8
+ pinned: false
9
+ duplicated_from: merve/write-with-transformer
10
+ ---
11
+
12
+ # Configuration
13
+
14
+ `title`: _string_
15
+ Display title for the Space
16
+
17
+ `emoji`: _string_
18
+ Space emoji (emoji-only character allowed)
19
+
20
+ `colorFrom`: _string_
21
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
22
+
23
+ `colorTo`: _string_
24
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
25
+
26
+ `sdk`: _string_
27
+ Can be either `gradio` or `streamlit`
28
+
29
+ `sdk_version` : _string_
30
+ Only applicable for `streamlit` SDK.
31
+ See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
32
+
33
+ `app_file`: _string_
34
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code).
35
+ Path is relative to the root of the repository.
36
+
37
+ `pinned`: _boolean_
38
+ Whether the Space stays on top of your list.
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ import streamlit as st
3
+
4
+ from transformers import AutoTokenizer, AutoModelWithLMHead
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained("gpt2-large")
7
+ @st.cache
8
+ def load_model(model_name):
9
+ model = AutoModelWithLMHead.from_pretrained("gpt2-large")
10
+ return model
11
+
12
+ model = load_model("gpt2-large")
13
+
14
+ def infer(input_ids, max_length, temperature, top_k, top_p):
15
+
16
+ output_sequences = model.generate(
17
+ input_ids=input_ids,
18
+ max_length=max_length,
19
+ temperature=temperature,
20
+ top_k=top_k,
21
+ top_p=top_p,
22
+ do_sample=True,
23
+ num_return_sequences=1
24
+ )
25
+
26
+ return output_sequences
27
+ default_value = "See how a modern neural network auto-completes your text 🤗 This site, built by the Hugging Face team, lets you write a whole document directly from your browser, and you can trigger the Transformer anywhere using the Tab key. Its like having a smart machine that completes your thoughts 😀 Get started by typing a custom snippet, check out the repository, or try one of the examples. Have fun!"
28
+
29
+ #prompts
30
+ st.title("Write with Transformers 🦄")
31
+ st.write("The almighty king of text generation, GPT-2 comes in four available sizes, only three of which have been publicly made available. Feared for its fake news generation capabilities, it currently stands as the most syntactically coherent model. A direct successor to the original GPT, it reinforces the already established pre-training/fine-tuning killer duo. From the paper: Language Models are Unsupervised Multitask Learners by Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever.")
32
+
33
+ sent = st.text_area("Text", default_value, height = 275)
34
+ max_length = st.sidebar.slider("Max Length", min_value = 10, max_value=30)
35
+ temperature = st.sidebar.slider("Temperature", value = 1.0, min_value = 0.0, max_value=1.0, step=0.05)
36
+ top_k = st.sidebar.slider("Top-k", min_value = 0, max_value=5, value = 0)
37
+ top_p = st.sidebar.slider("Top-p", min_value = 0.0, max_value=1.0, step = 0.05, value = 0.9)
38
+
39
+ encoded_prompt = tokenizer.encode(sent, add_special_tokens=False, return_tensors="pt")
40
+ if encoded_prompt.size()[-1] == 0:
41
+ input_ids = None
42
+ else:
43
+ input_ids = encoded_prompt
44
+
45
+
46
+ output_sequences = infer(input_ids, max_length, temperature, top_k, top_p)
47
+
48
+
49
+
50
+ for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
51
+ print(f"=== GENERATED SEQUENCE {generated_sequence_idx + 1} ===")
52
+ generated_sequences = generated_sequence.tolist()
53
+
54
+ # Decode text
55
+ text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
56
+
57
+ # Remove all text after the stop token
58
+ #text = text[: text.find(args.stop_token) if args.stop_token else None]
59
+
60
+ # Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
61
+ total_sequence = (
62
+ sent + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
63
+ )
64
+
65
+ generated_sequences.append(total_sequence)
66
+ print(total_sequence)
67
+
68
+
69
+ st.write(generated_sequences[-1])
70
+
71
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ streamlit
3
+ torch