File size: 2,234 Bytes
4f590f0
 
 
 
 
 
 
 
 
 
6a5c7ef
4f590f0
5d2e6f5
 
4f590f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f017bc7
4f590f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99ae404
4f590f0
 
 
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
import transformers
import torch
import tokenizers
import streamlit as st
import re

from PIL import Image


@st.cache(hash_funcs={tokenizers.Tokenizer: lambda _: None, tokenizers.AddedToken: lambda _: None, re.Pattern: lambda _: None}, allow_output_mutation=True, suppress_st_warning=True)
def get_model(model_name, model_path='pytorch_model.bin'):
    tokenizer = transformers.GPT2Tokenizer.from_pretrained(model_name)
    model = transformers.OPTForCausalLM.from_pretrained(model_name)
    model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
    
    model.eval()
    return model, tokenizer


def predict(text, model, tokenizer, n_beams=5, temperature=2.5, top_p=0.8, length_of_generated=300):
    text += '\n'
    input_ids = tokenizer.encode(text, return_tensors="pt")
    length_of_prompt = len(input_ids[0])
    with torch.no_grad():
        out = model.generate(input_ids,
                             do_sample=True,
                             num_beams=n_beams,
                             temperature=temperature,
                             top_p=top_p,
                             max_length=length_of_prompt + length_of_generated,
                             eos_token_id=tokenizer.eos_token_id
                             )
                          
    return list(map(tokenizer.decode, out))[0]


model, tokenizer = get_model('facebook/opt-13b')

# st.title("NeuroKorzh")

# image = Image.open('korzh.jpg')
# st.image(image, caption='НейроКорж')

# option = st.selectbox('Выберите своего Коржа', ('Быстрый', 'Глубокий'))
craziness = st.slider(label='Craziness', min_value=0, max_value=100, value=50, step=5)
temperature = 2 + craziness / 50.

st.markdown("\n")

text = st.text_area(label='What are you interested in?', value='Covid - a worldwide conspiracy?', height=80)
button = st.button('Go')

if button:
    try:
        with st.spinner('Finding out the truth'):
            result = predict(text, model, tokenizer, temperature=temperature)
        
        st.text_area(label='', value=result, height=1100)
    
    except Exception:
        st.error("Ooooops, something went wrong. Please try again and report to me, tg: @vladyur")