from os import O_ACCMODE import gradio as gr import joblib from transformers import pipeline import requests.exceptions from huggingface_hub import HfApi, hf_hub_download from huggingface_hub.repocard import metadata_load # work around for error, not happy really # import os # os.environ['KMP_DUPLICATE_LIB_OK']='True' app = gr.Blocks() model_1 = "juliensimon/distilbert-amazon-shoe-reviews" model_2 = "juliensimon/distilbert-amazon-shoe-reviews" def load_agent(model_id_1, model_id_2): """ This function load the agent's results """ # Load the metrics metadata_1 = get_metadata(model_id_1) # get predictions predictions_1 = predict(model_id_1) # Get the accuracy # results_1 = parse_metrics_accuracy(metadata_1) # Load the metrics metadata_2 = get_metadata(model_id_2) # get predictions predictions_2 = predict(model_id_2) # Get the accuracy # results_2 = parse_metrics_accuracy(metadata_2) return model_id_1, predictions_1, model_id_2, predictions_2 # def parse_metrics_accuracy(meta): # if "model-index" not in meta: # return None # result = meta["model-index"][0]["results"] # metrics = result[0]["metrics"] # accuracy = metrics[0]["value"] # return accuracy def get_metadata(model_id): """ Get the metadata of the model repo :param model_id: :return: metadata """ try: readme_path = hf_hub_download(model_id, filename="README.md") metadata = metadata_load(readme_path) print(metadata) return metadata except requests.exceptions.HTTPError: return None # classifier = pipeline("text-classification", model="juliensimon/distilbert-amazon-shoe-reviews") def predict(review, model_id): classifier = pipeline("text-classification", model=model_id) prediction = classifier(review) print(prediction) stars = prediction[0]['label'] stars = (int)(stars.split('_')[1])+1 score = 100*prediction[0]['score'] return "{} {:.0f}%".format("\U00002B50"*stars, score) with app: gr.Markdown( """ # Compare Sentiment Analysis Models Type text to predict sentiment. """) with gr.Row(): inp_1= gr.Textbox(label="Type text here.",placeholder="The customer service was satisfactory.") out_2 = gr.Textbox(label="Prediction") # gr.Markdown( # """ # Model Predictions # """) with gr.Row(): model1_input = gr.Textbox(label="Model 1") with gr.Row(): btn = gr.Button("Prediction for Model 1") btn.click(fn=predict(model_1), inputs=inp_1, outputs=out_2) with gr.Row(): model2_input = gr.Textbox(label="Model 2") with gr.Row(): btn = gr.Button("Prediction for Model 2") btn.click(fn=predict(model_2), inputs=inp_1, outputs=out_2) app_button.click(load_agent, inputs=[model1_input, model2_input], outputs=[model1_name, model1_score_output, model2_name, model2_score_output]) # examples = gr.Examples(examples=[["juliensimon/distilbert-amazon-shoe-reviews","juliensimon/distilbert-amazon-shoe-reviews"]], # inputs=[model1_input, model2_input]) app.launch()