File size: 2,288 Bytes
6976fe8 ec8f8b3 6976fe8 ec8f8b3 6976fe8 ec8f8b3 e5cfc2d a89c0f2 ec8f8b3 a89c0f2 ec8f8b3 6976fe8 ec8f8b3 6976fe8 ec8f8b3 e5cfc2d | 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 75 76 77 78 79 80 81 82 83 84 85 | import spaces
import gradio as gr
import pandas as pd
from app.infer import load, predict
TEXT_COLUMNS = ("text", "comment", "message", "content")
MAX_ROWS = 5000
load()
@spaces.GPU(duration=30)
def classify_text(text: str):
text = (text or "").strip()
if not text:
raise gr.Error("text is empty")
return predict([text])[0]
def _text_column(columns: list[str]) -> str:
lower = {c.lower(): c for c in columns}
for name in TEXT_COLUMNS:
if name in lower:
return lower[name]
raise gr.Error(f"CSV needs a text column. Tried: {', '.join(TEXT_COLUMNS)}")
@spaces.GPU(duration=60)
def classify_csv(file):
if file is None:
raise gr.Error("no CSV")
df = pd.read_csv(file)
if df.empty:
return pd.DataFrame()
col = _text_column(list(df.columns))
series = df[col].fillna("").astype(str).str.strip()
if len(series) > MAX_ROWS:
raise gr.Error(f"more than {MAX_ROWS} rows")
texts = []
rows = []
for idx, value in series.items():
if not value:
continue
texts.append(value)
rows.append(int(idx))
out = []
batch = 32
for i in range(0, len(texts), batch):
chunk = predict(texts[i : i + batch])
for row, item in zip(rows[i : i + batch], chunk):
item["row"] = row
out.append(item)
return pd.DataFrame(out)
with gr.Blocks(title="Bengali abusive classifier") as demo:
gr.Markdown("Bengali / romanized Bangla. Labels: `normal`, `abusive`. Latin text is transliterated first.")
with gr.Tab("Text"):
t_in = gr.Textbox(lines=6, label="paragraph")
t_out = gr.JSON(label="result")
gr.Button("Classify").click(
classify_text,
t_in,
t_out,
api_name="classify_text",
concurrency_limit=1,
)
with gr.Tab("CSV"):
f_in = gr.File(file_types=[".csv"], label="CSV with a text column")
f_out = gr.Dataframe(label="results")
gr.Button("Classify CSV").click(
classify_csv,
f_in,
f_out,
api_name="classify_csv",
concurrency_limit=1,
)
demo.queue(max_size=8)
if __name__ == "__main__":
demo.launch(ssr_mode=False)
|