Spaces:
Build error
Build error
Download app.py from chitlchow/ai-text-classifier: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/spaces/chitlchow/ai-text-classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/chitlchow/ai-text-classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/chitlchow/ai-text-classifier/resolve/main/app.py
924 Bytes
| import gradio as gr | |
| import torch | |
| from src.model import BertClassifier, RobertaClassifier | |
| from transformers import BertTokenizer | |
| from datetime import datetime | |
| device = torch.device('cpu') | |
| model_name = 'bert-base-uncased' | |
| model = BertClassifier(model_name, 0.5) | |
| model.to(device) | |
| model.load_state_dict(torch.load('models/bert-all-data.pth', map_location=device)) | |
| tokenizer = BertTokenizer.from_pretrained(model_name) | |
| def ai_text_classifier(text: str) -> dict: | |
| # Convert Text into tokens | |
| tokens = tokenizer(text, return_tensors='pt', max_length=512, padding='max_length', truncation=True).to(device) | |
| # Get probability of the text | |
| prob = model(tokens['input_ids'], tokens['attention_mask']).item() | |
| # Return the probability in dictionary | |
| return { | |
| "AI": prob, | |
| 'Others': 1 - prob | |
| } | |
| demo = gr.Interface(fn=ai_text_classifier, inputs="text", outputs="label") | |
| demo.launch() |