File size: 2,299 Bytes
3e149d5
 
 
 
 
0a042d8
cdfe192
703a8b7
0a042d8
cdfe192
3e149d5
cdfe192
 
 
 
 
 
 
 
 
3e149d5
 
 
 
 
 
 
 
 
 
 
 
 
703a8b7
db0e2dc
cdfe192
 
3e149d5
db0e2dc
cdfe192
 
db0e2dc
3e149d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
grammar_tokenizer = AutoTokenizer.from_pretrained("prithivida/grammar_error_correcter_v1")
grammar_model = AutoModelForSeq2SeqLM.from_pretrained("prithivida/grammar_error_correcter_v1")
import torch
import gradio as gr


def chat(message, history):
    history = history or []
    if message.startswith("How many"):
        response = random.randint(1, 10)
    elif message.startswith("How"):
        response = random.choice(["Great", "Good", "Okay", "Bad"])
    elif message.startswith("Where"):
        response = random.choice(["Here", "There", "Somewhere"])
    else:
        response = "I don't know"
    history.append((message, response))
    return history, feedback(message)


def feedback(text):
    tokenized_phrases = grammar_tokenizer([text], return_tensors='pt', padding=True)
    corrections = grammar_model.generate(**tokenized_phrases)
    corrections = grammar_tokenizer.batch_decode(corrections, skip_special_tokens=True)
    print("The corrections are: ", corrections)
    if corrections[0] == text:
        feedback = f'Looks good! Keep up the good work'
    else:
        feedback = f'\'{corrections[0]}\' might be a little better'
    return f'FEEDBACK:  {feedback}'

iface = gr.Interface(
    chat,
    ["text", "state"],
    ["chatbot", "text"],
    allow_screenshot=False,
    allow_flagging="never",
)
iface.launch()



new_user_input_ids = tokenizer.encode(text+tokenizer.eos_token, return_tensors='pt')
    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if chat_history_ids is not None else new_user_input_ids

    # generated a response while limiting the total chat history to 1000 tokens, 
    chat_history_ids = model.generate(bot_input_ids, max_length=5000, pad_token_id=tokenizer.eos_token_id)
    print("The text is ", [text])

    # pretty print last ouput tokens from bot
    output =  tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
    print("The outout is :", output)
    text_session.append(output)