File size: 5,473 Bytes
856e073
 
 
 
1eb01dd
856e073
30da747
 
 
 
 
 
 
 
856e073
e1881c6
856e073
 
 
 
e1881c6
856e073
 
1eb01dd
 
 
856e073
 
 
 
 
 
 
e1881c6
 
 
 
 
 
 
 
856e073
 
 
 
e1881c6
 
 
4ad1a6f
 
 
 
236bb3e
 
4ad1a6f
 
 
 
236bb3e
 
4ad1a6f
236bb3e
 
4ad1a6f
236bb3e
4ad1a6f
236bb3e
 
4ad1a6f
236bb3e
 
4ad1a6f
236bb3e
 
4ad1a6f
856e073
e1881c6
 
 
 
 
 
 
 
 
 
 
 
 
856e073
 
236bb3e
e1881c6
856e073
e1881c6
1eb01dd
e1881c6
 
 
 
 
 
1eb01dd
 
 
856e073
 
 
 
 
 
 
e1881c6
 
 
 
 
 
 
 
 
 
 
856e073
 
 
e1881c6
856e073
236bb3e
4ad1a6f
856e073
4ad1a6f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
import nltk
import spacy
import matplotlib.pyplot as plt
from transformers import pipeline
import random
import subprocess

# Ensure spaCy model is installed
try:
    nlp = spacy.load("en_core_web_sm")
except OSError:
    subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
    nlp = spacy.load("en_core_web_sm")

# Ensure NLTK resources are available
nltk.download("vader_lexicon")
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()

# Load English emotion detection pipeline
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)

# Load a better multilingual sentiment analysis model
multilingual_pipeline = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")

# Sample texts
sample_texts = [
    "The digital world is transforming the way we read and engage with text.",
    "Reading is an essential skill that shapes our understanding of the world.",
    "AI-driven education tools can personalize the learning experience for students."
]

# Supported languages for multilingual sentiment analysis
supported_languages = {
    "English": "en",
    "Dutch": "nl",
    "French": "fr",
    "German": "de"
}

# Streamlit UI
st.title("πŸ“– AI-Powered Adaptive Reading Engagement")
st.write("Analyze how users engage with digital reading using AI-powered insights.")

# Language selection
selected_language = st.selectbox("Select a language:", list(supported_languages.keys()))

# Use session state to store text input
if "user_text" not in st.session_state:
    st.session_state.user_text = ""

# Dropdown menu with a new option for "Choose Your Own Text"
text_option = st.selectbox("Choose a sample text or enter your own:", ["Choose Your Own Text"] + sample_texts)

# Text input limit
MAX_WORDS = 100  # Set the max word limit

# If user selects a sample text, use it. Otherwise, allow manual input.
if text_option == "Choose Your Own Text":
    text = st.text_area("Enter your own text:", st.session_state.user_text, height=150)
else:
    text = sample_texts[sample_texts.index(text_option)]  # Select the chosen sample text

# Show word count & limit warning only if the user is entering their own text
word_count = len(text.split())
if text_option == "Choose Your Own Text":
    st.markdown(f'<p style="font-size:12px; color:gray;">⚠️ Limit: {MAX_WORDS} words max.</p>', unsafe_allow_html=True)

    if word_count > MAX_WORDS:
        st.warning(f"⚠️ Your input has {word_count} words. Please limit it to {MAX_WORDS} words.")

# Save user input in session state only if within limit
if text_option == "Choose Your Own Text" and word_count <= MAX_WORDS:
    st.session_state.user_text = text

# Function to generate AI-driven feedback
def generate_feedback(sentiment_score, top_emotion):
    if sentiment_score['pos'] > 0.6:
        return "😊 Your reading is positive! This text might boost engagement and motivation."
    elif sentiment_score['neg'] > 0.6:
        return "πŸ˜” The text seems to have a strong negative tone. Consider balancing it with positive information."
    elif top_emotion in ["anger", "disgust"]:
        return "😑 This text might evoke strong emotions. Reflect on how it makes you feel and why."
    elif top_emotion in ["joy", "optimism"]:
        return "πŸŽ‰ This is an engaging and uplifting text. Keep exploring such content!"
    else:
        return "πŸ€” The text is neutral. You might want to explore different perspectives."

# Sentiment Analysis
if st.button("Analyze Engagement"):
    if text and (text_option != "Choose Your Own Text" or word_count <= MAX_WORDS):
        # Sentiment Analysis
        sentiment_score = sia.polarity_scores(text)

        # Emotion Detection
        if selected_language == "English":
            emotion_results = emotion_pipeline(text)
            top_emotion = max(emotion_results[0], key=lambda x: x['score'])['label']
            emotion_scores = {e['label']: e['score'] for e in emotion_results[0]}
        else:
            # Multilingual Sentiment Analysis
            multilingual_results = multilingual_pipeline(text)
            top_emotion = multilingual_results[0]["label"]
            emotion_scores = {top_emotion: multilingual_results[0]["score"]}

        # Display Sentiment
        st.subheader("πŸ“Š Sentiment Analysis")
        st.write(f"Positive: {sentiment_score['pos'] * 100:.2f}%, Negative: {sentiment_score['neg'] * 100:.2f}%, Neutral: {sentiment_score['neu'] * 100:.2f}%")

        # Display Emotion
        st.subheader("🎭 Emotion Detection")
        st.write(f"Top Emotion: **{top_emotion.capitalize()}**")

        # Display Multiple Emotion Scores
        st.subheader("πŸ” Expanded Emotion Insights")
        for emotion, score in emotion_scores.items():
            st.write(f"**{emotion.capitalize()}**: {score:.2f}")

        # AI-Generated Feedback
        st.subheader("πŸ’‘ AI-Generated Feedback")
        feedback = generate_feedback(sentiment_score, top_emotion)
        st.write(feedback)

        # Visualization
        fig, ax = plt.subplots()
        ax.bar(emotion_scores.keys(), emotion_scores.values())
        st.pyplot(fig)
    elif text_option == "Choose Your Own Text" and word_count > MAX_WORDS:
        st.warning("⚠️ Please reduce the text length to analyze.")
    else:
        st.warning("⚠️ Please enter a text to analyze.")