File size: 1,799 Bytes
9718b31
 
 
deb5853
9718b31
 
 
 
 
deb5853
 
9718b31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1468b43
9718b31
eef9168
 
9718b31
 
 
 
 
eef9168
9718b31
 
 
 
 
 
 
 
eef9168
 
9718b31
1468b43
9718b31
 
 
 
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
import wandb
import torch
import re
import os

import gradio

from transformers import GPT2Tokenizer,GPT2LMHeadModel

os.environ["WANDB_API_KEY"] = "d2ad0a7285379c0808ca816971d965fc242d0b5e"

wandb.login()

run = wandb.init(project="Question_Answer", job_type="model_loading", id='xeew4vz7', resume="must")

artifact = run.use_artifact('Question_Answer/final_model_QA:v0')

#artifact = run.use_artifact('enron-subgen-gpt2/model-1hhufzjv:v0')
# Download the artifact to a directory
artifact_dir = artifact.download()

MODEL_KEY = 'distilgpt2'
tokenizer= GPT2Tokenizer.from_pretrained(MODEL_KEY)
tokenizer.add_special_tokens({'pad_token':'{PAD}'})

model = GPT2LMHeadModel.from_pretrained(artifact_dir)
model.resize_token_embeddings(len(tokenizer))

def clean_text(text):
    # Lowercase the text
    
    res = re.sub(r'\d', '', text)

    text = text.lower()
    # Remove special characters
    text = re.sub(r'\W', ' ', text)
    # Remove extra white spaces
    text = re.sub(r'\s+', ' ', text).strip()
    return text

def generateAnswer(question):

    question = question.strip()

    question = "<question>" + clean_text(question) + "<answer>"

    prompt = []
    prompt.append(question)

    tokenizer.padding_side='left'
    prompts_batch_ids = tokenizer(prompt,
            padding=True, truncation=True, return_tensors='pt').to(model.device)
    output_ids = model.generate(
            **prompts_batch_ids, max_new_tokens=50,
            pad_token_id=tokenizer.pad_token_id)
    outputs_batch = [seq.split('<answer>')[1] for seq in
            tokenizer.batch_decode(output_ids, skip_special_tokens=True)]
    print(outputs_batch)
    tokenizer.padding_side='right'

    return outputs_batch[0]



iface = gradio.Interface(fn=generateAnswer, inputs="text", outputs="text")
iface.launch()