CoolFace
Apppublic

ahmadsanafarooq/Multi_Task_NLP_app

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