Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained("Qilex/colorpAI-monocolor")
|
| 7 |
+
|
| 8 |
+
def round_to_2(num):
|
| 9 |
+
return round(num, 2)
|
| 10 |
+
|
| 11 |
+
def format_output(out_list):
|
| 12 |
+
white = 0
|
| 13 |
+
for dictionary in out_list:
|
| 14 |
+
if dictionary["label"] =='W':
|
| 15 |
+
white = round_to_2(dictionary["score"])
|
| 16 |
+
for dictionary in out_list:
|
| 17 |
+
if dictionary["label"] =='U':
|
| 18 |
+
blue = round_to_2(dictionary["score"])
|
| 19 |
+
for dictionary in out_list:
|
| 20 |
+
if dictionary["label"] =='B':
|
| 21 |
+
black = round_to_2(dictionary["score"])
|
| 22 |
+
for dictionary in out_list:
|
| 23 |
+
if dictionary["label"] =='R':
|
| 24 |
+
red = round_to_2(dictionary["score"])
|
| 25 |
+
for dictionary in out_list:
|
| 26 |
+
if dictionary["label"] =='G':
|
| 27 |
+
green = round_to_2(dictionary["score"])
|
| 28 |
+
for dictionary in out_list:
|
| 29 |
+
if dictionary["label"] =='C':
|
| 30 |
+
colorless = round_to_2(dictionary["score"])
|
| 31 |
+
out= {}
|
| 32 |
+
out['White'] = white
|
| 33 |
+
out['Blue'] = blue
|
| 34 |
+
out['Black'] = black
|
| 35 |
+
out['Red'] = red
|
| 36 |
+
out['Green'] = green
|
| 37 |
+
out['Colorless'] = colorless
|
| 38 |
+
return out
|
| 39 |
+
|
| 40 |
+
def predict(card):
|
| 41 |
+
return predictor_lg(card)
|
| 42 |
+
|
| 43 |
+
def remove_colored_pips(text):
|
| 44 |
+
pattern = r'\{[W,U,B,R,G,C]+/*[W,U,B,R,G,C]*\}'
|
| 45 |
+
return(re.sub(pattern, '{?}', text))
|
| 46 |
+
|
| 47 |
+
def preprocess_text(text):
|
| 48 |
+
return remove_colored_pips(text)
|
| 49 |
+
|
| 50 |
+
def categorize(card):
|
| 51 |
+
text = preprocess_text(card)
|
| 52 |
+
prediction = predict(text)
|
| 53 |
+
|
| 54 |
+
return format_output(prediction)
|
| 55 |
+
|
| 56 |
+
title = "Color pAI Version 1.0"
|
| 57 |
+
description = """
|
| 58 |
+
Color pAI is trained on around 18,000 Magic: the Gathering cards made available under Wizards of the Coast's
|
| 59 |
+
<a href="https://company.wizards.com/en/legal/fancontentpolicy" target = 'blank'>fan content policy</a>.
|
| 60 |
+
<br>
|
| 61 |
+
Input a card text using Scryfall syntax, and the model will tell evaluate which color it is most likely to be.
|
| 62 |
+
<br>Replace any card names with the word CARDNAME.
|
| 63 |
+
<br>
|
| 64 |
+
<br>This only works on monocolored cards. Version 2 will also handle multicolored cards.
|
| 65 |
+
<br>
|
| 66 |
+
"""
|
| 67 |
+
article = '''
|
| 68 |
+
<br>
|
| 69 |
+
Magic: the Gathering is property of Wizards of the Coast.
|
| 70 |
+
'''
|
| 71 |
+
predictor_lg = TextClassificationPipeline(model=model, tokenizer=tokenizer, function_to_apply = 'softmax', top_k = 6)
|
| 72 |
+
|
| 73 |
+
gr.Interface(
|
| 74 |
+
fn=categorize,
|
| 75 |
+
inputs=gr.Textbox(lines=1, placeholder="Type card text here."),
|
| 76 |
+
outputs=gr.Label(num_top_classes=6),
|
| 77 |
+
title=title,
|
| 78 |
+
description=description,
|
| 79 |
+
article = article,
|
| 80 |
+
).launch()
|