Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import transformers
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load models
|
| 6 |
+
text_classification = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
| 7 |
+
ques_ans = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 8 |
+
summarization = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 9 |
+
|
| 10 |
+
st.title("NLP Task Reading ")
|
| 11 |
+
|
| 12 |
+
# Task selector
|
| 13 |
+
task = st.radio("Select NLP Task", ("Sentiment Analysis", "Question & Answer", "Summarization"))
|
| 14 |
+
|
| 15 |
+
# Sentiment Analysis
|
| 16 |
+
if task == "Sentiment Analysis":
|
| 17 |
+
st.subheader("Sentiment Analysis")
|
| 18 |
+
user_text = st.text_input("Enter text:")
|
| 19 |
+
if user_text:
|
| 20 |
+
prediction = text_classification(user_text)[0]
|
| 21 |
+
confidence_percentage = prediction["score"] * 100
|
| 22 |
+
label = prediction["label"]
|
| 23 |
+
statement = f"The model is {confidence_percentage:.2f}% confident that the sentiment is **{label}**."
|
| 24 |
+
st.write(statement)
|
| 25 |
+
|
| 26 |
+
# Question Answering
|
| 27 |
+
elif task == "Question & Answer":
|
| 28 |
+
st.subheader("Question & Answer")
|
| 29 |
+
question = st.text_input("Question:")
|
| 30 |
+
context = st.text_area("Context:")
|
| 31 |
+
if question and context:
|
| 32 |
+
result = ques_ans(question=question, context=context)
|
| 33 |
+
answer = result["answer"]
|
| 34 |
+
confidence = result["score"] * 100
|
| 35 |
+
st.write(f"The answer is: **{answer}**")
|
| 36 |
+
st.write(f"The model is {confidence:.2f}% confident in this answer.")
|
| 37 |
+
|
| 38 |
+
# Summarization
|
| 39 |
+
elif task == "Summarization":
|
| 40 |
+
st.subheader("Summarization")
|
| 41 |
+
text_to_summarize = st.text_area("Enter text to summarize:")
|
| 42 |
+
if text_to_summarize:
|
| 43 |
+
summary = summarization(text_to_summarize)[0]["summary_text"]
|
| 44 |
+
st.write(f"**Summary:** {summary}")
|
| 45 |
+
|