KevSun commited on
Commit
4a2ff38
·
verified ·
1 Parent(s): 8b7655d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -34
app.py CHANGED
@@ -1,48 +1,38 @@
1
  import streamlit as st
2
- import sys
3
- import subprocess
4
 
5
- def install(package):
6
- subprocess.check_call([sys.executable, "-m", "pip", "install", package])
 
 
7
 
 
8
  st.title("Personality Prediction App")
 
9
 
10
- # Check and install numpy library
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
- with st.spinner("Analyzing..."):
39
- result = model(user_input)
 
 
 
 
 
 
 
 
40
 
41
- st.subheader("Predicted personality traits:")
42
- for trait in result:
43
- st.write(f"- {trait['label']}: {trait['score']:.2f}")
 
44
  else:
45
- st.warning("Please enter some text to analyze.")
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