Spaces:
Sleeping
Sleeping
Streamlit UI
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
4 |
+
|
5 |
+
# Load the fine-tuned model and tokenizer at startup
|
6 |
+
model_name = "EbukaGaus/EbukaMBert"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Define the label mapping
|
11 |
+
label_mapping = {0: 'Neutral', 1: 'Negative', 2: 'Positive'}
|
12 |
+
|
13 |
+
# Define a function to predict sentiment
|
14 |
+
def predict_sentiment(text: str):
|
15 |
+
# Tokenise the input text
|
16 |
+
inputs = tokenizer(text, return_tensors="pt")
|
17 |
+
|
18 |
+
# Run inference without tracking gradients
|
19 |
+
with torch.no_grad():
|
20 |
+
outputs = model(**inputs)
|
21 |
+
|
22 |
+
# Apply softmax to get probabilities
|
23 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
24 |
+
|
25 |
+
# Get the most likely class
|
26 |
+
predicted_class = torch.argmax(probabilities, dim=1).item()
|
27 |
+
|
28 |
+
# Map the predicted class to the sentiment label
|
29 |
+
predicted_label = label_mapping[predicted_class]
|
30 |
+
|
31 |
+
# Retrieve the confidence score
|
32 |
+
confidence = probabilities[0][predicted_class].item()
|
33 |
+
|
34 |
+
return predicted_label, confidence
|
35 |
+
|
36 |
+
def main():
|
37 |
+
st.title("Simple Sentiment Analysis App")
|
38 |
+
|
39 |
+
# Text input
|
40 |
+
text_input = st.text_area("Enter your text here", "")
|
41 |
+
|
42 |
+
# Predict sentiment when the button is clicked
|
43 |
+
if st.button("Predict Sentiment"):
|
44 |
+
if text_input.strip() == "":
|
45 |
+
st.warning("Please enter some text first.")
|
46 |
+
else:
|
47 |
+
sentiment, confidence = predict_sentiment(text_input)
|
48 |
+
st.write(f"**Sentiment:** {sentiment}")
|
49 |
+
st.write(f"**Confidence:** {confidence:.2f}")
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
main()
|