tattle-made / Uli

Software and Resources for Mitigating Online Gender Based Violence in India
https://uli.tattle.co.in
GNU General Public License v3.0
40 stars 29 forks source link

Description for ogbv-ml-rest model on Hugging Face #362

Closed aatmanvaidya closed 8 months ago

aatmanvaidya commented 1 year ago

This is the ogbv-ml-rest model loaded on hugging face - https://huggingface.co/tattle-admin/july22-xlmtwtroberta-da-multi

The model is automatically loaded in a hosted inference API, where users can test it in the UI of hugging face. We just need to add a detailed description (code and text) on what is the model? and how to use the model.

I have written the code part of it.

This is a good example of how a text classification model should look on hugging face - https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest

Code Example

This is a colab notebook where the code is available if someone wants to test it out - https://colab.research.google.com/drive/1vKuIkbOrBcGRKcKQAcg-67vV1lmZBOQE?usp=sharing

Example Pipeline

# Use a pipeline as a high-level helper
from transformers import pipeline
sentiment_task = pipeline("text-classification", model="tattle-admin/july22-xlmtwtroberta-da-multi")
sentiment_task("Use Uli to redact slurs and abusive content, archive problematic content, and collectively push back against online gender based violence")
# LABEL MEANING - LABEL_0 = None and LABEL_1 = Hate

Full classification example

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import numpy as np
from scipy.special import softmax

tokenizer = AutoTokenizer.from_pretrained("tattle-admin/july22-xlmtwtroberta-da-multi")
model = AutoModelForSequenceClassification.from_pretrained("tattle-admin/july22-xlmtwtroberta-da-multi")

label_map = {
        0: 'None', 
        1: 'Hate'
    }

def preprocess(text):
    new_text = []
    for t in text.split(" "):
        t = '@user' if t.startswith('@') and len(t) > 1 else t
        t = 'http' if t.startswith('http') else t
        new_text.append(t)
    return " ".join(new_text)

text = "Use Uli to redact slurs and abusive content, archive problematic content, and collectively push back against online gender based violence"
clean_text = preprocess(text)

encoded_input = tokenizer(clean_text, return_tensors='pt')
output = model(**encoded_input)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
# Print labels and scores
ranking = np.argsort(scores)
ranking = ranking[::-1]
for i in range(scores.shape[0]):
    l = label_map[ranking[i]]
    s = scores[ranking[i]]
    print(f"{i+1}) {l} {np.round(float(s), 4)}")
# LABEL MEANING - LABEL_0 = None and LABEL_1 = Hate

Output

1) None 0.8103
2) Hate 0.1897
github-actions[bot] commented 12 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 8 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.