Spaces:
Sleeping
Sleeping
Denis Lebedev commited on
Return mtga_card_id in predict output
Browse files- app.py +30 -12
- src/mtga.py +3 -0
- src/predictor.py +4 -3
app.py
CHANGED
|
@@ -26,14 +26,28 @@ def main():
|
|
| 26 |
def format_for_gallery(predictions):
|
| 27 |
# Assuming predictions is like [("Black Lotus", 0.4), ("Mox Ruby", 0.5)]
|
| 28 |
return [
|
| 29 |
-
(card_name_to_image_url(card_name), f"{card_name} ({probability*100:.0f}%)")
|
| 30 |
-
for card_name, probability in predictions
|
| 31 |
]
|
| 32 |
|
| 33 |
def predict_fn(set_code, input):
|
| 34 |
state = json.loads(input.replace("'", "\""))
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def update_example(set_choice):
|
| 38 |
return json.dumps(examples.get(set_choice, {}), indent=2)
|
| 39 |
|
|
@@ -89,13 +103,17 @@ def main():
|
|
| 89 |
outputs=[pack_gallery, pool_gallery]
|
| 90 |
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Update text input when dropdown changes
|
| 100 |
set_dropdown.change(
|
| 101 |
fn=update_example,
|
|
@@ -107,7 +125,7 @@ def main():
|
|
| 107 |
gr.Button("Predict").click(
|
| 108 |
fn=predict_fn,
|
| 109 |
inputs=[set_dropdown, text_input],
|
| 110 |
-
outputs=[preidctions_gallery]
|
| 111 |
)
|
| 112 |
|
| 113 |
demo.launch()
|
|
|
|
| 26 |
def format_for_gallery(predictions):
|
| 27 |
# Assuming predictions is like [("Black Lotus", 0.4), ("Mox Ruby", 0.5)]
|
| 28 |
return [
|
| 29 |
+
(card_name_to_image_url(card_name), f"{card_name} ({probability*100:.0f}%)", mtga_card_id)
|
| 30 |
+
for mtga_card_id, card_name, probability in predictions
|
| 31 |
]
|
| 32 |
|
| 33 |
def predict_fn(set_code, input):
|
| 34 |
state = json.loads(input.replace("'", "\""))
|
| 35 |
+
predictions = predictor.predict(set_code, state)
|
| 36 |
+
|
| 37 |
+
# For gallery
|
| 38 |
+
gallery_output = [
|
| 39 |
+
(card_name_to_image_url(card_name), f"{card_name} ({probability*100:.0f}%)")
|
| 40 |
+
for mtga_card_id, card_name, probability in predictions
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
# For probabilities list
|
| 44 |
+
prob_output = [
|
| 45 |
+
(mtga_card_id, float(probability))
|
| 46 |
+
for mtga_card_id, _, probability in predictions
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
return gallery_output, prob_output
|
| 50 |
+
|
| 51 |
def update_example(set_choice):
|
| 52 |
return json.dumps(examples.get(set_choice, {}), indent=2)
|
| 53 |
|
|
|
|
| 103 |
outputs=[pack_gallery, pool_gallery]
|
| 104 |
)
|
| 105 |
|
| 106 |
+
with gr.Row():
|
| 107 |
+
preidctions_gallery = gr.Gallery(
|
| 108 |
+
label="Predictions",
|
| 109 |
+
#height=300,
|
| 110 |
+
columns=3,
|
| 111 |
+
object_fit='scale-down',
|
| 112 |
+
allow_preview=False
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
predictions_probs = gr.JSON(label="Card IDs and Probabilities")
|
| 116 |
+
|
| 117 |
# Update text input when dropdown changes
|
| 118 |
set_dropdown.change(
|
| 119 |
fn=update_example,
|
|
|
|
| 125 |
gr.Button("Predict").click(
|
| 126 |
fn=predict_fn,
|
| 127 |
inputs=[set_dropdown, text_input],
|
| 128 |
+
outputs=[preidctions_gallery, predictions_probs]
|
| 129 |
)
|
| 130 |
|
| 131 |
demo.launch()
|
src/mtga.py
CHANGED
|
@@ -49,5 +49,8 @@ def mtga_id_to_card_name(id):
|
|
| 49 |
def card_name_to_image_url(name):
|
| 50 |
return cards_by_name[name]['image_uris']['normal']
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
def mtga_id_to_image_url(id):
|
| 53 |
return cards_by_mtga_id[id]['image_uris']['normal']
|
|
|
|
| 49 |
def card_name_to_image_url(name):
|
| 50 |
return cards_by_name[name]['image_uris']['normal']
|
| 51 |
|
| 52 |
+
def card_name_to_mtga_id(name):
|
| 53 |
+
return cards_by_name[name]['arena_id']
|
| 54 |
+
|
| 55 |
def mtga_id_to_image_url(id):
|
| 56 |
return cards_by_mtga_id[id]['image_uris']['normal']
|
src/predictor.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
from fastai.tabular.all import *
|
| 3 |
-
from .mtga import mtga_id_to_card_name
|
| 4 |
|
| 5 |
class MTGPickPredictor:
|
| 6 |
def __init__(self):
|
|
@@ -19,8 +19,9 @@ class MTGPickPredictor:
|
|
| 19 |
result = ""
|
| 20 |
output = []
|
| 21 |
for _, (prob, idx) in enumerate(zip(topk_values, topk_indices)):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
print(result)
|
| 26 |
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
from fastai.tabular.all import *
|
| 3 |
+
from .mtga import mtga_id_to_card_name, card_name_to_mtga_id
|
| 4 |
|
| 5 |
class MTGPickPredictor:
|
| 6 |
def __init__(self):
|
|
|
|
| 19 |
result = ""
|
| 20 |
output = []
|
| 21 |
for _, (prob, idx) in enumerate(zip(topk_values, topk_indices)):
|
| 22 |
+
card_name = self._card_names[set_name][idx]
|
| 23 |
+
result = result + (f"{card_name}: {prob*100:.0f}%\n")
|
| 24 |
+
output.append((card_name_to_mtga_id(card_name), card_name, prob))
|
| 25 |
|
| 26 |
print(result)
|
| 27 |
|