Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# HuggingFace Spaces file to run a Gradio Interface for the ALBERT v2 Steam Review Constructiveness Classifier by Samuel Ruairí Bullard
|
2 |
+
|
3 |
+
# Package Imports
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import pipeline
|
6 |
+
import torch
|
7 |
+
|
8 |
+
# Checks if CUDA is available on the machine
|
9 |
+
print("CUDA Available: ", torch.cuda.is_available())
|
10 |
+
|
11 |
+
# if not os.path.isfile("./README.md"):
|
12 |
+
# !git clone https://huggingface.co/spaces/abullard1/albert-v2-steam-review-constructiveness-classifier
|
13 |
+
|
14 |
+
# 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)
|
15 |
+
device = 0 if torch.cuda.is_available() else -1
|
16 |
+
torch_d_type = torch.float16 if torch.cuda.is_available() else torch.float32
|
17 |
+
print(f"Device: {device}")
|
18 |
+
|
19 |
+
# Defines the name of the base model, the classifier was fine-tuned from
|
20 |
+
base_model_name = "albert-base-v2"
|
21 |
+
|
22 |
+
# Defines the name of the fine-tuned model used for the steam-review constructiveness classification
|
23 |
+
finetuned_model_name = "abullard1/albert-v2-steam-review-constructiveness-classifier"
|
24 |
+
|
25 |
+
# PyTorch classifier pipeline
|
26 |
+
classifier = pipeline(
|
27 |
+
task="text-classification", # Defines the task
|
28 |
+
model=finetuned_model_name, # Defines the fine-tuned model to use
|
29 |
+
tokenizer=base_model_name, # Defines the tokenizer to use (same as the base model)
|
30 |
+
device=device, # Defines the device the classification will be run on
|
31 |
+
top_k=None, # Returns all scores for all labels, not just the one with the highest score
|
32 |
+
truncation=True, # Truncates the input text if it exceeds the maximum length
|
33 |
+
max_length=512, # Defines the maximum length of the input text (512 for BERT. Explicitly set here)
|
34 |
+
torch_dtype=torch_d_type
|
35 |
+
# 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
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
# Extracts the labels and scores from the prediction result
|
40 |
+
def classify_steam_review(input_text):
|
41 |
+
result = classifier(input_text)
|
42 |
+
|
43 |
+
label_1, label_2 = result[0][0]["label"], result[0][1]["label"]
|
44 |
+
score_1, score_2 = result[0][0]["score"], result[0][1]["score"]
|
45 |
+
|
46 |
+
return {"label_1": label_1, "score_1": score_1, "label_2": label_2, "score_2": score_2}
|
47 |
+
|
48 |
+
|
49 |
+
# Provides a textual representation of the classification result
|
50 |
+
def get_steam_review_classification_result_text(label_1, score_1, label_2, score_2):
|
51 |
+
# Maps label values to constructiveness
|
52 |
+
def label_to_constructiveness(label):
|
53 |
+
return "Constructive" if label == "LABEL_1" else "Not Constructive"
|
54 |
+
|
55 |
+
# Formats the output in a readable format
|
56 |
+
def format_output(label, score, emoji):
|
57 |
+
return f'{label_to_constructiveness(label)} with a score of {score}. {emoji}'
|
58 |
+
|
59 |
+
# Determines the label and score with the highest score
|
60 |
+
if score_1 >= score_2:
|
61 |
+
return format_output(label_1, score_1, "👍🏻")
|
62 |
+
else:
|
63 |
+
return format_output(label_2, score_2, "👎🏻")
|
64 |
+
|
65 |
+
|
66 |
+
# Examples Steam Reviews to display in the Gradio Interface using the "examples" parameter
|
67 |
+
examples = [
|
68 |
+
[
|
69 |
+
"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"],
|
70 |
+
["Review: Trash game. Deleted., Playtime: 1, Voted Up: False, Upvotes: 0, Votes Funny: 0"],
|
71 |
+
["Review: This game is amazing., Playtime: 100, Voted Up: True, Upvotes: 1, Votes Funny: 0"],
|
72 |
+
["Review: Great game, but the community is toxic., Playtime: 50, Voted Up: True, Upvotes: 1, Votes Funny: 0"]
|
73 |
+
]
|
74 |
+
|
75 |
+
# HTML article to display in the Gradio Interface using the "article" parameter
|
76 |
+
article = (
|
77 |
+
"""
|
78 |
+
*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}.*
|
79 |
+
"""
|
80 |
+
)
|
81 |
+
|
82 |
+
# Main Gradio Interface using Gradio Blocks
|
83 |
+
# Docs: https://www.gradio.app/docs/gradio/blocks
|
84 |
+
with gr.Blocks() as steam_reviews_classifier_block:
|
85 |
+
gr.Markdown("## Steam Review Constructiveness Classifier")
|
86 |
+
gr.Markdown(article)
|
87 |
+
|
88 |
+
# Main Column
|
89 |
+
with gr.Column():
|
90 |
+
# Upper Row (Input Textbox, Constructive Label, Not Constructive Label)
|
91 |
+
with gr.Row(equal_height=True):
|
92 |
+
# Input Textbox Column
|
93 |
+
with gr.Column():
|
94 |
+
input_textbox = gr.Textbox(
|
95 |
+
lines=8,
|
96 |
+
label="Steam Review",
|
97 |
+
# info="Input Steam Review here",
|
98 |
+
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",
|
99 |
+
show_copy_button=False,
|
100 |
+
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"
|
101 |
+
)
|
102 |
+
|
103 |
+
# Constructive and Not Constructive Labels Column
|
104 |
+
with gr.Column():
|
105 |
+
constructive_label = gr.Label(label="Constructive")
|
106 |
+
not_constructive_label = gr.Label(label="Not Constructive")
|
107 |
+
|
108 |
+
# Examples Component
|
109 |
+
example_component = gr.Examples(
|
110 |
+
examples=examples,
|
111 |
+
inputs=input_textbox
|
112 |
+
)
|
113 |
+
|
114 |
+
# Output Textbox which shows the textual representation of the Constructiveness Prediction
|
115 |
+
output_textbox = gr.Textbox(
|
116 |
+
label="Constructiveness Prediction",
|
117 |
+
interactive=False,
|
118 |
+
show_copy_button=False,
|
119 |
+
# info="Textual representation of the Constructiveness Prediction"
|
120 |
+
)
|
121 |
+
|
122 |
+
# Submit Button
|
123 |
+
submit_button = gr.Button(value="Submit")
|
124 |
+
|
125 |
+
# Function to run when the Submit Button is clicked (Passes the input text to the classifier and displays the output text)
|
126 |
+
def on_submit_click(input_text):
|
127 |
+
classification_result = classify_steam_review(input_text)
|
128 |
+
classification_result_text = get_steam_review_classification_result_text(
|
129 |
+
label_1=classification_result["label_1"],
|
130 |
+
score_1=classification_result["score_1"],
|
131 |
+
label_2=classification_result["label_2"],
|
132 |
+
score_2=classification_result["score_2"]
|
133 |
+
)
|
134 |
+
output_text = classification_result_text
|
135 |
+
constructive, not_constructive = str(classification_result["score_1"]), str(classification_result["score_2"])
|
136 |
+
return output_text, constructive, not_constructive
|
137 |
+
|
138 |
+
|
139 |
+
# onClick event for the Submit Button
|
140 |
+
submit_button.click(
|
141 |
+
fn=on_submit_click,
|
142 |
+
inputs=input_textbox,
|
143 |
+
outputs=[output_textbox, not_constructive_label, constructive_label]
|
144 |
+
)
|
145 |
+
|
146 |
+
# Launches the Gradio Blocks Interface
|
147 |
+
steam_reviews_classifier_block.launch()
|