Spaces:
Sleeping
Sleeping
rifatramadhani
commited on
Commit
•
4edc781
1
Parent(s):
1f4fdb8
feat: hate speech detection
Browse files
app.py
CHANGED
@@ -1,7 +1,51 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
+
import torch
|
2 |
import gradio as gr
|
3 |
+
import os
|
4 |
+
from detoxify import Detoxify
|
5 |
+
import pandas as pd
|
6 |
+
import json
|
7 |
+
import spaces
|
8 |
+
import logging
|
9 |
+
import datetime
|
10 |
|
11 |
+
@spaces.GPU
|
12 |
+
def classify(query):
|
13 |
+
model = Detoxify("unbiased-small", device="cuda")
|
14 |
|
15 |
+
all_result = []
|
16 |
+
request_type = type(query)
|
17 |
+
try:
|
18 |
+
data = json.loads(query)
|
19 |
+
if type(data) != list:
|
20 |
+
data = [query]
|
21 |
+
else:
|
22 |
+
request_type = type(data)
|
23 |
+
except Exception as e:
|
24 |
+
print(e)
|
25 |
+
data = [query]
|
26 |
+
pass
|
27 |
+
|
28 |
+
for i in range(len(data)):
|
29 |
+
result = {}
|
30 |
+
start_time = datetime.datetime.now()
|
31 |
+
|
32 |
+
df = pd.DataFrame(model.predict(str(data[i])), index=[0])
|
33 |
+
columns = df.columns
|
34 |
+
|
35 |
+
for i, label in enumerate(columns):
|
36 |
+
result[label] = df[label][0].round(3).astype("float")
|
37 |
+
|
38 |
+
end_time = datetime.datetime.now()
|
39 |
+
elapsed_time = end_time - start_time
|
40 |
+
result["time"] = str(elapsed_time)
|
41 |
+
|
42 |
+
logging.debug("elapsed predict time: %s", str(elapsed_time))
|
43 |
+
print("elapsed predict time:", str(elapsed_time))
|
44 |
+
|
45 |
+
all_result.append(result)
|
46 |
+
|
47 |
+
|
48 |
+
return json.dumps(all_result) if request_type == list else all_result[0]
|
49 |
+
|
50 |
+
demo = gr.Interface(fn=classify, inputs=["text"], outputs="text")
|
51 |
demo.launch()
|