File size: 1,584 Bytes
af0197c
444246f
8f9f40e
af0197c
8f9f40e
 
d19e0a0
 
8f9f40e
444246f
 
3526dc3
 
 
 
 
 
 
 
444246f
d19e0a0
 
 
8f9f40e
d19e0a0
49d264d
8f9f40e
 
 
49d264d
8f9f40e
 
 
 
 
 
444246f
 
3483b56
8f9f40e
 
 
3526dc3
 
75c52ad
 
444246f
 
8f9f40e
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
import torch
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer

tokenizer = GPT2Tokenizer.from_pretrained("sberbank-ai/mGPT")
model = GPT2LMHeadModel.from_pretrained("sberbank-ai/mGPT")
#model.cuda()
#model.eval()

description = "Multilingual generation with mGPT"
title = "Generate your own example"

examples = [["""English: The vase with flowers is on the table.\nFinnish translation:""", "In May we celebrate "]]

article = (
    "<p style='text-align: center'>"
    "<a href='https://github.com/ai-forever/mgpt'>GitHub</a>  "
    "</p>"
)

device = "cuda" if torch.cuda.is_available() else "cpu"
fp16 = device != 'cpu'

def generate(prompt: str):
            input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
            out = model.generate(input_ids, 
                    min_length=100, 
                    max_length=200, 
                    top_p=0.8, 
                    top_k=0,
                    no_repeat_ngram_size=5
            )
            generated_text = list(map(tokenizer.decode, out))[0]
            return generated_text
            

interface = gr.Interface.load("huggingface/sberbank-ai/mGPT",
            description=description,
            examples=examples, 
            fn=generate,
            inputs="text",
            outputs='text',
            thumbnail = 'https://habrastorage.org/r/w1560/getpro/habr/upload_files/26a/fa1/3e1/26afa13e1d1a56f54c7b0356761af7b8.png',
            theme = "peach",
            article = article,
            cache_examples=True
)

interface.launch(enable_queue=True)