Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,96 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import random
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return
|
| 17 |
-
def
|
| 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 |
-
st.
|
| 72 |
-
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
if
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if type(result["error"]) is list:
|
| 97 |
-
for error in result["error"]:
|
| 98 |
-
st.write(f'{error}')
|
| 99 |
-
else:
|
| 100 |
-
print("hey")
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
import streamlit as st
|
| 5 |
import random
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
+
import tokenizers
|
| 10 |
+
#os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 11 |
+
random.seed(None)
|
| 12 |
+
suggested_text_list = ['ืคืขื ืืืช, ืืคื ื ืฉื ืื ืจืืืช','ืฉืืื, ืงืืจืืื ืื ืืืจืื ืืื ื','ืืืงืจ ืืื ืืืืื','ืืื ืืคืจืชื ืืช ืื ืืืื ืืืงืก ืืฉ']
|
| 13 |
+
@st.cache(hash_funcs={tokenizers.Tokenizer: id, tokenizers.AddedToken: id})
|
| 14 |
+
def load_model(model_name):
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 17 |
+
return model, tokenizer
|
| 18 |
+
def extend(input_text, max_size=20, top_k=50, top_p=0.95):
|
| 19 |
+
if len(input_text) == 0:
|
| 20 |
+
input_text = ""
|
| 21 |
+
encoded_prompt = tokenizer.encode(
|
| 22 |
+
input_text, add_special_tokens=False, return_tensors="pt")
|
| 23 |
+
encoded_prompt = encoded_prompt.to(device)
|
| 24 |
+
if encoded_prompt.size()[-1] == 0:
|
| 25 |
+
input_ids = None
|
| 26 |
+
else:
|
| 27 |
+
input_ids = encoded_prompt
|
| 28 |
+
|
| 29 |
+
output_sequences = model.generate(
|
| 30 |
+
input_ids=input_ids,
|
| 31 |
+
max_length=max_size + len(encoded_prompt[0]),
|
| 32 |
+
top_k=top_k,
|
| 33 |
+
top_p=top_p,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
num_return_sequences=1)
|
| 36 |
+
# Remove the batch dimension when returning multiple sequences
|
| 37 |
+
if len(output_sequences.shape) > 2:
|
| 38 |
+
output_sequences.squeeze_()
|
| 39 |
+
generated_sequences = []
|
| 40 |
+
for generated_sequence_idx, generated_sequence in enumerate(output_sequences):
|
| 41 |
+
generated_sequence = generated_sequence.tolist()
|
| 42 |
+
# Decode text
|
| 43 |
+
text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
|
| 44 |
+
# Remove all text after the stop token
|
| 45 |
+
text = text[: text.find(stop_token) if stop_token else None]
|
| 46 |
+
# Remove all text after 3 newlines
|
| 47 |
+
text = text[: text.find(new_lines) if new_lines else None]
|
| 48 |
+
# Add the prompt at the beginning of the sequence. Remove the excess text that was used for pre-processing
|
| 49 |
+
total_sequence = (
|
| 50 |
+
input_text + text[len(tokenizer.decode(encoded_prompt[0], clean_up_tokenization_spaces=True)) :]
|
| 51 |
+
)
|
| 52 |
+
generated_sequences.append(total_sequence)
|
| 53 |
+
|
| 54 |
+
parsed_text = total_sequence.replace("<|startoftext|>", "").replace("\r","").replace("\n\n", "\n")
|
| 55 |
+
if len(parsed_text) == 0:
|
| 56 |
+
parsed_text = "ืฉืืืื"
|
| 57 |
+
return parsed_text
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
st.title("Hebrew GPT Neo (Small)")
|
| 60 |
+
pre_model_path = "Norod78/hebrew-gpt_neo-small"
|
| 61 |
+
model, tokenizer = load_model(pre_model_path)
|
| 62 |
+
stop_token = "<|endoftext|>"
|
| 63 |
+
new_lines = "\n\n\n"
|
| 64 |
+
np.random.seed(None)
|
| 65 |
+
random_seed = np.random.randint(10000,size=1)
|
| 66 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 67 |
+
n_gpu = 0 if torch.cuda.is_available()==False else torch.cuda.device_count()
|
| 68 |
+
torch.manual_seed(random_seed)
|
| 69 |
+
if n_gpu > 0:
|
| 70 |
+
torch.cuda.manual_seed_all(random_seed)
|
| 71 |
+
model.to(device)
|
| 72 |
+
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.", 'ืืืืฉ ืืืืจืื ืืขืืื ืืฉื ืืื ืืืืจื ืืฉืืคืชืข ื ืฉืืขื ื ืงืืฉื')
|
| 73 |
+
st.sidebar.subheader("Configurable parameters")
|
| 74 |
+
max_len = st.sidebar.slider("Max-Length", 0, 256, 192,help="The maximum length of the sequence to be generated.")
|
| 75 |
+
top_k = st.sidebar.slider("Top-K", 0, 100, 40, help="The number of highest probability vocabulary tokens to keep for top-k-filtering.")
|
| 76 |
+
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.")
|
| 77 |
+
if st.button("Generate Text"):
|
| 78 |
+
with st.spinner(text="Generating results..."):
|
| 79 |
+
st.subheader("Result")
|
| 80 |
+
print(f"device:{device}, n_gpu:{n_gpu}, random_seed:{random_seed}, maxlen:{max_len}, top_k:{top_k}, top_p:{top_p}")
|
| 81 |
+
if len(text_area.strip()) == 0:
|
| 82 |
+
text_area = random.choice(suggested_text_list)
|
| 83 |
+
result = extend(input_text=text_area,
|
| 84 |
+
max_size=int(max_len),
|
| 85 |
+
top_k=int(top_k),
|
| 86 |
+
top_p=float(top_p))
|
| 87 |
+
print("Done length: " + str(len(result)) + " bytes")
|
| 88 |
+
#<div class="rtl" dir="rtl" style="text-align:right;">
|
| 89 |
+
st.markdown(f"<p dir=\"rtl\" style=\"text-align:right;\"> {result} </p>", unsafe_allow_html=True)
|
| 90 |
+
st.write("\n\nResult length: " + str(len(result)) + " bytes")
|
| 91 |
+
print(f"\"{result}\"")
|
| 92 |
+
|
| 93 |
+
st.markdown(
|
| 94 |
+
"""Hebrew text generation model (125M parameters) based on EleutherAI's gpt-neo architecture. Originally trained on a TPUv3-8 which was made avilable to me via the [TPU Research Cloud Program](https://sites.research.google/trc/)."""
|
| 95 |
+
)
|
| 96 |
+
st.markdown("<footer><hr><p style=\"font-size:14px\">Enjoy</p><p style=\"font-size:12px\">Created by <a href=\"https://linktr.ee/Norod78\">Doron Adler</a></p></footer> ", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|