File size: 879 Bytes
a8d8832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification

# Load pre-trained model and tokenizer
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Create Streamlit app
st.title("Hugging Face Transformers + Streamlit Example")

# Define a function to use the model for prediction
@st.cache(allow_output_mutation=True)
def run_model(input_text):
    inputs = tokenizer(input_text, return_tensors="pt")
    outputs = model(**inputs)
    predictions = torch.softmax(outputs.logits, dim=1)
    return predictions

# Streamlit interface
user_input = st.text_input("Enter text:", "Type Here...")
if st.button("Predict"):
    predictions = run_model(user_input)
    st.write(f"Predictions: {predictions}")