asony999's picture
Update app.py
1f92523 verified
import streamlit as st
from transformers import pipeline
# Title of the app
st.title("Multi-Task NLP App with Transformers")
st.write("Explore advanced NLP tasks: Sentiment Analysis, Text Summarization, and Question Answering.")
# Load pre-trained models
sentiment_analyzer = pipeline("sentiment-analysis")
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
qa_pipeline = pipeline("question-answering")
# Sidebar for task selection
task = st.sidebar.selectbox("Choose a Task", ["Sentiment Analysis", "Text Summarization", "Question Answering"])
if task == "Sentiment Analysis":
st.header("Sentiment Analysis")
user_input = st.text_area("Enter text for sentiment analysis:")
if st.button("Analyze Sentiment"):
if user_input:
result = sentiment_analyzer(user_input)
st.write("Sentiment Analysis Result:")
st.write(result)
else:
st.write("Please enter some text to analyze.")
elif task == "Text Summarization":
st.header("Text Summarization")
# User input
user_input = st.text_area("Enter text to summarize:")
# Summarization parameters
max_length = st.slider("Max Length of Summary", 50, 150, 100)
min_length = st.slider("Min Length of Summary", 10, 50, 25)
if st.button("Summarize"):
if user_input:
try:
# Generate summary
result = summarizer(user_input, max_length=max_length, min_length=min_length, do_sample=False)
st.write("Summary:")
st.write(result[0]['summary_text'])
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.write("Please enter some text to summarize.")
elif task == "Question Answering":
st.header("Question Answering")
context = st.text_area("Enter context (the text to ask questions about):")
question = st.text_input("Enter your question:")
if st.button("Get Answer"):
if context and question:
result = qa_pipeline(question=question, context=context)
st.write("Answer:")
st.write(result['answer'])
else:
st.write("Please provide both context and question.")