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