Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +58 -29
src/streamlit_app.py
CHANGED
|
@@ -2,39 +2,68 @@ import altair as alt
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 10 |
|
| 11 |
+
@st.cache_resource(show_spinner=False)
|
| 12 |
+
def load_model():
|
| 13 |
+
# Load your fine-tuned model and tokenizer
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained("CustomModel")
|
| 15 |
+
model = BertForSequenceClassification.from_pretrained("CustomModel")
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
return tokenizer, model, device
|
| 19 |
|
| 20 |
+
tokenizer, model, device = load_model()
|
|
|
|
| 21 |
|
| 22 |
+
st.title("Batch Toxic Comment Classifier")
|
| 23 |
+
st.write("Upload a CSV file containing text comments and get toxicity scores for each row.")
|
| 24 |
|
| 25 |
+
# CSV upload
|
| 26 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 27 |
+
if uploaded_file is not None:
|
| 28 |
+
df = pd.read_csv(uploaded_file)
|
| 29 |
|
| 30 |
+
# Let user select which column contains text
|
| 31 |
+
text_cols = df.select_dtypes(include=["object"]).columns.tolist()
|
| 32 |
+
if not text_cols:
|
| 33 |
+
st.error("No text columns found in the uploaded file.")
|
| 34 |
+
else:
|
| 35 |
+
col = st.selectbox("Select text column to classify", text_cols)
|
| 36 |
+
if st.button("Classify CSV"):
|
| 37 |
+
texts = df[col].astype(str).tolist()
|
| 38 |
+
results = []
|
| 39 |
+
|
| 40 |
+
# Batch inference
|
| 41 |
+
for text in texts:
|
| 42 |
+
inputs = tokenizer(
|
| 43 |
+
text,
|
| 44 |
+
padding=True,
|
| 45 |
+
truncation=True,
|
| 46 |
+
return_tensors="pt"
|
| 47 |
+
).to(device)
|
| 48 |
+
outputs = model(**inputs)
|
| 49 |
+
probs = F.softmax(outputs.logits, dim=-1).detach().cpu().numpy()[0]
|
| 50 |
+
id2label = model.config.id2label if hasattr(model.config, "id2label") else {0: "non-toxic", 1: "toxic"}
|
| 51 |
+
# record per-row scores
|
| 52 |
+
row_res = {id2label[i]: float(probs[i]) for i in range(len(probs))}
|
| 53 |
+
results.append(row_res)
|
| 54 |
|
| 55 |
+
# Combine with original
|
| 56 |
+
score_df = pd.DataFrame(results)
|
| 57 |
+
combined = pd.concat([df.reset_index(drop=True), score_df], axis=1)
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
st.subheader("Classification Results")
|
| 60 |
+
st.dataframe(combined)
|
| 61 |
+
|
| 62 |
+
# Optional: download results
|
| 63 |
+
csv = combined.to_csv(index=False).encode('utf-8')
|
| 64 |
+
st.download_button(
|
| 65 |
+
label="Download results as CSV",
|
| 66 |
+
data=csv,
|
| 67 |
+
file_name="classified_results.csv",
|
| 68 |
+
mime="text/csv"
|
| 69 |
+
)
|