Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +63 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# ---------------------- Page Config ----------------------
|
| 5 |
+
st.set_page_config(page_title="π§ Smart NLP Assistant", layout="centered")
|
| 6 |
+
|
| 7 |
+
# ---------------------- Sidebar --------------------------
|
| 8 |
+
st.sidebar.title("π§ Smart NLP Assistant")
|
| 9 |
+
task = st.sidebar.selectbox("π Select an NLP Task:",
|
| 10 |
+
["π Text Classification", "β Question Answering", "π° Summarization"])
|
| 11 |
+
|
| 12 |
+
st.title("π NLP Companion App")
|
| 13 |
+
st.markdown("Interact with powerful **Transformer models** for different Natural Language Processing tasks right in your browser!")
|
| 14 |
+
|
| 15 |
+
# -------------------- Load Models ------------------------
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_pipelines():
|
| 18 |
+
sentiment_model = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 19 |
+
qa_model = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 20 |
+
summarizer_model = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 21 |
+
return sentiment_model, qa_model, summarizer_model
|
| 22 |
+
|
| 23 |
+
text_classifier, qa_pipeline, summarizer = load_pipelines()
|
| 24 |
+
|
| 25 |
+
# -------------------- Task: Sentiment Analysis ---------------------
|
| 26 |
+
if task == "π Text Classification":
|
| 27 |
+
st.subheader("π Sentiment Analysis")
|
| 28 |
+
user_input = st.text_input("βοΈ Enter a sentence to analyze sentiment:")
|
| 29 |
+
|
| 30 |
+
if user_input.strip():
|
| 31 |
+
with st.spinner("π Analyzing sentiment..."):
|
| 32 |
+
result = text_classifier(user_input)[0]
|
| 33 |
+
st.success(f"π£ **Sentiment:** {result['label'].capitalize()}")
|
| 34 |
+
st.info(f"π― **Confidence Score:** {result['score']*100:.2f}%")
|
| 35 |
+
else:
|
| 36 |
+
st.warning("β οΈ Please enter some text above to proceed.")
|
| 37 |
+
|
| 38 |
+
# -------------------- Task: Question Answering ---------------------
|
| 39 |
+
elif task == "β Question Answering":
|
| 40 |
+
st.subheader("β Ask a Question")
|
| 41 |
+
context = st.text_area("π Paste the context passage:")
|
| 42 |
+
question = st.text_input("π Ask your question here:")
|
| 43 |
+
|
| 44 |
+
if context.strip() and question.strip():
|
| 45 |
+
with st.spinner("π‘ Finding the answer..."):
|
| 46 |
+
result = qa_pipeline(question=question, context=context)
|
| 47 |
+
st.success(f"β
**Answer:** {result['answer']}")
|
| 48 |
+
st.info(f"π― **Confidence Score:** {result['score']*100:.2f}%")
|
| 49 |
+
elif question or context:
|
| 50 |
+
st.warning("β οΈ Please provide both context and question.")
|
| 51 |
+
|
| 52 |
+
# -------------------- Task: Summarization ---------------------
|
| 53 |
+
elif task == "π° Summarization":
|
| 54 |
+
st.subheader("π° Summarize Text")
|
| 55 |
+
long_text = st.text_area("π Paste or type the long text to summarize:")
|
| 56 |
+
|
| 57 |
+
if long_text.strip():
|
| 58 |
+
with st.spinner("π§ Generating summary..."):
|
| 59 |
+
summary = summarizer(long_text, max_length=150, min_length=30, do_sample=False)[0]
|
| 60 |
+
st.success("π **Summary:**")
|
| 61 |
+
st.write(summary["summary_text"])
|
| 62 |
+
else:
|
| 63 |
+
st.warning("β οΈ Please enter content to summarize.")
|
requirements.txt
ADDED
|
File without changes
|