|
|
|
|
|
|
|
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 = round(result[0][0]["score"], 6), round(result[0][1]["score"], 6) |
|
|
|
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): |
|
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 = [ |
|
["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"] |
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
labeling_criteria = ( |
|
""" |
|
<ul> |
|
<li> |
|
<b><span style="color:green;">Constructive</span></b>: |
|
Reviews that provide <b>helpful feedback</b>, <b>suggestions for improvement</b>, |
|
<b>constructive criticism</b>, or <b>detailed insights</b> into the game. |
|
</li> |
|
<li> |
|
<b><span style="color:red;">Not Constructive</span></b>: |
|
Reviews that <b>do not offer useful feedback</b>, <b>lack substance</b>, |
|
are <b>vague</b>, <b>off-topic</b>, <b>irrelevant</b>, or <b>trolling</b>. |
|
</li> |
|
</ul> |
|
""" |
|
) |
|
|
|
|
|
custom_css = """ |
|
<style> |
|
.card { |
|
border: 1px solid rgba(255, 255, 255, 0.1); |
|
padding: 16px; |
|
border-radius: 12px; |
|
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); |
|
background-color: transparent; |
|
margin-bottom: 16px; |
|
} |
|
</style> |
|
""" |
|
|
|
|
|
|
|
with gr.Blocks() as steam_reviews_classifier_block: |
|
with gr.Column(elem_classes=["card"]): |
|
gr.Markdown("## Steam Review Constructiveness Classifier") |
|
|
|
|
|
gr.HTML(custom_css) |
|
|
|
with gr.Column(): |
|
|
|
with gr.Column(elem_classes=["card"]): |
|
|
|
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") |
|
|
|
|
|
output_textbox = gr.Textbox( |
|
label="Constructiveness Prediction", |
|
interactive=False, |
|
show_copy_button=False, |
|
|
|
) |
|
|
|
gr.Markdown("<br>") |
|
|
|
|
|
example_component = gr.Examples( |
|
examples=examples, |
|
inputs=input_textbox |
|
) |
|
|
|
|
|
submit_button = gr.Button(value="Submit") |
|
|
|
|
|
with gr.Accordion(label="See Labeling Criteria:", open=False): |
|
gr.Markdown(labeling_criteria) |
|
|
|
|
|
def on_submit_click(input_text): |
|
classification_result = classify_steam_review(input_text) |
|
label_1 = classification_result["label_1"] |
|
score_1 = classification_result["score_1"] |
|
score_2 = classification_result["score_2"] |
|
|
|
output_text = get_steam_review_classification_result_text(label_1, score_1) |
|
|
|
|
|
if label_1 == "LABEL_1": |
|
constructive, not_constructive = score_1, score_2 |
|
else: |
|
constructive, not_constructive = score_2, score_1 |
|
|
|
return output_text, constructive, not_constructive |
|
|
|
|
|
|
|
submit_button.click( |
|
fn=on_submit_click, |
|
inputs=input_textbox, |
|
outputs=[output_textbox, constructive_label, not_constructive_label] |
|
) |
|
|
|
|
|
steam_reviews_classifier_block.launch() |
|
|