File size: 1,352 Bytes
86f8f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, TFAutoModelForCausalLM


SAVED_CHECKPOINT = 'mikegarts/distilgpt2-erichmariaremarque'
MIN_WORDS = 60


def get_model():
    model = TFAutoModelForCausalLM.from_pretrained(SAVED_CHECKPOINT)
    tokenizer = AutoTokenizer.from_pretrained(SAVED_CHECKPOINT)
    return model, tokenizer


def generate(prompt):
    model, tokenizer = get_model()

    input_context = prompt
    input_ids = tokenizer.encode(input_context, return_tensors="tf")

    outputs = model.generate(
        input_ids=input_ids,
        max_length=MIN_WORDS,
        temperature=0.7,
        num_return_sequences=1,
        do_sample=True
    )

    return tokenizer.decode(outputs[0], skip_special_tokens=True).rsplit('.', 1)[0] + '.'


def predict(prompt):
    return generate(prompt=prompt)


title = "What would Remarques say?"
description = """
The bot was trained to complete your prompt as if it was a begining of a paragraph of Remarque's book.
<img src="https://upload.wikimedia.org/wikipedia/commons/1/10/Bundesarchiv_Bild_183-R04034%2C_Erich_Maria_Remarque_%28cropped%29.jpg" align=center width=200px>
"""

gr.Interface(
    fn=predict,
    inputs="textbox",
    outputs="text",
    title=title,
    description=description,
    examples=[["I was drinking because"], ["Who is Karl for me?"]]
).launch(debug=True)