Team Ai
Apppublic

widged/text-classification

sourceHugging Faceupdated 5y agoView on Hugging Face
3likes
app.py46 linesDownload Raw Back to root
1import streamlit as st2from transformers import pipeline3import spacy4from spacy import displacy5import plotly.express as px6import numpy as np7st.set_page_config(page_title="Text Classification")8st.title("Text Classification'")9st.write("_This web application is intended for educational use, please do not upload any sensitive information._")10st.write("Placing a piece of text into one or more categories.")11 12@st.cache(allow_output_mutation=True, show_spinner=False)13def Loading_Classifier():14    class1 = pipeline("zero-shot-classification",framework="pt")15    return class116 17def plot_result(top_topics, scores):18    top_topics = np.array(top_topics)19    scores = np.array(scores)20    scores *= 10021    fig = px.bar(x=scores, y=top_topics, orientation='h',22                 labels={'x': 'Probability', 'y': 'Category'},23                 text=scores,24                 range_x=(0,115),25                 title='Top Predictions',26                 color=np.linspace(0,1,len(scores)),27                 color_continuous_scale="Bluered")28    fig.update(layout_coloraxis_showscale=False)29    fig.update_traces(texttemplate='%{text:0.1f}%', textposition='outside')30    st.plotly_chart(fig)31 32with st.spinner(text="Please wait for the models to load. This could take up to 60 seconds."):33    class1 = Loading_Classifier()34 35cat1 = st.text_input('Enter each possible category name (separated by a comma). Maximum 5 categories.')36text = st.text_area('Enter Text Below:', height=200)37submit = st.button('Generate')38if submit:39    st.subheader("Classification Results:")40    labels1 = cat1.strip().split(',')41    result = class1(text, candidate_labels=labels1)42    cat1name = result['labels'][0]43    cat1prob = result['scores'][0]44    st.write('Category: {} | Probability: {:.1f}%'.format(cat1name,(cat1prob*100)))45    plot_result(result['labels'][::-1][-10:], result['scores'][::-1][-10:])46