import gradio as gr from fastai.text.all import load_learner import os import pathlib import torch ## Workaround for Windows Path - PathPosix issue # temp = pathlib.PosixPath # pathlib.PosixPath = pathlib.WindowsPath def predict(text): # Directly use the string path model_path = "models/LikeItOrNot-base-v1.pkl" learner = load_learner(model_path) prediction = learner.predict(text) confidence_score = torch.tensor(prediction[2]).max() confidence= f"{confidence_score:.2%}" if str(prediction[0]) == "1": label = "NEGATIVE" elif str(prediction[0]) == "2": label = "POSITIVE" else: label = "dunno" output = f"Prediction: {label} \n Confidence: {confidence}" print(prediction) return output, confidence iface = gr.Interface(fn=predict, inputs="text", outputs=["label", "text"]) iface.launch()