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