ahella commited on
Commit
e92a48c
1 Parent(s): 4fbedc4

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1CbDOX8PDJB6ZyLZiLMXbPyr6k7dvrs20
8
+ """
9
+
10
+ !pip install gradio
11
+
12
+ import gradio as gr
13
+ import torch
14
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
15
+
16
+ # Load the model and tokenizer
17
+ model_name = "qarib/bert-base-qarib"
18
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
19
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
20
+
21
+ # Preprocessing function
22
+ def light_preprocess(text):
23
+ text = text.replace("@USER", "").replace("RT", "").strip()
24
+ return text
25
+
26
+ # Prediction function
27
+ def predict_offensive(text):
28
+ preprocessed_text = light_preprocess(text)
29
+ inputs = tokenizer(preprocessed_text, return_tensors="pt", truncation=True, padding=True)
30
+ with torch.no_grad():
31
+ outputs = model(**inputs)
32
+ logits = outputs.logits
33
+ predicted_class = torch.argmax(logits, dim=1).item()
34
+ return "Offensive" if predicted_class == 1 else "Not Offensive"
35
+
36
+ # Create the Gradio interface
37
+ iface = gr.Interface(
38
+ fn=predict_offensive,
39
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
40
+ outputs="text",
41
+ title="Offensive Language Detection",
42
+ description="Enter a text to check if it's offensive or not.",
43
+ )
44
+
45
+ # Launch the interface
46
+ iface.launch()