Spaces:
Sleeping
Sleeping
Download app/main.py from ogtega/datamining-project: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/ogtega/datamining-project/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/ogtega/datamining-project/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/ogtega/datamining-project/resolve/main/app/main.py
2.03 kB
| #!/usr/bin/env python3 | |
| import streamlit as st | |
| from transformers import AutoTokenizer | |
| from transformers import ( | |
| TFAutoModelForSequenceClassification as AutoModelForSequenceClassification, | |
| ) | |
| from transformers import pipeline | |
| st.title("Toxic Tweet Classifier") | |
| demo = """AI never ceases to amaze me! On one hand, it's incredible to see the progress we've made and the potential it holds for the future. On the other hand, it's hard not to feel a bit uneasy about its power and impact on our society.""" | |
| text = "" | |
| submit = False | |
| model_name = "" | |
| col1, col2, col3 = st.columns([2,1,1]) # Initiate columns | |
| with st.container(): # Container for model selection and submit button | |
| model_name = st.selectbox( | |
| "Select the model you want to use below.", | |
| ("ogtega/tweet-toxicity-classifier",), | |
| ) | |
| submit = st.button("Submit", type="primary", use_container_width=True) | |
| # Obtain the pre-trained model | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForSequenceClassification.from_pretrained(model_name) | |
| clf = pipeline( | |
| "sentiment-analysis", model=model, tokenizer=tokenizer, return_all_scores=True | |
| ) | |
| with col1: | |
| st.subheader("Tweet") | |
| text = st.text_area("Input text", demo, height=275) | |
| with col2: | |
| st.subheader("Classification") | |
| with col3: | |
| st.subheader("Probability") | |
| input = tokenizer(text, return_tensors="tf") # Run input text through tokenizer | |
| if submit: # Code ran after submit button is clicked | |
| results = dict(d.values() for d in clf(text)[0]) | |
| classes = {k: results[k] for k in results.keys() if not k == "toxic"} | |
| max_class = max(classes, key=classes.get) | |
| with col2: | |
| st.write(f"#### {max_class}") | |
| with col3: | |
| st.write(f"#### **{classes[max_class]:.2f}%**") | |
| if results["toxic"] < 0.5: | |
| st.success("This tweet is unlikely to be be toxic!", icon="✅") | |
| else: | |
| st.warning('This tweet is likely to be toxic.', icon="⚠️") | |
| expander = st.expander("Raw output") | |
| expander.write(results) | |