# HuggingFace Spaces file to run a Gradio Interface for the ALBERT v2 Steam Review Constructiveness Classifier by Samuel Ruairí Bullard
# Package Imports
import gradio as gr
from transformers import pipeline
import torch
# Checks if CUDA is available on the machine
print("CUDA Available: ", torch.cuda.is_available())
# if not os.path.isfile("./README.md"):
# !git clone https://huggingface.co/spaces/abullard1/albert-v2-steam-review-constructiveness-classifier
# Sets the torch dtype to 16-bit half-precision floating-point format if CUDA is available, otherwise sets it to 32-bit single-precision floating-point format. (Available for GPUs with Tensor Cores like NVIDIA's Volta, Turing, Ampere Architectures have for example)
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}")
# Defines the name of the base model, the classifier was fine-tuned from
base_model_name = "albert-base-v2"
# Defines the name of the fine-tuned model used for the steam-review constructiveness classification
finetuned_model_name = "abullard1/albert-v2-steam-review-constructiveness-classifier"
# PyTorch classifier pipeline
classifier = pipeline(
task="text-classification", # Defines the task
model=finetuned_model_name, # Defines the fine-tuned model to use
tokenizer=base_model_name, # Defines the tokenizer to use (same as the base model)
device=device, # Defines the device the classification will be run on
top_k=None, # Returns all scores for all labels, not just the one with the highest score
truncation=True, # Truncates the input text if it exceeds the maximum length
max_length=512, # Defines the maximum length of the input text (512 for BERT. Explicitly set here)
torch_dtype=torch_d_type
# Sets the torch dtype to 16-bit half-precision floating-point format if CUDA is available, otherwise sets it to 32-bit single-precision floating-point format
)
# Extracts the labels and scores from the prediction result
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}
# Provides a textual representation of the classification result
def get_steam_review_classification_result_text(label_1, score_1):
if label_1 == "LABEL_1":
return f"Constructive with a score of {score_1}. 👍🏻"
else:
return f"Not Constructive with a score of {score_1}. 👎🏻"
# Examples Steam Reviews to display in the Gradio Interface using the "examples" parameter
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"]
]
# HTML article to display in the Gradio Interface using the "article" parameter
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}.*
"""
)
labeling_criteria = (
"""