ShaopengZhang commited on
Commit
e6fc786
1 Parent(s): 7b55cdb

add requirements/app

Browse files
Files changed (2) hide show
  1. app.py +45 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import string
2
+ import gradio as gr
3
+ import requests
4
+ import torch
5
+ from transformers import (
6
+ AutoConfig,
7
+ AutoModelForSequenceClassification,
8
+ AutoTokenizer,
9
+ )
10
+
11
+ model_dir = "my-bert-model"
12
+
13
+ config = AutoConfig.from_pretrained(model_dir, num_labels=3, finetuning_task="text-classification")
14
+ tokenizer = AutoTokenizer.from_pretrained(model_dir)
15
+ model = AutoModelForSequenceClassification.from_pretrained(model_dir, config=config)
16
+
17
+ def inference(input_text):
18
+ inputs = tokenizer.batch_encode_plus(
19
+ [input_text],
20
+ max_length=512,
21
+ pad_to_max_length=True,
22
+ truncation=True,
23
+ padding="max_length",
24
+ return_tensors="pt",
25
+ )
26
+
27
+ with torch.no_grad():
28
+ logits = model(**inputs).logits
29
+
30
+ predicted_class_id = logits.argmax().item()
31
+ output = model.config.id2label[predicted_class_id]
32
+ return output
33
+
34
+ demo = gr.Interface(
35
+ fn=inference,
36
+ inputs=gr.Textbox(label="Input Text", scale=2, container=False),
37
+ outputs=gr.Textbox(label="Output Label"),
38
+ examples = [
39
+ ["My last two weather pics from the storm on August 2nd. People packed up real fast after the temp dropped and winds picked up.", 1],
40
+ ["Lying Clinton sinking! Donald Trump singing: Let's Make America Great Again!", 0],
41
+ ],
42
+ title="Tutorial: BERT-based Text Classificatioin",
43
+ )
44
+
45
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==4.30.2
2
+ torch==2.0.0
3
+ ~