Peter Moc commited on
Commit
b7f8a86
·
verified ·
1 Parent(s): 5831314

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+
5
+ # Specify the local paths where the model and tokenizer are saved
6
+ model_dir = "path_to_save_model"
7
+ tokenizer_dir = "path_to_save_tokenizer"
8
+
9
+ # Load the model and tokenizer from the local directories
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_dir)
11
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_dir)
12
+
13
+ # Function to predict the sentiment
14
+ def predict_sentiment(text):
15
+ inputs = tokenizer(text, return_tensors="pt")
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
19
+ return torch.argmax(probs, dim=-1).item(), probs
20
+
21
+ # Streamlit interface
22
+ st.title("KIWI Classifier")
23
+ st.write("Enter a question or statement to classify:")
24
+
25
+ user_input = st.text_area("Your input", "")
26
+ if st.button("Classify"):
27
+ if user_input:
28
+ label, probabilities = predict_sentiment(user_input)
29
+ st.write(f"Prediction: {label}")
30
+ st.write(f"Probabilities: {probabilities.tolist()}")
31
+ else:
32
+ st.write("Please enter some text to classify.")
33
+
34
+ # Additional instructions or information
35
+ st.write("This application uses a fine-tuned BERT model to classify questions and statements.")