import streamlit as st from transformers import pipeline #strength_pipeline_tr = pipeline(task = "text-classification", model = "SoDehghan/test-strength") strength_pipeline_tr = pipeline(task = "text-classification", model = "SoDehghan/BERTurk-hate-speech-strength-prediction") target_pipeline_tr = pipeline(task = "text-classification", model = "SoDehghan/test-target") TR_TWEETS_EXAMPLES = [ "Mülteciler ölsün istiyorum.", "Mülteciler ölsünler.", "Bütün mülteciler hırsızdır.", "Mültecilerden nefret ediyorum.", "Defolun gidin bizim ülkemizden.", "Suriyelilerin yemekleri çok güzel.", "Mültecileri seviyorum.", "Kadınlar dizinizi kırıp evde oturun yoksa tacize uğrarsınız, demedi demeyein!", "Yunan haddını bilecek yoksa bir gece ansızın gelebiliriz!", "Pazardan elma aldim.", ] NON_HATEFUL_RESPONSE = 'This content is classified as "non-hate" speech. ✅😊' HATEFUL_RESPONSE = 'This content is classified as "hate" speech. ❌😔' strength_mapping = { 'LABEL_0': 'Severity Level: 0 - No Hate Detected', 'LABEL_1': 'Severity Level: 1 - Mild (Insult)', 'LABEL_2': 'Severity Level: 2 - Moderate (Exclusion)', 'LABEL_3': 'Severity Level: 3 - Severe (Wishing Harm)', 'LABEL_4': 'Severity Level: 4 - Extreme (Threatening Harm)' } strength_mapping = { 'LABEL_0': 'Severity Level: 0 - No Hate Detected', 'LABEL_1': 'Severity Level: 1 - Mild (Symbolization)', 'LABEL_2': 'Severity Level: 2 - Moderate (Exaggeration, Generalization, Attribution, Distortion)', 'LABEL_3': 'Severity Level: 3 - Severe (Swearing, Insult, Defamation, Dehumanization)', 'LABEL_4': 'Severity Level: 4 - Extreme (Threat of enmnity, war, attack, murder, harm)' } sentiment_mapping = { 'LABEL_0': NON_HATEFUL_RESPONSE, 'LABEL_1': HATEFUL_RESPONSE, 'LABEL_2': HATEFUL_RESPONSE, 'LABEL_3': HATEFUL_RESPONSE, 'LABEL_4': HATEFUL_RESPONSE } target_mapping ={ 'LABEL_0': 'Refugees/Immigrants', 'LABEL_1': 'Country/Nationality', 'LABEL_2': 'Gender (female/LGBTQ+)' } def remove_punctuation_marks(text): punc = '''!()-[]{};:'"\,<>./?$%^&*~''' for i in text: if i in punc: text = text.replace(i, " ") return text def sidebar_callback(): st.session_state['tr_input'] = st.session_state['sidebar'] def write(): if 'tr_input' not in st.session_state: st.session_state['tr_input'] = "" st.markdown( """ # Hate Speech Detection in Turkish tweets """ ) # st.markdown(""" # A brief description of the model and the task it was trained on. # """) tr_input = st.text_area("Enter text for analysis:", height=50, key="tr_input") # Examples dropdown on = st.toggle("Show predefined examples to test") if on: st.selectbox("Select a predefined example to test:", TR_TWEETS_EXAMPLES, key='sidebar', on_change=sidebar_callback) # Prediction button if st.button("Analyze Text", key="tr_predict"): st.write(" ") with st.spinner('Generating predictions...'): result_strength_tr = strength_pipeline_tr(remove_punctuation_marks(tr_input)) strength_tr = result_strength_tr[0]["label"] sentiment_tr = sentiment_mapping[strength_tr] strength_tr = strength_mapping[strength_tr] if strength_tr == 'Severity Level: 0 - No Hate Detected': target_tr = 'There is no hate speech against target groups' else: result_target_tr = target_pipeline_tr(tr_input) target_tr = result_target_tr[0]["label"] target_tr = target_mapping[target_tr] st.write(f" **Binary Classification:** {sentiment_tr}") st.write(f" **Strength of Hate:** {strength_tr}") st.write(f" **Target Group:** {target_tr}")