Spaces:
Sleeping
Sleeping
| 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 ------------------------ | |
| 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.") | |