File size: 2,787 Bytes
53dcf47
 
 
 
 
 
 
 
 
74ce976
53dcf47
 
 
 
 
 
 
74ce976
53dcf47
 
74ce976
 
53dcf47
 
 
 
 
bf4c3e8
 
 
53dcf47
bf4c3e8
 
53dcf47
bf4c3e8
 
53dcf47
 
 
 
 
 
 
 
 
 
 
 
 
74ce976
 
53dcf47
 
 
 
 
 
 
 
 
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
import streamlit as st
from datasets import load_dataset
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoModel, Trainer, TrainingArguments, LineByLineTextDataset
import json


@st.cache()
def get_model():
    model = AutoModelForSequenceClassification.from_pretrained("siebert/sentiment-roberta-large-english", num_labels=2)
    model.load_state_dict(torch.load('cached_model.pth'))
    return model

@st.cache()
def get_tokenizer():
    tokenizer = AutoTokenizer.from_pretrained("siebert/sentiment-roberta-large-english")
    return tokenizer

def make_prediction(to_analyze):
    model = get_model()
    tokenizer = tokenizer()
    to_return = model(**tokenizer(to_anayze))
    return to_return
    


st.header("Sentiment analysis on twitter datasets")
st.markdown("Here is a sentiment model further trained on a slice of a twitter dataset")
# st.markdown("""
# <img width=700px src='https://imagez.tmz.com/image/73/4by3/2020/10/05/735aaee2f6b9464ca220e62ef797dab0_md.jpg'> 
# """, unsafe_allow_html=True)
st.markdown("""
<img width=700px src='https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.thrillist.com%2Fnews%2Fnation%2Fwendys-is-brutally-roasting-people-on-twitter-right-now&psig=AOvVaw3GKmzTk1mg1D4zbajRUZdB&ust=1681809839298000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCIiln4LMsP4CFQAAAAAdAAAAABAe
'>""", unsafe_allow_html=True)


text = st.markdown("Try typing something here! \n You will see how much better our model is compared to the base model! No kidding")
# ^-- показать текстовое поле. В поле text лежит строка, которая находится там в данный момент

### Loading and tokenizing data
# data = load_dataset("carblacac/twitter-sentiment-analysis")
# tokenizer = AutoTokenizer.from_pretrained("siebert/sentiment-roberta-large-english")
# dataset = data.map(lambda xs: tokenizer(xs["text"], truncation=True, padding='max_length'))
# dataset = dataset.rename_column("feeling", "labels")

with st.form(key='input_form'):
    to_analyze = st.text_input(label='Input text to be analyzed')
    button = st.form_submit_button(label='Classify')
if button:
    if to_analyze:
        pred = make_prediction(to_analyze)
        st.markdown(pred)
    else:
        st.markdown("Empty request. Please resubmit")

# classifier = pipeline('sentiment-analysis', model="distilbert-base-uncased-finetuned-sst-2-english")
# raw_predictions = classifier(text)
# тут уже знакомый вам код с huggingface.transformers -- его можно заменить на что угодно от fairseq до catboost

# st.markdown(f"{raw_predictions}")
# выводим результаты модели в текстовое поле, на потеху пользователю