Spaces:
Runtime error
Runtime error
Commit
·
1fcd3cd
1
Parent(s):
a555e1b
deployment files added
Browse files- README.md +1 -0
- app.py +52 -0
- checkpoint/config.json +39 -0
- checkpoint/generation_config.json +6 -0
- checkpoint/model.safetensors +3 -0
- checkpoint/optimizer.pt +3 -0
- checkpoint/rng_state.pth +3 -0
- checkpoint/scheduler.pt +3 -0
- checkpoint/trainer_state.json +37 -0
- checkpoint/training_args.bin +3 -0
- dockerfile +14 -0
- fastapi.py +48 -0
- requirements.txt +0 -0
README.md
CHANGED
@@ -6,6 +6,7 @@ colorTo: blue
|
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: mit
|
|
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
license: mit
|
9 |
+
app_port:8081
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from transformers import LineByLineTextDataset
|
4 |
+
from transformers import DataCollatorForLanguageModeling
|
5 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
6 |
+
from transformers import Trainer, TrainingArguments
|
7 |
+
|
8 |
+
def load_model(model_path):
|
9 |
+
model = GPT2LMHeadModel.from_pretrained(model_path)
|
10 |
+
return model
|
11 |
+
|
12 |
+
|
13 |
+
def load_tokenizer(tokenizer_path):
|
14 |
+
tokenizer = GPT2Tokenizer.from_pretrained(tokenizer_path)
|
15 |
+
return tokenizer
|
16 |
+
|
17 |
+
|
18 |
+
def generate_text(sequence, max_new_tokens):
|
19 |
+
ids = tokenizer.encode(f'{sequence}', return_tensors='pt')
|
20 |
+
input_length = ids.size(1)
|
21 |
+
max_length = input_length + max_new_tokens
|
22 |
+
final_outputs = model.generate(
|
23 |
+
ids,
|
24 |
+
do_sample=True,
|
25 |
+
max_length=max_length,
|
26 |
+
pad_token_id=model.config.eos_token_id
|
27 |
+
)
|
28 |
+
return tokenizer.decode(final_outputs[0], skip_special_tokens=True)
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
model_path = r'C:\Users\Suraj Singh\aiml_gr16\checkpoint'
|
34 |
+
model = load_model(model_path)
|
35 |
+
tokenizer = load_tokenizer(model_path)
|
36 |
+
|
37 |
+
|
38 |
+
def generate_text(sequence, max_new_tokens):
|
39 |
+
ids = tokenizer.encode(f'{sequence}', return_tensors='pt')
|
40 |
+
input_length = ids.size(1)
|
41 |
+
max_length = input_length + max_new_tokens
|
42 |
+
final_outputs = model.generate(
|
43 |
+
ids,
|
44 |
+
do_sample=True,
|
45 |
+
max_length=max_length,
|
46 |
+
pad_token_id=model.config.eos_token_id
|
47 |
+
)
|
48 |
+
return tokenizer.decode(final_outputs[0], skip_special_tokens=True)
|
49 |
+
|
50 |
+
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
|
51 |
+
|
52 |
+
iface.launch()
|
checkpoint/config.json
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "gpt2",
|
3 |
+
"activation_function": "gelu_new",
|
4 |
+
"architectures": [
|
5 |
+
"GPT2LMHeadModel"
|
6 |
+
],
|
7 |
+
"attn_pdrop": 0.1,
|
8 |
+
"bos_token_id": 50256,
|
9 |
+
"embd_pdrop": 0.1,
|
10 |
+
"eos_token_id": 50256,
|
11 |
+
"initializer_range": 0.02,
|
12 |
+
"layer_norm_epsilon": 1e-05,
|
13 |
+
"model_type": "gpt2",
|
14 |
+
"n_ctx": 1024,
|
15 |
+
"n_embd": 768,
|
16 |
+
"n_head": 12,
|
17 |
+
"n_inner": null,
|
18 |
+
"n_layer": 12,
|
19 |
+
"n_positions": 1024,
|
20 |
+
"reorder_and_upcast_attn": false,
|
21 |
+
"resid_pdrop": 0.1,
|
22 |
+
"scale_attn_by_inverse_layer_idx": false,
|
23 |
+
"scale_attn_weights": true,
|
24 |
+
"summary_activation": null,
|
25 |
+
"summary_first_dropout": 0.1,
|
26 |
+
"summary_proj_to_labels": true,
|
27 |
+
"summary_type": "cls_index",
|
28 |
+
"summary_use_proj": true,
|
29 |
+
"task_specific_params": {
|
30 |
+
"text-generation": {
|
31 |
+
"do_sample": true,
|
32 |
+
"max_length": 50
|
33 |
+
}
|
34 |
+
},
|
35 |
+
"torch_dtype": "float32",
|
36 |
+
"transformers_version": "4.35.2",
|
37 |
+
"use_cache": true,
|
38 |
+
"vocab_size": 50260
|
39 |
+
}
|
checkpoint/generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 50256,
|
4 |
+
"eos_token_id": 50256,
|
5 |
+
"transformers_version": "4.35.2"
|
6 |
+
}
|
checkpoint/model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f1073341caebe0161048436239014844c08e8e90c876a716243de40d748bc1a8
|
3 |
+
size 497783424
|
checkpoint/optimizer.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ae943840e0a03905d0b8308e3f6decacf098823f9d5b3760d993e74d2858dc96
|
3 |
+
size 995660293
|
checkpoint/rng_state.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:54e75e628b60bbf47c975189df6cadd2954bdb738cdab1ed51bee943916d8cde
|
3 |
+
size 14575
|
checkpoint/scheduler.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e5e77b611783d805064e78066c7980b99b7e814294e237573ce5feea04f3061
|
3 |
+
size 627
|
checkpoint/trainer_state.json
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": null,
|
3 |
+
"best_model_checkpoint": null,
|
4 |
+
"epoch": 0.999722914934885,
|
5 |
+
"eval_steps": 1000,
|
6 |
+
"global_step": 1804,
|
7 |
+
"is_hyper_param_search": false,
|
8 |
+
"is_local_process_zero": true,
|
9 |
+
"is_world_process_zero": true,
|
10 |
+
"log_history": [
|
11 |
+
{
|
12 |
+
"epoch": 0.28,
|
13 |
+
"learning_rate": 3.6141906873614186e-05,
|
14 |
+
"loss": 4.4207,
|
15 |
+
"step": 500
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"epoch": 0.55,
|
19 |
+
"learning_rate": 2.2283813747228384e-05,
|
20 |
+
"loss": 3.4761,
|
21 |
+
"step": 1000
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"epoch": 0.83,
|
25 |
+
"learning_rate": 8.425720620842573e-06,
|
26 |
+
"loss": 3.4196,
|
27 |
+
"step": 1500
|
28 |
+
}
|
29 |
+
],
|
30 |
+
"logging_steps": 500,
|
31 |
+
"max_steps": 1804,
|
32 |
+
"num_train_epochs": 1,
|
33 |
+
"save_steps": 500,
|
34 |
+
"total_flos": 2368420383744000.0,
|
35 |
+
"trial_name": null,
|
36 |
+
"trial_params": null
|
37 |
+
}
|
checkpoint/training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:be9eb2e69f263713b25dc91833537b35ea0d88d98ff9893d9d11d60a5a703bb9
|
3 |
+
size 4155
|
dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "fastapi:app", "--host", "0.0.0.0", "--port", "8081"]
|
fastapi.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
|
3 |
+
from transformers import LineByLineTextDataset
|
4 |
+
from transformers import DataCollatorForLanguageModeling
|
5 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
6 |
+
from transformers import Trainer, TrainingArguments
|
7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
app.add_middleware(
|
11 |
+
CORSMiddleware,
|
12 |
+
allow_origins=["*"],
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"],
|
15 |
+
)
|
16 |
+
|
17 |
+
def load_model(model_path):
|
18 |
+
model = GPT2LMHeadModel.from_pretrained(model_path)
|
19 |
+
return model
|
20 |
+
|
21 |
+
|
22 |
+
def load_tokenizer(tokenizer_path):
|
23 |
+
tokenizer = GPT2Tokenizer.from_pretrained(tokenizer_path)
|
24 |
+
return tokenizer
|
25 |
+
|
26 |
+
model_path = r'../checkpoint/'
|
27 |
+
model = load_model(model_path)
|
28 |
+
tokenizer = load_tokenizer(model_path)
|
29 |
+
|
30 |
+
|
31 |
+
def generate_text(sequence, max_new_tokens):
|
32 |
+
ids = tokenizer.encode(f'{sequence}', return_tensors='pt')
|
33 |
+
input_length = ids.size(1)
|
34 |
+
max_length = input_length + max_new_tokens
|
35 |
+
final_outputs = model.generate(
|
36 |
+
ids,
|
37 |
+
do_sample=True,
|
38 |
+
max_length=max_length,
|
39 |
+
pad_token_id=model.config.eos_token_id
|
40 |
+
)
|
41 |
+
return tokenizer.decode(final_outputs[0], skip_special_tokens=True)
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
@app.get("/subject/{prompt}")
|
46 |
+
async def root(prompt: str):
|
47 |
+
print(prompt)
|
48 |
+
return {"subject": generate_text("Email : " + prompt + " Subject : ", 7).split('Subject : ')[1]}
|
requirements.txt
ADDED
File without changes
|