Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import nltk
|
| 3 |
+
import spacy
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import random
|
| 7 |
+
|
| 8 |
+
# Load NLP models
|
| 9 |
+
nltk.download("vader_lexicon")
|
| 10 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
| 11 |
+
sia = SentimentIntensityAnalyzer()
|
| 12 |
+
|
| 13 |
+
nlp = spacy.load("en_core_web_sm")
|
| 14 |
+
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
|
| 15 |
+
|
| 16 |
+
# Sample texts
|
| 17 |
+
sample_texts = [
|
| 18 |
+
"The digital world is transforming the way we read and engage with text.",
|
| 19 |
+
"Reading is an essential skill that shapes our understanding of the world.",
|
| 20 |
+
"AI-driven education tools can personalize the learning experience for students."
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# Streamlit UI
|
| 24 |
+
st.title("π AI-Powered Adaptive Reading Engagement")
|
| 25 |
+
st.write("Analyze how users engage with digital reading using AI-powered insights.")
|
| 26 |
+
|
| 27 |
+
# Text Input
|
| 28 |
+
text_option = st.selectbox("Choose a sample text or enter your own:", ["Use Sample"] + sample_texts)
|
| 29 |
+
if text_option == "Use Sample":
|
| 30 |
+
text = st.text_area("Read this passage:", random.choice(sample_texts), height=150)
|
| 31 |
+
else:
|
| 32 |
+
text = st.text_area("Enter your own text:", height=150)
|
| 33 |
+
|
| 34 |
+
# Sentiment Analysis
|
| 35 |
+
if st.button("Analyze Engagement"):
|
| 36 |
+
if text:
|
| 37 |
+
sentiment_score = sia.polarity_scores(text)
|
| 38 |
+
emotion_results = emotion_pipeline(text)
|
| 39 |
+
|
| 40 |
+
# Display Sentiment
|
| 41 |
+
st.subheader("π Sentiment Analysis")
|
| 42 |
+
st.write(f"Positive: {sentiment_score['pos'] * 100:.2f}%, Negative: {sentiment_score['neg'] * 100:.2f}%, Neutral: {sentiment_score['neu'] * 100:.2f}%")
|
| 43 |
+
|
| 44 |
+
# Display Emotion
|
| 45 |
+
st.subheader("π Emotion Detection")
|
| 46 |
+
top_emotion = max(emotion_results[0], key=lambda x: x['score'])
|
| 47 |
+
st.write(f"Detected Emotion: **{top_emotion['label']}** (Confidence: {top_emotion['score']:.2f})")
|
| 48 |
+
|
| 49 |
+
# Visualization
|
| 50 |
+
labels = [e['label'] for e in emotion_results[0]]
|
| 51 |
+
scores = [e['score'] for e in emotion_results[0]]
|
| 52 |
+
fig, ax = plt.subplots()
|
| 53 |
+
ax.bar(labels, scores)
|
| 54 |
+
st.pyplot(fig)
|
| 55 |
+
else:
|
| 56 |
+
st.warning("Please enter a text to analyze.")
|