new
Browse files- app.py +29 -0
- requirements.txt +4 -0
app.py
CHANGED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import onnxruntime as rt
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
import torch, json
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("distilroberta-base")
|
7 |
+
|
8 |
+
with open("types_encoded.json", "r") as fp:
|
9 |
+
encode_types = json.load(fp)
|
10 |
+
|
11 |
+
recipe = list(encode_types.keys())
|
12 |
+
|
13 |
+
inf_session = rt.InferenceSession('recipe-classifier-quantized.onnx')
|
14 |
+
input_name = inf_session.get_inputs()[0].name
|
15 |
+
output_name = inf_session.get_outputs()[0].name
|
16 |
+
|
17 |
+
def classify_recipe(description):
|
18 |
+
input_ids = tokenizer(description)['input_ids'][:512]
|
19 |
+
logits = inf_session.run([output_name], {input_name: [input_ids]})[0]
|
20 |
+
logits = torch.FloatTensor(logits)
|
21 |
+
probs = torch.sigmoid(logits)[0]
|
22 |
+
return dict(zip(recipe, map(float, probs)))
|
23 |
+
|
24 |
+
label = gr.outputs.Label(num_top_classes=5)
|
25 |
+
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
iface = gr.Interface(fn=classify_recipe, inputs="text", outputs=label)
|
29 |
+
iface.launch(inline=False)
|
requirements.txt
CHANGED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.17.0
|
2 |
+
onnxruntime==1.13.1
|
3 |
+
torch==1.13.1
|
4 |
+
transformers==4.26.0
|