Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load the text classification model from Hugging Face
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_name = "annyalvarez/MoralS-BERT"
|
| 9 |
+
return pipeline("text-classification", model=model_name)
|
| 10 |
+
|
| 11 |
+
model = load_model()
|
| 12 |
+
|
| 13 |
+
# Streamlit app interface
|
| 14 |
+
st.title("Moral Values Text Classification")
|
| 15 |
+
|
| 16 |
+
# Text input from the user
|
| 17 |
+
user_input = st.text_area("Enter text for classification:", "")
|
| 18 |
+
|
| 19 |
+
if st.button("Classify"):
|
| 20 |
+
if user_input:
|
| 21 |
+
# Get model predictions
|
| 22 |
+
predictions = model(user_input)
|
| 23 |
+
|
| 24 |
+
# Sort predictions by score
|
| 25 |
+
predictions = sorted(predictions, key=lambda x: x['score'], reverse=True)
|
| 26 |
+
|
| 27 |
+
# Format the predictions as required
|
| 28 |
+
output = [{
|
| 29 |
+
"label": pred['label'],
|
| 30 |
+
"score": pred['score']
|
| 31 |
+
} for pred in predictions]
|
| 32 |
+
|
| 33 |
+
# Display the output
|
| 34 |
+
st.json(output)
|
| 35 |
+
else:
|
| 36 |
+
st.write("Please enter some text to classify.")
|