SoDehghan's picture
Update models/test.py
237ad46 verified
import streamlit as st
from transformers import pipeline
import openai
# Turkish
#sentiment_pipeline_tr = pipeline(task = "text-classification", model = "SoDehghan/BERTurk-hate-speech-detection") # "gritli/bert-sentiment-analyses-imdb"
#sentiment_pipeline_tr_test = pipeline(task = "text-classification", model = "SoDehghan/test")
strength_pipeline_tr = pipeline(task = "text-classification", model = "SoDehghan/BERTurk-hate-speech-strength-prediction")
def write():
st.markdown(
"""
# Hate Speech Detection in Turkish
"""
)
def determine_hate_terms (chunk):
response = openai.ChatCompletion.create(
model= "gpt-3.5-turbo-1106",
messages= [{"role":"system", "content":"This is a tool for detecting hate speech and preventing spread of them on social networks\n1. Please help us\n2. Use encoding=utf8"},
{"role": "user", "content":f"determine which words in this text contain hateful sentiment:{chunk}"}])
return (response['choices'][0]['message']['content'])
tr_input = st.text_area("Enter your text here:", height=50, key="tr_input") #height=30
if st.button("Evaluate", key="tr_predict"):
st.write(" ")
with st.spinner('Generating predictions...'):
#result_sentiment_tr = sentiment_pipeline_tr(tr_input)
#sentiment_tr = result_sentiment_tr[0]["label"]
#label_dict_sentiment = {'LABEL_1': 'Detection: Hate ❌', 'LABEL_0': 'Detection: Non-hate βœ…'}
#sentiment_tr = label_dict_sentiment[sentiment_tr]
#result_sentiment_tr_test = sentiment_pipeline_tr_test(tr_input)
#sentiment_tr_test = result_sentiment_tr_test[0]["label"]
#label_dict_sentiment = {'LABEL_1': 'Detection: Hate ❌', 'LABEL_0': 'Detection: Non-hate βœ…'}
#sentiment_tr_test = label_dict_sentiment[sentiment_tr_test]
result_strength_tr = strength_pipeline_tr(tr_input)
strength_tr = result_strength_tr[0]["label"]
#label_dict_strength = {'LABEL_0': ' 0 (No hate speech)',
# 'LABEL_1': ' 1 (Exaggeration/Generalization/Distortion)',
# 'LABEL_2': ' 2 (Symbolization)',
# 'LABEL_3': ' 3 (Swearing/Insult/Humiliation)',
# 'LABEL_4': ' 4 (Hostility/Attack/Threat of Injury)'}
label_dict_strength = {'LABEL_0': ' 0 ',
'LABEL_1': ' 1 ',
'LABEL_2': ' 2 ',
'LABEL_3': ' 3 ',
'LABEL_4': ' 4 '}
label_dict_sentiment = {'LABEL_0': ' Non-hate βœ…', 'LABEL_1': ' Hate ❌', 'LABEL_2': ' Hate ❌', 'LABEL_3': ' Hate ❌', 'LABEL_4': ' Hate ❌'}
openai.api_key = ''
#hate_terms = determine_hate_terms(tr_input)
sentiment_tr = label_dict_sentiment[strength_tr]
strength_tr = label_dict_strength[strength_tr]
st.write(f" **Binary Classification:** {sentiment_tr}")
st.write(f" **Strength of Hate:** {strength_tr}")
#st.write(' ')
#st.write(f" **ChatGPT Prediction:** {hate_terms}")
#st.write(hate_terms)
#st.write(sentiment_tr_test)
#st.success(sentiment_tr)
#st.success(strength_tr)