Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,38 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
st.title("Personality Prediction App")
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
try:
|
| 12 |
-
import numpy as np
|
| 13 |
-
except ImportError:
|
| 14 |
-
st.warning("The numpy library is not installed. Attempting to install it now...")
|
| 15 |
-
install('numpy')
|
| 16 |
-
st.experimental_rerun()
|
| 17 |
-
|
| 18 |
-
# Check and install transformers library
|
| 19 |
-
try:
|
| 20 |
-
from transformers import pipeline
|
| 21 |
-
except ImportError:
|
| 22 |
-
st.warning("The transformers library is not installed. Attempting to install it now...")
|
| 23 |
-
install('transformers')
|
| 24 |
-
st.experimental_rerun()
|
| 25 |
-
|
| 26 |
-
@st.cache_resource
|
| 27 |
-
def load_model():
|
| 28 |
-
return pipeline("text-classification", model="KevSun/Personality_LM")
|
| 29 |
-
|
| 30 |
-
model = load_model()
|
| 31 |
-
|
| 32 |
-
st.write("Enter your text below to predict personality traits:")
|
| 33 |
-
|
| 34 |
user_input = st.text_area("Your text here:")
|
| 35 |
|
| 36 |
if st.button("Predict"):
|
| 37 |
if user_input:
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
else:
|
| 45 |
-
st.
|
| 46 |
|
| 47 |
st.info("Note: This is a demonstration and predictions may not be entirely accurate.")
|
| 48 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the model and tokenizer from Hugging Face
|
| 6 |
+
model_name = "KevSun/Personality_LM"
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Streamlit app
|
| 11 |
st.title("Personality Prediction App")
|
| 12 |
+
st.write("Enter your text below to predict BigFive Personality traits:")
|
| 13 |
|
| 14 |
+
# Input text from user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
user_input = st.text_area("Your text here:")
|
| 16 |
|
| 17 |
if st.button("Predict"):
|
| 18 |
if user_input:
|
| 19 |
+
# Tokenize input text
|
| 20 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
| 21 |
+
|
| 22 |
+
# Get predictions from the model
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
|
| 26 |
+
# Extract the predictions
|
| 27 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 28 |
+
predictions = predictions[0].tolist()
|
| 29 |
|
| 30 |
+
# Display the predictions
|
| 31 |
+
labels = ["Extraversion", "Agreeableness", "Conscientiousness", "Neuroticism", "Openness"]
|
| 32 |
+
for label, score in zip(labels, predictions):
|
| 33 |
+
st.write(f"{label}: {score:.4f}")
|
| 34 |
else:
|
| 35 |
+
st.write("Please enter your text.")
|
| 36 |
|
| 37 |
st.info("Note: This is a demonstration and predictions may not be entirely accurate.")
|
| 38 |
|