Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import altair as alt
|
3 |
+
import torch
|
4 |
+
from transformers import AlbertTokenizer, AlbertForSequenceClassification
|
5 |
+
|
6 |
+
# Load pre-trained model and tokenizer
|
7 |
+
model_name = "albert-base-v2"
|
8 |
+
tokenizer = AlbertTokenizer.from_pretrained(model_name)
|
9 |
+
model = AlbertForSequenceClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Define function to classify input text
|
12 |
+
def classify_text(text):
|
13 |
+
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits.detach().numpy()[0]
|
16 |
+
probabilities = torch.softmax(torch.tensor(logits), dim=0).tolist()
|
17 |
+
return probabilities
|
18 |
+
|
19 |
+
# Set up Streamlit app
|
20 |
+
st.title("ALBERT Text Classification App")
|
21 |
+
|
22 |
+
# Create input box for user to enter text
|
23 |
+
text_input = st.text_area("Enter text to classify", height=200)
|
24 |
+
|
25 |
+
# Classify input text and display results
|
26 |
+
if st.button("Classify"):
|
27 |
+
if text_input:
|
28 |
+
probabilities = classify_text(text_input)
|
29 |
+
df = pd.DataFrame({
|
30 |
+
'Label': ['Negative', 'Positive'],
|
31 |
+
'Probability': probabilities
|
32 |
+
})
|
33 |
+
chart = alt.Chart(df).mark_bar().encode(
|
34 |
+
x='Probability',
|
35 |
+
y=alt.Y('Label', sort=['Negative', 'Positive'])
|
36 |
+
)
|
37 |
+
st.write(chart)
|
38 |
+
else:
|
39 |
+
st.write("Please enter some text to classify.")
|