Spaces:
Running on Zero
Running on Zero
File size: 6,994 Bytes
6644b47 98d1e65 9cde5e3 1717c0c 9cde5e3 98d1e65 1717c0c 9cde5e3 98d1e65 972b71a 1717c0c 9cde5e3 1717c0c 98d1e65 1717c0c 9cde5e3 972b71a 1717c0c 137f7ef 972b71a 1717c0c 98d1e65 9cde5e3 1717c0c 972b71a 98d1e65 fab4507 98d1e65 6644b47 9cde5e3 6c01195 98d1e65 fab4507 9cde5e3 98d1e65 9cde5e3 fab4507 6c01195 98d1e65 1717c0c 137f7ef 9cde5e3 972b71a 1717c0c 98d1e65 972b71a 98d1e65 1717c0c 98d1e65 9cde5e3 98d1e65 972b71a 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 1717c0c 98d1e65 fab4507 98d1e65 6c01195 75e4c8f 6c01195 98d1e65 fab4507 1717c0c fab4507 0dc1bff 98d1e65 | 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 | import spaces
import gradio as gr
from pathlib import Path
from src.infra.tokenizer import CharacterTokenizer
from src.task.config import ModelConfig
from src.task.model import load_model
from src.task.inference.inference import TextGenerator
from src.task.inference.classify import TextClassifier
from src.task.inference.extract import TextExtractor
# Paths
# DATA_PATH = Path("data/tiny_shakespear.txt")
GENERATOR_MODEL_PATH = Path("checkpoints/tiny_poet.pt")
CLASSIFIER_MODEL_PATH = Path("checkpoints/mh_tiny_text_classifier.pt")
ANSWERING_MODEL_PATH = Path("checkpoints/tiny_answering.pt")
CLASSIFIER_TOKENIZER_PATH = Path("checkpoints/ag_news_tokenizer.json")
TEXT_TOKENIZER_PATH = Path("checkpoints/shakespeare_tokenizer.json")
QA_TOKENIZER_PATH = Path("checkpoints/qa_tokenizer.json")
#A. GENERATOR
#==========================
generator_tokenizer = CharacterTokenizer.load(TEXT_TOKENIZER_PATH)
generator_config = ModelConfig(context_length=128, d_model=128, d_ff=512, num_layers=4, number_of_heads=4)
generator_model = load_model(
checkpoint_path=GENERATOR_MODEL_PATH,
config=generator_config,
vocab_size=generator_tokenizer.vocab_size,
type="GENERATOR"
)
text_generator = TextGenerator(
model=generator_model,
tokenizer=generator_tokenizer,
context_length=generator_config.context_length,
)
# Text Generation
@spaces.GPU
def generate_text(
prompt,
max_new_tokens,
temperature,
top_p,
):
return text_generator.generate(
prompt_text=prompt,
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p
)
# B. CLASSIFIER
#==========================
classifier_tokenizer = CharacterTokenizer.load(CLASSIFIER_TOKENIZER_PATH)
classifier_config = ModelConfig(context_length=384, d_model=64, d_ff=256, num_layers=2)
classifier_model = load_model(
checkpoint_path=CLASSIFIER_MODEL_PATH,
config=classifier_config,
vocab_size=classifier_tokenizer.vocab_size,
type="CLASSIFIER"
)
text_classifier = TextClassifier(
model=classifier_model,
tokenizer=classifier_tokenizer,
context_length=classifier_config.context_length,
)
CLASS_NAMES = [
"World",
"Sports",
"Business",
"Sci/Tech",
]
@spaces.GPU
def classify_text(text):
prediction = text_classifier.classify(text)
return CLASS_NAMES[prediction]
#C. QUESTION ANSWERING
qa_tokenizer = CharacterTokenizer.load(QA_TOKENIZER_PATH)
qa_config = ModelConfig(context_length=512, d_model=64, d_ff=256, num_layers=2)
answering_model = load_model(
checkpoint_path=ANSWERING_MODEL_PATH,
config=qa_config,
vocab_size=qa_tokenizer.vocab_size,
type="ANSWERING"
)
text_extractor = TextExtractor(
model=answering_model,
tokenizer=qa_tokenizer,
context_length=qa_config.context_length,
)
@spaces.GPU
def ask_question(
context,
question
):
answer_text = ""
token_tensor, predicted_start, predicted_end = text_extractor.extract(
context_text=context,
question_text=question
)
if (predicted_start >= predicted_end):
answer_text = "Invalid Answer Found. OOPS"
else:
answer_ids = token_tensor[predicted_start:predicted_end + 1]
answer_text = qa_tokenizer.decode(answer_ids.tolist())
return answer_text
#USER INTERFACE
#==========================
#.
with gr.Blocks() as demo:
gr.Markdown(
"""
# Tiny Text Transformers
A set of tiny character-level Transformers built from first principles using Pytorch.
POCs:- Generation, Classification and Question Answering (extractive).
Generation was trained on Shakespeare text and Tiny Stories,
Classification was trained on AG News (World, Sport, Business and Sci/Tech),
Question Answering was trained on a subset of SQuAD v1.1 dataset (kenya).
NOTE: These are not general purpose models and will only return useful responses within the context of the datasets above.
Goals:
- Build capable but highly specialized models
- Explore limits of micro intelligence
- Fluency in tranformer design (personal)
"""
)
with gr.Tab("Text Generation"):
gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(
label="Prompt",
value="ROMEO:",
),
gr.Slider(
minimum=10,
maximum=500,
value=300,
step=10,
label="Max new tokens",
),
gr.Slider(
minimum=0.1,
maximum=1.5,
value=0.7,
step=0.1,
label="Temperature",
),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.8,
step=0.1,
label="Top P",
),
],
outputs=gr.Textbox(
label="Generated text",
lines=15,
),
)
with gr.Tab("Text Classification"):
gr.Interface(
fn=classify_text,
inputs=gr.Textbox(
label="News text",
value="The newest Mac mini was announced on August to launch in stores on September",
lines=5,
),
outputs=gr.Textbox(
label="Predicted category",
),
)
with gr.Tab("Question Answering"):
gr.Interface(
fn=ask_question,
inputs=[
gr.Textbox(
label="Context",
value="Kenya (/ˈkɛnjə/; locally [ˈkɛɲa] ( listen)), officially the Republic of Kenya, is a country in Africa and a founding member of the East African Community (EAC). Its capital and largest city is Nairobi. Kenya's territory lies on the equator and overlies the East African Rift covering a diverse and expansive terrain that extends roughly from Lake Victoria to Lake Turkana (formerly called Lake Rudolf) and further south-east to the Indian Ocean. It is bordered by Tanzania to the south, Uganda to the west, South Sudan to the north-west, Ethiopia to the north and Somalia to the north-east. Kenya covers 581,309 km2 (224,445 sq mi), and had a population of approximately 45 million people in July 2014.",
lines=5,
),
gr.Textbox(
label="Question",
value="what is the capital of kenya?",
)
],
outputs=gr.Textbox(
label="Predicted answer",
),
)
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
) |