File size: 3,028 Bytes
83ca326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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.")