Spaces:
Sleeping
Sleeping
File size: 2,785 Bytes
a7df232 ef21555 e72e718 a7df232 e72e718 a7df232 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import streamlit as st
from transformers import pipeline
st.set_page_config(page_title="Multi-Task NLP Hub", page_icon="π€")
st.title("π€ Multi-Task NLP ")
st.write(
"Choose an NLP task and enter your text below."
)
TASKS = {
"Sentiment Analysis": {
"pipeline": "sentiment-analysis",
"model": "distilbert-base-uncased-finetuned-sst-2-english",
"description": "Classify text as positive or negative sentiment.",
},
"Summarization": {
"pipeline": "summarization",
"model": "sshleifer/distilbart-cnn-12-6",
"description": "Summarize long articles or text passages.",
},
"Translation (English β French)": {
"pipeline": "translation_en_to_fr",
"model": "Helsinki-NLP/opus-mt-en-fr",
"description": "Translate English text to French.",
},
"Text Generation": {
"pipeline": "text-generation",
"model": "gpt2",
"description": "Generate text based on a prompt.",
},
}
task = st.selectbox("Choose NLP Task:", list(TASKS.keys()))
st.caption(TASKS[task]["description"])
user_input = st.text_area("Enter your text:", height=150)
@st.cache_resource
def get_pipeline(task_name):
t = TASKS[task_name]
if task_name == "Translation (English β French)":
return pipeline("translation_en_to_fr", model=t["model"])
else:
return pipeline(t["pipeline"], model=t["model"])
nlp = get_pipeline(task)
if st.button("Run"):
if not user_input.strip():
st.warning("Please enter some text first.")
else:
with st.spinner("Processing..."):
if task == "Summarization":
# Summarization expects max 1024 tokens, so we truncate for demo
result = nlp(user_input[:1024], max_length=130, min_length=30, do_sample=False)
summary = result[0]["summary_text"]
st.markdown("**Summary:**")
st.success(summary)
elif task == "Sentiment Analysis":
result = nlp(user_input)
label = result[0]["label"]
score = result[0]["score"]
st.markdown(f"**Sentiment:** `{label}` \n**Confidence:** `{score:.2%}`")
elif task == "Translation (English β French)":
result = nlp(user_input)
translation = result[0]["translation_text"]
st.markdown("**French Translation:**")
st.success(translation)
elif task == "Text Generation":
# For demo, limit to 50 tokens
result = nlp(user_input, max_length=50, num_return_sequences=1)
generated = result[0]["generated_text"]
st.markdown("**Generated Text:**")
st.info(generated) |