Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,32 +35,33 @@ st.write("Analyze how users engage with digital reading using AI-powered insight
|
|
35 |
if "user_text" not in st.session_state:
|
36 |
st.session_state.user_text = ""
|
37 |
|
38 |
-
# Dropdown menu
|
39 |
-
text_option = st.selectbox("Choose a sample text or enter your own:", ["
|
40 |
|
41 |
# Text input limit
|
42 |
MAX_WORDS = 100 # Set the max word limit
|
43 |
|
44 |
-
# If user selects
|
45 |
-
if text_option == "
|
46 |
-
text = st.text_area("Read this passage:", st.session_state.user_text or random.choice(sample_texts), height=150)
|
47 |
-
else:
|
48 |
text = st.text_area("Enter your own text:", st.session_state.user_text, height=150)
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
word_count = len(text.split())
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
st.warning(f"⚠️ Your input has {word_count} words. Please limit it to {MAX_WORDS} words.")
|
56 |
|
57 |
-
# Save
|
58 |
-
if word_count <= MAX_WORDS:
|
59 |
st.session_state.user_text = text
|
60 |
|
61 |
# Sentiment Analysis
|
62 |
if st.button("Analyze Engagement"):
|
63 |
-
if text and word_count <= MAX_WORDS:
|
64 |
sentiment_score = sia.polarity_scores(text)
|
65 |
emotion_results = emotion_pipeline(text)
|
66 |
|
@@ -79,7 +80,7 @@ if st.button("Analyze Engagement"):
|
|
79 |
fig, ax = plt.subplots()
|
80 |
ax.bar(labels, scores)
|
81 |
st.pyplot(fig)
|
82 |
-
elif word_count > MAX_WORDS:
|
83 |
st.warning("⚠️ Please reduce the text length to analyze.")
|
84 |
else:
|
85 |
st.warning("⚠️ Please enter a text to analyze.")
|
|
|
35 |
if "user_text" not in st.session_state:
|
36 |
st.session_state.user_text = ""
|
37 |
|
38 |
+
# Dropdown menu with a new option for "Choose Your Own Text"
|
39 |
+
text_option = st.selectbox("Choose a sample text or enter your own:", ["Choose Your Own Text"] + sample_texts)
|
40 |
|
41 |
# Text input limit
|
42 |
MAX_WORDS = 100 # Set the max word limit
|
43 |
|
44 |
+
# If user selects a sample text, use it. Otherwise, allow manual input.
|
45 |
+
if text_option == "Choose Your Own Text":
|
|
|
|
|
46 |
text = st.text_area("Enter your own text:", st.session_state.user_text, height=150)
|
47 |
+
else:
|
48 |
+
text = sample_texts[sample_texts.index(text_option)] # Select the chosen sample text
|
49 |
|
50 |
+
# Show word count & limit warning only if the user is entering their own text
|
51 |
word_count = len(text.split())
|
52 |
+
if text_option == "Choose Your Own Text":
|
53 |
+
st.markdown(f'<p style="font-size:12px; color:gray;">⚠️ Limit: {MAX_WORDS} words max.</p>', unsafe_allow_html=True)
|
54 |
|
55 |
+
if word_count > MAX_WORDS:
|
56 |
+
st.warning(f"⚠️ Your input has {word_count} words. Please limit it to {MAX_WORDS} words.")
|
|
|
57 |
|
58 |
+
# Save user input in session state only if within limit
|
59 |
+
if text_option == "Choose Your Own Text" and word_count <= MAX_WORDS:
|
60 |
st.session_state.user_text = text
|
61 |
|
62 |
# Sentiment Analysis
|
63 |
if st.button("Analyze Engagement"):
|
64 |
+
if text and (text_option != "Choose Your Own Text" or word_count <= MAX_WORDS):
|
65 |
sentiment_score = sia.polarity_scores(text)
|
66 |
emotion_results = emotion_pipeline(text)
|
67 |
|
|
|
80 |
fig, ax = plt.subplots()
|
81 |
ax.bar(labels, scores)
|
82 |
st.pyplot(fig)
|
83 |
+
elif text_option == "Choose Your Own Text" and word_count > MAX_WORDS:
|
84 |
st.warning("⚠️ Please reduce the text length to analyze.")
|
85 |
else:
|
86 |
st.warning("⚠️ Please enter a text to analyze.")
|