Multitask_NLP / app.py
DasariHarshitha's picture
Upload 2 files
83ca326 verified
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.")