|
|
|
|
|
|
|
import gradio as gr |
|
from transformers import pipeline |
|
import torch |
|
|
|
|
|
print("CUDA Available: ", torch.cuda.is_available()) |
|
|
|
|
|
|
|
|
|
|
|
device = 0 if torch.cuda.is_available() else -1 |
|
torch_d_type = torch.float16 if torch.cuda.is_available() else torch.float32 |
|
print(f"Device: {device}") |
|
|
|
|
|
base_model_name = "albert-base-v2" |
|
|
|
|
|
finetuned_model_name = "abullard1/albert-v2-steam-review-constructiveness-classifier" |
|
|
|
|
|
classifier = pipeline( |
|
task="text-classification", |
|
model=finetuned_model_name, |
|
tokenizer=base_model_name, |
|
device=device, |
|
top_k=None, |
|
truncation=True, |
|
max_length=512, |
|
torch_dtype=torch_d_type |
|
|
|
) |
|
|
|
|
|
|
|
def classify_steam_review(input_text): |
|
result = classifier(input_text) |
|
|
|
label_1, label_2 = result[0][0]["label"], result[0][1]["label"] |
|
score_1, score_2 = result[0][0]["score"], result[0][1]["score"] |
|
|
|
return {"label_1": label_1, "score_1": score_1, "label_2": label_2, "score_2": score_2} |
|
|
|
|
|
|
|
def get_steam_review_classification_result_text(label_1, score_1, label_2, score_2): |
|
|
|
def label_to_constructiveness(label): |
|
return "Constructive" if label == "LABEL_1" else "Not Constructive" |
|
|
|
|
|
def format_output(label, score, emoji): |
|
return f'{label_to_constructiveness(label)} with a score of {score}. {emoji}' |
|
|
|
|
|
if score_1 >= score_2: |
|
return format_output(label_1, score_1, "👍🏻") |
|
else: |
|
return format_output(label_2, score_2, "👎🏻") |
|
|
|
|
|
|
|
examples = [ |
|
[ |
|
"Review: I think this is a great game but it still has some room for improvement., Playtime: 12, Voted Up: True, Upvotes: 1, Votes Funny: 0"], |
|
["Review: Trash game. Deleted., Playtime: 1, Voted Up: False, Upvotes: 0, Votes Funny: 0"], |
|
["Review: This game is amazing., Playtime: 100, Voted Up: True, Upvotes: 1, Votes Funny: 0"], |
|
["Review: Great game, but the community is toxic., Playtime: 50, Voted Up: True, Upvotes: 1, Votes Funny: 0"] |
|
] |
|
|
|
|
|
article = ( |
|
""" |
|
*Format your input as follows for the best results: **Review**: {review_text}, **Playtime**: {author_playtime_at_review}, **Voted Up**: {voted_up}, **Upvotes**: {upvotes}, **Votes Funny**: {votes_funny}.* |
|
""" |
|
) |
|
|
|
|
|
|
|
with gr.Blocks() as steam_reviews_classifier_block: |
|
gr.Markdown("## Steam Review Constructiveness Classifier") |
|
gr.Markdown(article) |
|
|
|
|
|
with gr.Column(): |
|
|
|
with gr.Row(equal_height=True): |
|
|
|
with gr.Column(): |
|
input_textbox = gr.Textbox( |
|
lines=8, |
|
label="Steam Review", |
|
|
|
placeholder="Review: I think this is a great game but it still has some room for improvement., Playtime: 12, Voted Up: True, Upvotes: 1, Votes Funny: 0", |
|
show_copy_button=False, |
|
value="Review: I think this is a great game but it still has some room for improvement., Playtime: 12, Voted Up: True, Upvotes: 1, Votes Funny: 0" |
|
) |
|
|
|
|
|
with gr.Column(): |
|
constructive_label = gr.Label(label="Constructive") |
|
not_constructive_label = gr.Label(label="Not Constructive") |
|
|
|
|
|
example_component = gr.Examples( |
|
examples=examples, |
|
inputs=input_textbox |
|
) |
|
|
|
|
|
output_textbox = gr.Textbox( |
|
label="Constructiveness Prediction", |
|
interactive=False, |
|
show_copy_button=False, |
|
|
|
) |
|
|
|
|
|
submit_button = gr.Button(value="Submit") |
|
|
|
|
|
def on_submit_click(input_text): |
|
classification_result = classify_steam_review(input_text) |
|
classification_result_text = get_steam_review_classification_result_text( |
|
label_1=classification_result["label_1"], |
|
score_1=classification_result["score_1"], |
|
label_2=classification_result["label_2"], |
|
score_2=classification_result["score_2"] |
|
) |
|
output_text = classification_result_text |
|
constructive, not_constructive = str(classification_result["score_1"]), str(classification_result["score_2"]) |
|
return output_text, constructive, not_constructive |
|
|
|
|
|
|
|
submit_button.click( |
|
fn=on_submit_click, |
|
inputs=input_textbox, |
|
outputs=[output_textbox, not_constructive_label, constructive_label] |
|
) |
|
|
|
|
|
steam_reviews_classifier_block.launch() |