jynzo94 commited on
Commit
7e5e60c
·
1 Parent(s): 0688f83

lang detection, translator, offensive detection

Browse files
Files changed (1) hide show
  1. app.py +46 -10
app.py CHANGED
@@ -1,31 +1,67 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load language detection model from Hugging Face
5
  lang_classifier = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
6
 
7
- # Load hate speech detection model from Hugging Face
8
- hate_classifier = pipeline("text-classification", model="Hate-speech-CNERG/dehatebert-mono-english")
 
 
 
 
 
 
 
 
 
 
9
 
10
  def analyze_text(text):
11
  if not text.strip():
12
  return {"error": "No text provided"}, {"error": "No text provided"}
13
 
 
14
  lang_result = lang_classifier(text)
15
- hate_result = hate_classifier(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- language_output = {"language": lang_result[0]['label'], "confidence": lang_result[0]['score']}
18
- hate_output = {"label": hate_result[0]['label'], "score": hate_result[0]['score']}
 
 
 
 
 
 
 
 
 
19
 
20
  return language_output, hate_output
21
 
22
  # Define the Gradio interface
23
  iface = gr.Interface(
24
- fn=analyze_text, # Use the combined function
25
  inputs=gr.Textbox(label="Enter text"),
26
- outputs=[gr.JSON(label="Language Detection"), gr.JSON(label="Hate Speech Detection")],
27
- title="Language & Hate Speech Detection",
28
- description="Enter text to detect its language and check for hate speech."
 
 
 
29
  )
30
 
31
  # Launch the Gradio app
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load language detection model
5
  lang_classifier = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
6
 
7
+ # Load translation model (multi-language to English)
8
+ translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
9
+
10
+ # Load hate speech detection model
11
+ offensive_classifier = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-offensive")
12
+
13
+ # Mapping from ISO 639-1 to NLLB-200 language codes
14
+ LANGUAGE_CODES = {
15
+ "en": "eng_Latn", "fr": "fra_Latn", "es": "spa_Latn", "de": "deu_Latn",
16
+ "bg": "bul_Cyrl", "ru": "rus_Cyrl", "it": "ita_Latn", "zh": "zho_Hans",
17
+ "ar": "arb_Arab", "pt": "por_Latn", "nl": "nld_Latn", "hi": "hin_Deva"
18
+ }
19
 
20
  def analyze_text(text):
21
  if not text.strip():
22
  return {"error": "No text provided"}, {"error": "No text provided"}
23
 
24
+ # Detect language
25
  lang_result = lang_classifier(text)
26
+ detected_language = lang_result[0]['label']
27
+ language_confidence = lang_result[0]['score']
28
+
29
+ # Convert detected language to NLLB-200 format
30
+ detected_language_nllb = LANGUAGE_CODES.get(detected_language, "eng_Latn")
31
+
32
+ # Translate if not English
33
+ translated_text = text
34
+ if detected_language_nllb != "eng_Latn":
35
+ translation_result = translator(text, src_lang=detected_language_nllb, tgt_lang="eng_Latn")
36
+ translated_text = translation_result[0]['translation_text']
37
+
38
+ # Detect hate speech using the translated text
39
+ hate_result = offensive_classifier(translated_text)
40
 
41
+ language_output = {
42
+ "language": detected_language,
43
+ "confidence": language_confidence,
44
+ "original_text": text,
45
+ "translated_text": translated_text if detected_language_nllb != "eng_Latn" else "Already in English"
46
+ }
47
+
48
+ hate_output = {
49
+ "label": hate_result[0]['label'],
50
+ "score": hate_result[0]['score']
51
+ }
52
 
53
  return language_output, hate_output
54
 
55
  # Define the Gradio interface
56
  iface = gr.Interface(
57
+ fn=analyze_text,
58
  inputs=gr.Textbox(label="Enter text"),
59
+ outputs=[
60
+ gr.JSON(label="Language Detection & Translation"),
61
+ gr.JSON(label="Hate Speech Detection")
62
+ ],
63
+ title="Language & Hate Speech Detection with Translation",
64
+ description="Enter text to detect its language, translate it to English if needed, and check for hate speech."
65
  )
66
 
67
  # Launch the Gradio app