Spaces:
Running
Running
| # import sklearn | |
| 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 | |
| app = gr.Blocks() | |
| model_id_1 = "nlptown/bert-base-multilingual-uncased-sentiment" | |
| model_id_2 = "microsoft/deberta-base" | |
| model_id_3 = "juliensimon/distilbert-amazon-shoe-reviews" | |
| def load_agent(model_id): | |
| """ | |
| This function load the agent's results | |
| """ | |
| # Load the metrics | |
| metadata = get_metadata(model_id) | |
| # get predictions | |
| predictions = predict(model_id) | |
| return model_id, predictions | |
| 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 | |
| def get_prediction(model_id): | |
| classifier = pipeline("text-classification", model=model_id, return_all_scores=True) | |
| def predict(review): | |
| prediction = classifier(review) | |
| print(prediction) | |
| return prediction | |
| return predict | |
| 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.") | |
| gr.Markdown( | |
| """ | |
| **Model Predictions** | |
| """) | |
| gr.Markdown( | |
| """ | |
| Model 1 = nlptown/bert-base-multilingual-uncased-sentiment | |
| """) | |
| with gr.Row(): | |
| btn1 = gr.Button("Predict for Model 1") | |
| with gr.Row(): | |
| out_1 = gr.Textbox(label="Prediction for Model 1") | |
| btn1.click(fn=get_prediction(model_id_1), inputs=inp_1, outputs=out_1) | |
| gr.Markdown( | |
| """ | |
| Model 2 = microsoft/deberta-base | |
| """) | |
| with gr.Row(): | |
| btn2 = gr.Button("Predict for Model 2") | |
| with gr.Row(): | |
| out_2 = gr.Textbox(label="Prediction for Model 2") | |
| btn2.click(fn=get_prediction(model_id_2), inputs=inp_1, outputs=out_2) | |
| gr.Markdown( | |
| """ | |
| Model 3 = juliensimon/distilbert-amazon-shoe-reviews | |
| """) | |
| with gr.Row(): | |
| btn3 = gr.Button("Predict for Model 3") | |
| with gr.Row(): | |
| out_3 = gr.Textbox(label="Prediction for Model 3") | |
| btn3.click(fn=get_prediction(model_id_3), inputs=inp_1, outputs=out_3) | |
| app.launch() |