import gradio as gr from transformers import AutoTokenizer import torch from fastai.text.all import * # Define the path to your model model_path = "models/origin-classifier-stage-2.pkl" # Load the learner learner_inf = load_learner(model_path) # Create a mapping from class labels to indices class_label_mapping = {label: idx for idx, label in enumerate(learner_inf.dls.vocab)} # Define a function to make predictions def predict_text(text): prediction = learner_inf.blurr_predict(text)[0] predicted_class_index = prediction['class_index'] predicted_class_label = list(class_label_mapping.keys())[list(class_label_mapping.values()).index(predicted_class_index)] return predicted_class_label # Create a Gradio interface iface = gr.Interface( fn=predict_text, inputs="text", outputs="text", title="FoodOrigin Classification App", description="Enter a text, and it will predict the class label.", ) # Start the Gradio app iface.launch()