import gradio as gr import joblib import numpy as np import pandas as pd from sentence_transformers import SentenceTransformer # Load saved model artifacts and the embedding model rf_model = joblib.load('model.pkl') preprocessor = joblib.load('tabular_preprocessor.pkl') embedding_model = SentenceTransformer('all-MiniLM-L6-v2') def predict_price(cmc, power, toughness, rarity, color_identity, oracle_text): # Construct input dataframe input_df = pd.DataFrame([{ 'cmc': float(cmc), 'power_num': float(power), 'toughness_num': float(toughness), 'rarity': rarity, 'color_identity': color_identity }]) # 1. Preprocess tabular inputs tabular_features = preprocessor.transform(input_df) # 2. Generate sentence embedding for text text_embedding = embedding_model.encode([oracle_text]) # 3. Stack features side-by-side X_input = np.hstack([tabular_features, text_embedding]) # 4. Predict log price and convert back to $ USD log_pred = rf_model.predict(X_input)[0] predicted_usd = np.expm1(log_pred) return f"${predicted_usd:.2f} USD" # Build Gradio Web Interface interface = gr.Interface( fn=predict_price, inputs=[ gr.Number(label="Mana Value (CMC)", value=3), gr.Number(label="Power", value=2), gr.Number(label="Toughness", value=2), gr.Dropdown(choices=['common', 'uncommon', 'rare', 'mythic'], label="Rarity", value='rare'), gr.Dropdown(choices=['W', 'U', 'B', 'R', 'G', 'C', 'WU', 'UB', 'BR', 'RG', 'GW'], label="Color Identity", value='U'), gr.Textbox(label="Oracle Text", value="When this creature enters, draw a card.") ], outputs=gr.Textbox(label="Predicted Secondary Market Price"), title="MTG Legendary Creature Price Predictor" ) if __name__ == "__main__": interface.launch()