-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
52 lines (40 loc) · 1.72 KB
/
streamlit_app.py
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
import streamlit as st
import torch
from huggingface_hub import HfApi
from transformers import AutoTokenizer, AutoModelForSequenceClassification
api = HfApi()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Show title and description
st.title("🤖 Model Testing Platform")
st.write(
"This app allows you to test different text classification models. "
"Select a model, enter some text, and the app will classify it."
)
# Load available models from the 'models/' folder
def load_models():
models = api.list_models(author="Nadav-Deepchecks")
model_names = [model.modelId for model in models]
return model_names
# Create a dropdown to select the model
st.write("Loading a model may take up to 2 minutes")
model_name = st.selectbox("Select a model", load_models())
# Load the selected text classification model and tokenizer
def load_selected_model(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
model.to(device)
return tokenizer, model
# Create a text input field for the user
tokenizer, model = load_selected_model(model_name)
text = st.text_area("Enter some text to classify:")
# Classify the text when the user clicks the "Classify" button
if st.button("Classify"):
# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")
# Use the model to get the predicted label and score
outputs = model(**inputs)
logits = outputs.logits
predicted_class_id = logits.argmax().item()
predicted_label = model.config.id2label[predicted_class_id]
# Display the classification result
st.write(f"The text was classified as '{predicted_label}'")