Durganihantri commited on
Commit
4ad1a6f
·
verified ·
1 Parent(s): d0e6057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -31,16 +31,36 @@ sample_texts = [
31
  st.title("📖 AI-Powered Adaptive Reading Engagement")
32
  st.write("Analyze how users engage with digital reading using AI-powered insights.")
33
 
34
- # Text Input
 
 
 
 
35
  text_option = st.selectbox("Choose a sample text or enter your own:", ["Use Sample"] + sample_texts)
 
 
 
 
 
36
  if text_option == "Use Sample":
37
- text = st.text_area("Read this passage:", random.choice(sample_texts), height=150)
38
  else:
39
- text = st.text_area("Enter your own text:", height=150)
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Sentiment Analysis
42
  if st.button("Analyze Engagement"):
43
- if text:
44
  sentiment_score = sia.polarity_scores(text)
45
  emotion_results = emotion_pipeline(text)
46
 
@@ -59,5 +79,7 @@ if st.button("Analyze Engagement"):
59
  fig, ax = plt.subplots()
60
  ax.bar(labels, scores)
61
  st.pyplot(fig)
 
 
62
  else:
63
- st.warning("Please enter a text to analyze.")
 
31
  st.title("📖 AI-Powered Adaptive Reading Engagement")
32
  st.write("Analyze how users engage with digital reading using AI-powered insights.")
33
 
34
+ # Use session state to store text input
35
+ if "user_text" not in st.session_state:
36
+ st.session_state.user_text = ""
37
+
38
+ # Dropdown menu to select a sample text
39
  text_option = st.selectbox("Choose a sample text or enter your own:", ["Use Sample"] + sample_texts)
40
+
41
+ # Text input limit
42
+ MAX_WORDS = 100 # Set the max word limit
43
+
44
+ # If user selects "Use Sample," show a sample text
45
  if text_option == "Use Sample":
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
+ # Count words in user input
51
+ word_count = len(text.split())
52
+
53
+ # Show word count & limit warning
54
+ if word_count > MAX_WORDS:
55
+ st.warning(f"⚠️ Your input has {word_count} words. Please limit it to {MAX_WORDS} words.")
56
+
57
+ # Save the text input to session state only if it is within the limit
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
  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.")