Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Custom CSS for styling
|
| 5 |
+
st.markdown(
|
| 6 |
+
"""
|
| 7 |
+
<style>
|
| 8 |
+
.stApp {
|
| 9 |
+
background: linear-gradient(120deg, #2C5364, #203A43, #0F2027);
|
| 10 |
+
color: #F0F0F0;
|
| 11 |
+
}
|
| 12 |
+
.stTitle {
|
| 13 |
+
text-align: center;
|
| 14 |
+
color: #FFD700;
|
| 15 |
+
}
|
| 16 |
+
.css-1d391kg p, .css-1v0mbdj p {
|
| 17 |
+
color: #F0F0F0;
|
| 18 |
+
}
|
| 19 |
+
.stButton > button:hover {
|
| 20 |
+
background-color: #FFD700;
|
| 21 |
+
color: #0F2027;
|
| 22 |
+
transform: scale(1.05);
|
| 23 |
+
}
|
| 24 |
+
</style>
|
| 25 |
+
""",
|
| 26 |
+
unsafe_allow_html=True,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
st.title("π§ NLP Tool with Hugging Face", anchor=False)
|
| 30 |
+
|
| 31 |
+
st.sidebar.subheader("π Explore NLP Features")
|
| 32 |
+
|
| 33 |
+
# Dropdown to select NLP task
|
| 34 |
+
nlp_task = st.sidebar.selectbox(
|
| 35 |
+
"Select an NLP Task:",
|
| 36 |
+
["Text Classification", "Sentiment Analysis", "Question Answering", "Summarization", "Text Generation"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Input text area
|
| 40 |
+
user_input = st.text_area("Enter your text here:", "", height=150)
|
| 41 |
+
|
| 42 |
+
# Functionality for each task
|
| 43 |
+
if user_input:
|
| 44 |
+
if nlp_task == "Text Classification":
|
| 45 |
+
st.subheader("π Text Classification")
|
| 46 |
+
classifier = pipeline("text-classification")
|
| 47 |
+
result = classifier(user_input)
|
| 48 |
+
st.write("**Classification Results:**")
|
| 49 |
+
for res in result:
|
| 50 |
+
st.write(f"- **Label**: {res['label']}, **Score**: {res['score']:.2f}")
|
| 51 |
+
|
| 52 |
+
elif nlp_task == "Sentiment Analysis":
|
| 53 |
+
st.subheader("π Sentiment Analysis")
|
| 54 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 55 |
+
result = sentiment_analyzer(user_input)
|
| 56 |
+
st.write("**Sentiment Analysis Results:**")
|
| 57 |
+
for res in result:
|
| 58 |
+
st.write(f"- **Label**: {res['label']}, **Score**: {res['score']:.2f}")
|
| 59 |
+
|
| 60 |
+
elif nlp_task == "Question Answering":
|
| 61 |
+
st.subheader("β Question Answering")
|
| 62 |
+
question = st.text_input("Ask a question about the provided text:")
|
| 63 |
+
if question:
|
| 64 |
+
qa_pipeline = pipeline("question-answering")
|
| 65 |
+
result = qa_pipeline(question=question, context=user_input)
|
| 66 |
+
st.write(f"**Answer:** {result['answer']}")
|
| 67 |
+
|
| 68 |
+
elif nlp_task == "Summarization":
|
| 69 |
+
st.subheader("βοΈ Summarization")
|
| 70 |
+
summarizer = pipeline("summarization")
|
| 71 |
+
summary = summarizer(user_input, max_length=50, min_length=25, do_sample=False)
|
| 72 |
+
st.write("**Summary:**")
|
| 73 |
+
st.write(summary[0]['summary_text'])
|
| 74 |
+
|
| 75 |
+
elif nlp_task == "Text Generation":
|
| 76 |
+
st.subheader("π Text Generation")
|
| 77 |
+
generator = pipeline("text-generation")
|
| 78 |
+
generated_text = generator(user_input, max_length=50, num_return_sequences=1)
|
| 79 |
+
st.write("**Generated Text:**")
|
| 80 |
+
st.write(generated_text[0]['generated_text'])
|
| 81 |
+
|
| 82 |
+
# Space for connecting this app to another space
|
| 83 |
+
st.sidebar.markdown("---")
|
| 84 |
+
st.sidebar.subheader("π Connect to Other Spaces")
|
| 85 |
+
other_space_url = st.sidebar.text_input("Enter the URL of another Streamlit app or Hugging Face space:")
|
| 86 |
+
if other_space_url:
|
| 87 |
+
st.sidebar.markdown(f"[Go to Connected Space]({other_space_url})")
|
| 88 |
+
|
| 89 |
+
st.sidebar.success("Explore NLP tasks or connect to another space!")
|