File size: 1,564 Bytes
036acc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import random
device = 'cpu'

def ans(question ):
    description=''
    category=''
    seed = random.randint(1, 10000000)
    print(f'Seed: {seed}')
    torch.manual_seed(seed)
    
    inp = tokenizer.encode(f'Вопрос: {question}\nОписание: {description}\nОтвет:',return_tensors="pt").to(device)
    print('question',question)
    gen = model.generate(inp, do_sample=True, top_p=0.9, temperature=0.86, max_new_tokens=100, repetition_penalty=1.2) #, stop_token="<eos>")
    
    gen = tokenizer.decode(gen[0])
    gen = gen[:gen.index('<eos>') if '<eos>' in gen else len(gen)]
    gen = gen.split('Ответ:')[1]
    return gen







# Download checkpoint:
checkpoint = "its5Q/rugpt3large_mailqa"
tokenizer =  AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint)
model = model.eval()

# Gradio

title = "Ответы на главные вопросы жизни, вселенной и вообще"
description = "t5 large predict activity "
article = "<p style='text-align: center'><a href='https://github.com/NeuralPushkin/MailRu_Q-A'>Github with fine-tuning ruGPT3large on QA</a></p> Cозданно при поддержке <p style='text-align: center'><a href='https://t.me/lovedeathtransformers'>Love Death Transformers</a></p>"


iface = gr.Interface(fn=ans, title=title, description=description, article=article, inputs="text", outputs="text")

if __name__ == "__main__":
    iface.launch()