sandbox338 commited on
Commit
d962114
·
verified ·
1 Parent(s): 3cc2689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -1,22 +1,39 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load the model
5
  classifier = pipeline("text-classification", model="sandbox338/hatespeech")
6
 
7
- # Define function to return only the label
 
 
 
 
 
 
 
8
  def classify_text(text):
9
  result = classifier(text)[0]
10
- return result['label']
 
 
 
 
 
 
 
 
11
 
12
- # Gradio interface
13
  interface = gr.Interface(
14
  fn=classify_text,
15
- inputs=gr.Textbox(lines=4, placeholder="Enter Swahili text here..."),
16
  outputs="text",
17
  title="Swahili Hate Speech Classifier",
18
- description="Classifies text as one of: Non-hate speech, Political hate speech, or Offensive language."
 
19
  )
20
 
21
  if __name__ == "__main__":
22
  interface.launch()
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load model from Hugging Face Hub
5
  classifier = pipeline("text-classification", model="sandbox338/hatespeech")
6
 
7
+ # Map model labels to readable labels
8
+ label_map = {
9
+ "LABEL_0": "Non-hate speech",
10
+ "LABEL_1": "Political hate speech",
11
+ "LABEL_2": "Offensive language"
12
+ }
13
+
14
+ # Classification function
15
  def classify_text(text):
16
  result = classifier(text)[0]
17
+ label = result['label']
18
+ return label_map.get(label, "Unknown")
19
+
20
+ # Example inputs for testing
21
+ examples = [
22
+ ["Hii ni ujumbe wa kawaida bila matusi."],
23
+ ["Wanasiasa hawa ni wabaya na lazima waondoke!"],
24
+ ["Unasema upuuzi na wewe ni mjinga kabisa!"]
25
+ ]
26
 
27
+ # Gradio Interface
28
  interface = gr.Interface(
29
  fn=classify_text,
30
+ inputs=gr.Textbox(lines=4, placeholder="Andika maandishi ya Kiswahili hapa..."),
31
  outputs="text",
32
  title="Swahili Hate Speech Classifier",
33
+ description="Classifies Swahili text as one of the following:\n- Non-hate speech\n- Political hate speech\n- Offensive language",
34
+ examples=examples
35
  )
36
 
37
  if __name__ == "__main__":
38
  interface.launch()
39
+