Spaces:
Runtime error
Runtime error
initial commit
Browse files- .ipynb_checkpoints/README-checkpoint.md +33 -0
- .ipynb_checkpoints/app-checkpoint.py +127 -0
- .ipynb_checkpoints/start-checkpoint.sh +10 -0
- README.md +28 -7
- app.py +127 -0
- requirements.txt +4 -0
- start.sh +10 -0
.ipynb_checkpoints/README-checkpoint.md
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Light generator
|
3 |
+
emoji: ๐
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: gray
|
6 |
+
sdk: streamlit
|
7 |
+
app_file: app.py
|
8 |
+
pinned: true
|
9 |
+
---
|
10 |
+
|
11 |
+
# Configuration
|
12 |
+
|
13 |
+
`title`: _string_
|
14 |
+
Display title for the Space
|
15 |
+
|
16 |
+
`emoji`: _string_
|
17 |
+
Space emoji (emoji-only character allowed)
|
18 |
+
|
19 |
+
`colorFrom`: _string_
|
20 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
21 |
+
|
22 |
+
`colorTo`: _string_
|
23 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
24 |
+
|
25 |
+
`sdk`: _string_
|
26 |
+
Can be either `gradio` or `streamlit`
|
27 |
+
|
28 |
+
`app_file`: _string_
|
29 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
30 |
+
Path is relative to the root of the repository.
|
31 |
+
|
32 |
+
`pinned`: _boolean_
|
33 |
+
Whether the Space stays on top of your list.
|
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
|
3 |
+
import argparse
|
4 |
+
import re
|
5 |
+
import os
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
import random
|
9 |
+
import numpy as np
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
12 |
+
import tokenizers
|
13 |
+
|
14 |
+
#os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
15 |
+
|
16 |
+
random.seed(None)
|
17 |
+
suggested_text_list = ['ืืื, ']
|
18 |
+
|
19 |
+
@st.cache(hash_funcs={tokenizers.Tokenizer: id, tokenizers.AddedToken: id})
|
20 |
+
def load_model(model_name):
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
22 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
23 |
+
return model, tokenizer
|
24 |
+
|
25 |
+
def extend(input_text, max_size=20, top_k=50, top_p=0.95):
|
26 |
+
if len(input_text) == 0:
|
27 |
+
input_text = ""
|
28 |
+
|
29 |
+
encoded_prompt = tokenizer.encode(
|
30 |
+
input_text, add_special_tokens=False, return_tensors="pt")
|
31 |
+
|
32 |
+
encoded_prompt = encoded_prompt.to(device)
|
33 |
+
|
34 |
+
if encoded_prompt.size()[-1] == 0:
|
35 |
+
input_ids = None
|
36 |
+
else:
|
37 |
+
input_ids = encoded_prompt
|
38 |
+
|
39 |
+
output_sequences = model.generate(
|
40 |
+
input_ids=input_ids,
|
41 |
+
max_length=max_size + len(encoded_prompt[0]),
|
42 |
+
top_k=top_k,
|
43 |
+
top_p=top_p,
|
44 |
+
do_sample=True,
|
45 |
+
repetition_penalty=25.0,
|
46 |
+
num_return_sequences=1)
|
47 |
+
|
48 |
+
# Remove the batch dimension when returning multiple sequences
|
49 |
+
if len(output_sequences.shape) > 2:
|
50 |
+
output_sequences.squeeze_()
|
51 |
+
|
52 |
+
generated_sequences = []
|
53 |
+
|
54 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
55 |
+
generated_sequence = generated_sequence.tolist()
|
56 |
+
|
57 |
+
# Decode text
|
58 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
59 |
+
|
60 |
+
# Remove all text after the stop token
|
61 |
+
text = text[: text.find(stop_token) if stop_token else None]
|
62 |
+
|
63 |
+
# Remove all text after 3 newlines
|
64 |
+
text = text[: text.find(new_lines) if new_lines else None]
|
65 |
+
|
66 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
67 |
+
total_sequence = (
|
68 |
+
input_text + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
69 |
+
)
|
70 |
+
|
71 |
+
generated_sequences.append(total_sequence)
|
72 |
+
|
73 |
+
parsed_text = total_sequence.replace("<|startoftext|>", "").replace("\r","").replace("\n\n", "\n")
|
74 |
+
if len(parsed_text) == 0:
|
75 |
+
parsed_text = "ืฉืืืื"
|
76 |
+
return parsed_text
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
st.title("Light generator")
|
80 |
+
pre_model_path = "orendar/light_generator"
|
81 |
+
model, tokenizer = load_model(pre_model_path)
|
82 |
+
|
83 |
+
stop_token = "<|endoftext|>"
|
84 |
+
new_lines = "\n\n\n"
|
85 |
+
|
86 |
+
np.random.seed(None)
|
87 |
+
random_seed = np.random.randint(10000,size=1)
|
88 |
+
|
89 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
90 |
+
n_gpu = 0 if torch.cuda.is_available()==False else torch.cuda.device_count()
|
91 |
+
|
92 |
+
torch.manual_seed(random_seed)
|
93 |
+
if n_gpu > 0:
|
94 |
+
torch.cuda.manual_seed_all(random_seed)
|
95 |
+
|
96 |
+
model.to(device)
|
97 |
+
|
98 |
+
text_area = st.text_area("Enter the first few words (or leave blank), tap on \"Generate Text\" below. Tapping again will produce a different result.", 'ืืื, ')
|
99 |
+
|
100 |
+
st.sidebar.subheader("Configurable parameters")
|
101 |
+
|
102 |
+
max_len = st.sidebar.slider("Max-Length", 0, 256, 192,help="The maximum length of the sequence to be generated.")
|
103 |
+
top_k = st.sidebar.slider("Top-K", 0, 100, 40, help="The number of highest probability vocabulary tokens to keep for top-k-filtering.")
|
104 |
+
top_p = st.sidebar.slider("Top-P", 0.0, 1.0, 0.92, help="If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation.")
|
105 |
+
|
106 |
+
if st.button("Generate Text"):
|
107 |
+
with st.spinner(text="Generating results..."):
|
108 |
+
st.subheader("Result")
|
109 |
+
print(f"device:{device}, n_gpu:{n_gpu}, random_seed:{random_seed}, maxlen:{max_len}, top_k:{top_k}, top_p:{top_p}")
|
110 |
+
if len(text_area.strip()) == 0:
|
111 |
+
text_area = random.choice(suggested_text_list)
|
112 |
+
result = extend(input_text=text_area,
|
113 |
+
max_size=int(max_len),
|
114 |
+
top_k=int(top_k),
|
115 |
+
top_p=float(top_p))
|
116 |
+
|
117 |
+
print("Done length: " + str(len(result)) + " bytes")
|
118 |
+
#<div class="rtl" dir="rtl" style="text-align:right;">
|
119 |
+
st.markdown(f"<p dir=\"rtl\" style=\"text-align:right;\"> {result} </p>", unsafe_allow_html=True)
|
120 |
+
st.write("\n\nResult length: " + str(len(result)) + " bytes")
|
121 |
+
print(f"\"{result}\"")
|
122 |
+
|
123 |
+
st.markdown(
|
124 |
+
"""This model was trained on archive materials."""
|
125 |
+
)
|
126 |
+
|
127 |
+
st.markdown("<footer><hr><p style=\"font-size:14px\">Enjoy the light.</p><p style=\"font-size:12px\">Created by Oren Dar. Many thanks to Norod78 for providing the base model and the Spaces example!</a></p></footer> ", unsafe_allow_html=True)
|
.ipynb_checkpoints/start-checkpoint.sh
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env bash
|
2 |
+
set -e
|
3 |
+
|
4 |
+
if [ "$DEBUG" = true ] ; then
|
5 |
+
echo 'Debugging - ON'
|
6 |
+
nodemon --exec streamlit run app.py
|
7 |
+
else
|
8 |
+
echo 'Debugging - OFF'
|
9 |
+
streamlit run app.py
|
10 |
+
fi
|
README.md
CHANGED
@@ -1,12 +1,33 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.2.0
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Light generator
|
3 |
+
emoji: ๐
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: gray
|
6 |
sdk: streamlit
|
|
|
7 |
app_file: app.py
|
8 |
+
pinned: true
|
9 |
---
|
10 |
|
11 |
+
# Configuration
|
12 |
+
|
13 |
+
`title`: _string_
|
14 |
+
Display title for the Space
|
15 |
+
|
16 |
+
`emoji`: _string_
|
17 |
+
Space emoji (emoji-only character allowed)
|
18 |
+
|
19 |
+
`colorFrom`: _string_
|
20 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
21 |
+
|
22 |
+
`colorTo`: _string_
|
23 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
24 |
+
|
25 |
+
`sdk`: _string_
|
26 |
+
Can be either `gradio` or `streamlit`
|
27 |
+
|
28 |
+
`app_file`: _string_
|
29 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
30 |
+
Path is relative to the root of the repository.
|
31 |
+
|
32 |
+
`pinned`: _boolean_
|
33 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
|
3 |
+
import argparse
|
4 |
+
import re
|
5 |
+
import os
|
6 |
+
|
7 |
+
import streamlit as st
|
8 |
+
import random
|
9 |
+
import numpy as np
|
10 |
+
import torch
|
11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
12 |
+
import tokenizers
|
13 |
+
|
14 |
+
#os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
15 |
+
|
16 |
+
random.seed(None)
|
17 |
+
suggested_text_list = ['ืืื, ']
|
18 |
+
|
19 |
+
@st.cache(hash_funcs={tokenizers.Tokenizer: id, tokenizers.AddedToken: id})
|
20 |
+
def load_model(model_name):
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
22 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
23 |
+
return model, tokenizer
|
24 |
+
|
25 |
+
def extend(input_text, max_size=20, top_k=50, top_p=0.95):
|
26 |
+
if len(input_text) == 0:
|
27 |
+
input_text = ""
|
28 |
+
|
29 |
+
encoded_prompt = tokenizer.encode(
|
30 |
+
input_text, add_special_tokens=False, return_tensors="pt")
|
31 |
+
|
32 |
+
encoded_prompt = encoded_prompt.to(device)
|
33 |
+
|
34 |
+
if encoded_prompt.size()[-1] == 0:
|
35 |
+
input_ids = None
|
36 |
+
else:
|
37 |
+
input_ids = encoded_prompt
|
38 |
+
|
39 |
+
output_sequences = model.generate(
|
40 |
+
input_ids=input_ids,
|
41 |
+
max_length=max_size + len(encoded_prompt[0]),
|
42 |
+
top_k=top_k,
|
43 |
+
top_p=top_p,
|
44 |
+
do_sample=True,
|
45 |
+
repetition_penalty=25.0,
|
46 |
+
num_return_sequences=1)
|
47 |
+
|
48 |
+
# Remove the batch dimension when returning multiple sequences
|
49 |
+
if len(output_sequences.shape) > 2:
|
50 |
+
output_sequences.squeeze_()
|
51 |
+
|
52 |
+
generated_sequences = []
|
53 |
+
|
54 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
55 |
+
generated_sequence = generated_sequence.tolist()
|
56 |
+
|
57 |
+
# Decode text
|
58 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
59 |
+
|
60 |
+
# Remove all text after the stop token
|
61 |
+
text = text[: text.find(stop_token) if stop_token else None]
|
62 |
+
|
63 |
+
# Remove all text after 3 newlines
|
64 |
+
text = text[: text.find(new_lines) if new_lines else None]
|
65 |
+
|
66 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
67 |
+
total_sequence = (
|
68 |
+
input_text + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
69 |
+
)
|
70 |
+
|
71 |
+
generated_sequences.append(total_sequence)
|
72 |
+
|
73 |
+
parsed_text = total_sequence.replace("<|startoftext|>", "").replace("\r","").replace("\n\n", "\n")
|
74 |
+
if len(parsed_text) == 0:
|
75 |
+
parsed_text = "ืฉืืืื"
|
76 |
+
return parsed_text
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
st.title("Light generator")
|
80 |
+
pre_model_path = "orendar/light_generator"
|
81 |
+
model, tokenizer = load_model(pre_model_path)
|
82 |
+
|
83 |
+
stop_token = "<|endoftext|>"
|
84 |
+
new_lines = "\n\n\n"
|
85 |
+
|
86 |
+
np.random.seed(None)
|
87 |
+
random_seed = np.random.randint(10000,size=1)
|
88 |
+
|
89 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
90 |
+
n_gpu = 0 if torch.cuda.is_available()==False else torch.cuda.device_count()
|
91 |
+
|
92 |
+
torch.manual_seed(random_seed)
|
93 |
+
if n_gpu > 0:
|
94 |
+
torch.cuda.manual_seed_all(random_seed)
|
95 |
+
|
96 |
+
model.to(device)
|
97 |
+
|
98 |
+
text_area = st.text_area("Enter the first few words (or leave blank), tap on \"Generate Text\" below. Tapping again will produce a different result.", 'ืืื, ')
|
99 |
+
|
100 |
+
st.sidebar.subheader("Configurable parameters")
|
101 |
+
|
102 |
+
max_len = st.sidebar.slider("Max-Length", 0, 256, 192,help="The maximum length of the sequence to be generated.")
|
103 |
+
top_k = st.sidebar.slider("Top-K", 0, 100, 40, help="The number of highest probability vocabulary tokens to keep for top-k-filtering.")
|
104 |
+
top_p = st.sidebar.slider("Top-P", 0.0, 1.0, 0.92, help="If set to float < 1, only the most probable tokens with probabilities that add up to top_p or higher are kept for generation.")
|
105 |
+
|
106 |
+
if st.button("Generate Text"):
|
107 |
+
with st.spinner(text="Generating results..."):
|
108 |
+
st.subheader("Result")
|
109 |
+
print(f"device:{device}, n_gpu:{n_gpu}, random_seed:{random_seed}, maxlen:{max_len}, top_k:{top_k}, top_p:{top_p}")
|
110 |
+
if len(text_area.strip()) == 0:
|
111 |
+
text_area = random.choice(suggested_text_list)
|
112 |
+
result = extend(input_text=text_area,
|
113 |
+
max_size=int(max_len),
|
114 |
+
top_k=int(top_k),
|
115 |
+
top_p=float(top_p))
|
116 |
+
|
117 |
+
print("Done length: " + str(len(result)) + " bytes")
|
118 |
+
#<div class="rtl" dir="rtl" style="text-align:right;">
|
119 |
+
st.markdown(f"<p dir=\"rtl\" style=\"text-align:right;\"> {result} </p>", unsafe_allow_html=True)
|
120 |
+
st.write("\n\nResult length: " + str(len(result)) + " bytes")
|
121 |
+
print(f"\"{result}\"")
|
122 |
+
|
123 |
+
st.markdown(
|
124 |
+
"""This model was trained on archive materials."""
|
125 |
+
)
|
126 |
+
|
127 |
+
st.markdown("<footer><hr><p style=\"font-size:14px\">Enjoy the light.</p><p style=\"font-size:12px\">Created by Oren Dar. Many thanks to Norod78 for providing the base model and the Spaces example!</a></p></footer> ", unsafe_allow_html=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
transformers
|
3 |
+
tokenizers
|
4 |
+
torch
|
start.sh
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env bash
|
2 |
+
set -e
|
3 |
+
|
4 |
+
if [ "$DEBUG" = true ] ; then
|
5 |
+
echo 'Debugging - ON'
|
6 |
+
nodemon --exec streamlit run app.py
|
7 |
+
else
|
8 |
+
echo 'Debugging - OFF'
|
9 |
+
streamlit run app.py
|
10 |
+
fi
|