File size: 2,204 Bytes
4a2ada5 793061a 4a2ada5 793061a 53b155c 793061a fca027b 793061a 4a2ada5 793061a fca027b 4a2ada5 793061a fca027b 793061a 4a2ada5 793061a fca027b 793061a 4a2ada5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
from transformers import pipeline
import os
from dotenv import load_dotenv
load_dotenv()
token = os.environ.get("HF_TOKEN")
raw_model_name = "distilbert-base-uncased"
raw_model = pipeline("sentiment-analysis", model=raw_model_name)
scademy_500_500_model_name = "scademy/DistilBert-500-500-0"
scademy_500_500_model = pipeline("sentiment-analysis", model=scademy_500_500_model_name, token=token)
scademy_2500_2500_model_name = "scademy/bert-finetuned-2500-2500-0"
scademy_2500_2500_model = pipeline("sentiment-analysis", model=scademy_2500_2500_model_name, token=token)
scademy_12500_12500_model_name = "scademy/bert-finetuned-12500-12500-0"
scademy_12500_12500_model = pipeline("sentiment-analysis", model=scademy_12500_12500_model_name, token=token)
fine_tuned_model_name = "distilbert-base-uncased-finetuned-sst-2-english"
fine_tuned_model = pipeline("sentiment-analysis", model=fine_tuned_model_name)
def get_model_output(input_text, model_choice):
raw_result = raw_model(input_text)
scademy_500_500_result = scademy_500_500_model(input_text)
scademy_2500_2500_result = scademy_2500_2500_model(input_text)
scademy_12500_12500_result = scademy_12500_12500_model(input_text)
fine_tuned_result = fine_tuned_model(input_text)
return (
format_model_output(raw_result[0]),
format_model_output(scademy_500_500_result[0]),
format_model_output(scademy_2500_2500_result[0]),
format_model_output(scademy_12500_12500_result[0]),
format_model_output(fine_tuned_result[0])
)
def format_model_output(output):
return f"I am {output['score']*100:.2f}% sure that the sentiment is {output['label']}"
iface = gr.Interface(
fn=get_model_output,
title="DistilBERT Sentiment Analysis",
inputs=[
gr.Textbox(label="Input Text"),
],
outputs=[
gr.Textbox(label="Base DistilBERT output (distilbert-base-uncased)"),
gr.Textbox(label="Scademy DistilBERT 500-500 output"),
gr.Textbox(label="Scademy DistilBERT 2500-2500 output"),
gr.Textbox(label="Scademy DistilBERT 12500-12500 output"),
gr.Textbox(label="Fine-tuned DistilBERT output"),
],
)
iface.launch()
|