Spaces:
Sleeping
Sleeping
| """ | |
| Jade TrainerBox - API Gradio com ZeroGPU | |
| Space isolado para treino de modelos ML. | |
| """ | |
| import gradio as gr | |
| # Tentar importar spaces (só funciona no HF Spaces) | |
| try: | |
| import spaces | |
| HAS_SPACES = True | |
| except ImportError: | |
| HAS_SPACES = False | |
| # Mock decorator para rodar localmente | |
| class spaces: | |
| def GPU(duration=120): | |
| def decorator(func): | |
| return func | |
| return decorator | |
| from trainer import train_model, run_eda | |
| # Wrapper com ZeroGPU | |
| def train_with_gpu(csv_data: str, target_col: str, model_type: str) -> dict: | |
| """ | |
| Treina modelo usando ZeroGPU (quando disponível). | |
| """ | |
| return train_model(csv_data, target_col, model_type) | |
| def api_train(csv_data: str, target_col: str, model_type: str = "xgboost") -> dict: | |
| """ | |
| Endpoint principal de treino. | |
| Args: | |
| csv_data: CSV como string | |
| target_col: Nome da coluna target | |
| model_type: "xgboost", "lightgbm", ou "mlp" | |
| """ | |
| if not csv_data or not csv_data.strip(): | |
| return {"success": False, "error": "CSV vazio"} | |
| if not target_col or not target_col.strip(): | |
| return {"success": False, "error": "Coluna target não especificada"} | |
| # Usar GPU se disponível (MLP se beneficia mais) | |
| if HAS_SPACES and model_type == "mlp": | |
| return train_with_gpu(csv_data, target_col, model_type) | |
| else: | |
| return train_model(csv_data, target_col, model_type) | |
| def api_eda(csv_data: str) -> dict: | |
| """ | |
| Endpoint de análise exploratória. | |
| """ | |
| if not csv_data or not csv_data.strip(): | |
| return {"success": False, "error": "CSV vazio"} | |
| return run_eda(csv_data) | |
| # Interface Gradio | |
| with gr.Blocks(title="Jade TrainerBox 🧠") as demo: | |
| gr.Markdown("# 🧠 Jade TrainerBox") | |
| gr.Markdown("Space isolado para treino de modelos ML. Use a API programaticamente.") | |
| with gr.Tab("Treino"): | |
| with gr.Row(): | |
| csv_input = gr.Textbox( | |
| label="CSV Data", | |
| placeholder="col1,col2,target\n1,2,0\n3,4,1", | |
| lines=5 | |
| ) | |
| with gr.Row(): | |
| target_input = gr.Textbox(label="Coluna Target", placeholder="target") | |
| model_dropdown = gr.Dropdown( | |
| choices=["xgboost", "lightgbm", "mlp"], | |
| value="xgboost", | |
| label="Modelo" | |
| ) | |
| train_btn = gr.Button("Treinar", variant="primary") | |
| train_output = gr.JSON(label="Resultado") | |
| train_btn.click( | |
| fn=api_train, | |
| inputs=[csv_input, target_input, model_dropdown], | |
| outputs=train_output, | |
| api_name="train" | |
| ) | |
| with gr.Tab("EDA"): | |
| eda_csv = gr.Textbox( | |
| label="CSV Data", | |
| placeholder="Dados para análise exploratória", | |
| lines=5 | |
| ) | |
| eda_btn = gr.Button("Analisar", variant="secondary") | |
| eda_output = gr.JSON(label="Análise") | |
| eda_btn.click( | |
| fn=api_eda, | |
| inputs=eda_csv, | |
| outputs=eda_output, | |
| api_name="eda" | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown("### 📡 API Usage") | |
| gr.Markdown(""" | |
| ```python | |
| from gradio_client import Client | |
| client = Client("seu-usuario/jade-trainerbox") | |
| result = client.predict( | |
| csv_data="col1,col2,target\\n1,2,0\\n3,4,1", | |
| target_col="target", | |
| model_type="xgboost", | |
| api_name="/train" | |
| ) | |
| print(result) | |
| ``` | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() | |