import streamlit as st from transformers import pipeline # ---------------------- Page Config ---------------------- st.set_page_config(page_title="🧠 Smart NLP Assistant", layout="centered") # ---------------------- Sidebar -------------------------- st.sidebar.title("🧠 Smart NLP Assistant") task = st.sidebar.selectbox("📌 Select an NLP Task:", ["📝 Text Classification", "❓ Question Answering", "📰 Summarization"]) st.title("🌟 NLP Companion App") st.markdown("Interact with powerful **Transformer models** for different Natural Language Processing tasks right in your browser!") # -------------------- Load Models ------------------------ @st.cache_resource def load_pipelines(): sentiment_model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2") summarizer_model = pipeline("summarization", model="facebook/bart-large-cnn") return sentiment_model, qa_model, summarizer_model text_classifier, qa_pipeline, summarizer = load_pipelines() # -------------------- Task: Sentiment Analysis --------------------- if task == "📝 Text Classification": st.subheader("📝 Sentiment Analysis") user_input = st.text_input("✍️ Enter a sentence to analyze sentiment:") if user_input.strip(): with st.spinner("🔍 Analyzing sentiment..."): result = text_classifier(user_input)[0] st.success(f"📣 **Sentiment:** {result['label'].capitalize()}") st.info(f"🎯 **Confidence Score:** {result['score']*100:.2f}%") else: st.warning("⚠️ Please enter some text above to proceed.") # -------------------- Task: Question Answering --------------------- elif task == "❓ Question Answering": st.subheader("❓ Ask a Question") context = st.text_area("📄 Paste the context passage:") question = st.text_input("🔍 Ask your question here:") if context.strip() and question.strip(): with st.spinner("💡 Finding the answer..."): result = qa_pipeline(question=question, context=context) st.success(f"✅ **Answer:** {result['answer']}") st.info(f"🎯 **Confidence Score:** {result['score']*100:.2f}%") elif question or context: st.warning("⚠️ Please provide both context and question.") # -------------------- Task: Summarization --------------------- elif task == "📰 Summarization": st.subheader("📰 Summarize Text") long_text = st.text_area("📃 Paste or type the long text to summarize:") if long_text.strip(): with st.spinner("🧠 Generating summary..."): summary = summarizer(long_text, max_length=150, min_length=30, do_sample=False)[0] st.success("📝 **Summary:**") st.write(summary["summary_text"]) else: st.warning("⚠️ Please enter content to summarize.")