|
import streamlit as st |
|
import altair as alt |
|
import torch |
|
from transformers import AlbertTokenizer, AlbertForSequenceClassification |
|
|
|
|
|
model_name = "albert-base-v2" |
|
tokenizer = AlbertTokenizer.from_pretrained(model_name) |
|
model = AlbertForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
def classify_text(text): |
|
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt") |
|
outputs = model(**inputs) |
|
logits = outputs.logits.detach().numpy()[0] |
|
probabilities = torch.softmax(torch.tensor(logits), dim=0).tolist() |
|
return probabilities |
|
|
|
|
|
st.title("ALBERT Text Classification App") |
|
|
|
|
|
text_input = st.text_area("Enter text to classify", height=200) |
|
|
|
|
|
if st.button("Classify"): |
|
if text_input: |
|
probabilities = classify_text(text_input) |
|
df = pd.DataFrame({ |
|
'Label': ['Negative', 'Positive'], |
|
'Probability': probabilities |
|
}) |
|
chart = alt.Chart(df).mark_bar().encode( |
|
x='Probability', |
|
y=alt.Y('Label', sort=['Negative', 'Positive']) |
|
) |
|
st.write(chart) |
|
else: |
|
st.write("Please enter some text to classify.") |
|
|