Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import transformers | |
| from transformers import pipeline | |
| # Load models | |
| text_classification = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest") | |
| ques_ans = pipeline("question-answering", model="deepset/roberta-base-squad2") | |
| summarization = pipeline("summarization", model="facebook/bart-large-cnn") | |
| st.title("NLP Task Reading ") | |
| # Task selector | |
| task = st.radio("Select NLP Task", ("Sentiment Analysis", "Question & Answer", "Summarization")) | |
| # Sentiment Analysis | |
| if task == "Sentiment Analysis": | |
| st.subheader("Sentiment Analysis") | |
| user_text = st.text_input("Enter text:") | |
| if user_text: | |
| prediction = text_classification(user_text)[0] | |
| confidence_percentage = prediction["score"] * 100 | |
| label = prediction["label"] | |
| statement = f"The model is {confidence_percentage:.2f}% confident that the sentiment is **{label}**." | |
| st.write(statement) | |
| # Question Answering | |
| elif task == "Question & Answer": | |
| st.subheader("Question & Answer") | |
| question = st.text_input("Question:") | |
| context = st.text_area("Context:") | |
| if question and context: | |
| result = ques_ans(question=question, context=context) | |
| answer = result["answer"] | |
| confidence = result["score"] * 100 | |
| st.write(f"The answer is: **{answer}**") | |
| st.write(f"The model is {confidence:.2f}% confident in this answer.") | |
| # Summarization | |
| elif task == "Summarization": | |
| st.subheader("Summarization") | |
| text_to_summarize = st.text_area("Enter text to summarize:") | |
| if text_to_summarize: | |
| summary = summarization(text_to_summarize)[0]["summary_text"] | |
| st.write(f"**Summary:** {summary}") | |