Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import json | |
import langdetect | |
from keybert import KeyBERT | |
from sklearn.feature_extraction.text import CountVectorizer | |
# Load Pretrained Models | |
def load_models(): | |
return { | |
"emotion": pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True), | |
"sentiment": pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment"), | |
"summarization": pipeline("summarization", model="facebook/bart-large-cnn"), | |
"ner": pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", grouped_entities=True), | |
"toxicity": pipeline("text-classification", model="unitary/unbiased-toxic-roberta"), | |
"keyword_extraction": KeyBERT() | |
} | |
models = load_models() | |
# Function: Emotion Detection | |
def analyze_emotions(text): | |
results = models["emotion"](text) | |
return {r['label']: round(r['score'], 2) for r in results[0]} | |
# Function: Sentiment Analysis | |
def analyze_sentiment(text): | |
result = models["sentiment"](text)[0] | |
return {result['label']: round(result['score'], 2)} | |
# Function: Text Summarization | |
def summarize_text(text): | |
return models["summarization"](text[:1024])[0]['summary_text'] | |
# Function: Keyword Extraction | |
def extract_keywords(text): | |
vectorizer = CountVectorizer(ngram_range=(1, 2)) | |
return models["keyword_extraction"].extract_keywords(text, vectorizer=vectorizer, stop_words='english') | |
# Function: Named Entity Recognition (NER) | |
def analyze_ner(text): | |
entities = models["ner"](text) | |
return {entity["word"]: entity["entity_group"] for entity in entities} | |
# Function: Language Detection | |
def detect_language(text): | |
try: | |
return langdetect.detect(text) | |
except: | |
return "Error detecting language" | |
# Function: Toxicity Detection | |
def detect_toxicity(text): | |
results = models["toxicity"](text) | |
return {results[0]['label']: round(results[0]['score'], 2)} | |
# Streamlit UI | |
st.title("๐ AI-Powered Text Intelligence App") | |
st.markdown("Analyze text with multiple NLP features: Emotion Detection, Sentiment Analysis, Summarization, NER, Keywords, Language Detection, and more!") | |
# User Input | |
text_input = st.text_area("Enter text to analyze:", "") | |
if st.button("Analyze Text"): | |
if text_input.strip(): | |
st.subheader("๐น Emotion Detection") | |
st.json(analyze_emotions(text_input)) | |
st.subheader("๐น Sentiment Analysis") | |
st.json(analyze_sentiment(text_input)) | |
st.subheader("๐น Text Summarization") | |
st.write(summarize_text(text_input)) | |
st.subheader("๐น Keyword Extraction") | |
st.json(extract_keywords(text_input)) | |
st.subheader("๐น Named Entity Recognition (NER)") | |
st.json(analyze_ner(text_input)) | |
st.subheader("๐น Language Detection") | |
st.write(f"Detected Language: `{detect_language(text_input)}`") | |
st.subheader("๐น Toxicity Detection") | |
st.json(detect_toxicity(text_input)) | |
# Save results to JSON | |
results = { | |
"emotion": analyze_emotions(text_input), | |
"sentiment": analyze_sentiment(text_input), | |
"summary": summarize_text(text_input), | |
"keywords": extract_keywords(text_input), | |
"ner": analyze_ner(text_input), | |
"language": detect_language(text_input), | |
"toxicity": detect_toxicity(text_input) | |
} | |
st.download_button("Download JSON Report", json.dumps(results, indent=2), "text_analysis.json", "application/json") | |
else: | |
st.warning("โ ๏ธ Please enter some text to analyze.") | |