url-classifier / app.py
plomty's picture
Shift to url classifier
53332ed
raw history blame
No virus
2.47 kB
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
class UrlClassifier:
def __init__(self):
self.pipeline = pipeline(
"zero-shot-classification",
model="MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli"
)
self.candidate_labels = ["Apps", "Arts and Entertainment", "Automotive Ownership", "Automotive Products", "Beauty Services", "Business and Industrial", "Clothing and Accessories", "Collectables and Antiques", "Consumer Electronics", "Edible Consumer Packaged Goods", "Education and Careers", "Family and Parenting", "Finance and Insurance", "Food and Beverage Services", "Furniture", "Gifts and Holiday Items", "Hardware Supplies", "Health and Medical Services", "Hobbies and Interests", "Home and Garden Services", "Legal Services", "Life Events", "Logistics and Delivery", "Non-Profits", "Office Equipment and Supplies", "Pet Services", "Pharmaceuticals", "Real Estate", "Recreation and Fitness Activities", "Software", "Sporting Goods", "Travel and Tourism", "Web Services"]
def predict(self, input_text: str):
pred = self.pipeline(input_text, self.candidate_labels)
# pred = self.pipeline(input_text)[0]
sorted_preds = sorted(zip(pred['scores'], pred['labels']))
result = {}
for i in range(len(sorted_preds)):
result[sorted_preds[i][1]] = sorted_preds[i][0]
# result = {
# "Sadness 😭": pred[0]["score"],
# "Joy πŸ˜‚": pred[1]["score"],
# "Love 😍": pred[2]["score"],
# "Anger 😠": pred[3]["score"],
# "Fear 😨": pred[4]["score"],
# "Surprise 😲": pred[5]["score"],
# }
return result
def main():
model = UrlClassifier()
iface = gr.Interface(
fn=model.predict,
inputs=gr.inputs.Textbox(
lines=3,
placeholder="Input a shopping website URL",
label="Input URL",
),
outputs="label",
title="Ecommerce URL Classification",
examples=[
"I get so down when I'm alone",
"I believe that today everything will work out",
"It was so dark there I was afraid to go",
"I loved the gift you gave me",
"I was very surprised by your presentation.",
],
)
iface.launch()
if __name__ == "__main__":
main()