Download app.py from Rhasan97/Recipe: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/spaces/Rhasan97/Recipe/resolve/main/app.py
- Command line
-
hf download hf://spaces/Rhasan97/Recipe/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Rhasan97/Recipe/resolve/main/app.py
924 Bytes
| import gradio as gr | |
| import onnxruntime as rt | |
| from transformers import AutoTokenizer | |
| import torch, json | |
| tokenizer = AutoTokenizer.from_pretrained("roberta-base") | |
| with open("recipe_types_encoded19.json", "r") as fp: | |
| encode_types = json.load(fp) | |
| recipe = list(encode_types.keys()) | |
| inf_session = rt.InferenceSession('recipe-classifier.onnx') | |
| input_name = inf_session.get_inputs()[0].name | |
| output_name = inf_session.get_outputs()[0].name | |
| def classify_recipe(description): | |
| input_ids = tokenizer(description)['input_ids'][:512] | |
| logits = inf_session.run([output_name], {input_name: [input_ids]})[0] | |
| logits = torch.FloatTensor(logits) | |
| probs = torch.sigmoid(logits)[0] | |
| result = {class_label: float(prob) for class_label, prob in zip(recipe,probs)} | |
| return result | |
| label = gr.outputs.Label(num_top_classes=5) | |
| iface = gr.Interface(fn=classify_recipe, inputs="text", outputs=label) | |
| iface.launch(inline=False) |