Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from peft import AutoPeftModelForCausalLM
|
| 3 |
-
from transformers import AutoTokenizer
|
| 4 |
from huggingface_hub import login, snapshot_download
|
| 5 |
import torch
|
| 6 |
import os
|
|
@@ -9,25 +9,34 @@ import json
|
|
| 9 |
# Login using secret (secure, no hardcode)
|
| 10 |
login(os.environ["HF_TOKEN"])
|
| 11 |
|
| 12 |
-
# Model setup (loads once on Space startup
|
| 13 |
-
model_id = "agarkovv/CryptoTrader-LM"
|
| 14 |
-
base_model_id = "
|
| 15 |
MAX_LENGTH = 32768
|
| 16 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available (ZeroGPU on HF)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
config_path = os.path.join(
|
| 21 |
with open(config_path, 'r') as f:
|
| 22 |
-
|
| 23 |
-
if 'model_type' in
|
| 24 |
-
del
|
| 25 |
with open(config_path, 'w') as f:
|
| 26 |
-
json.dump(
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
token = os.environ["HF_TOKEN"]
|
| 30 |
-
model = AutoPeftModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, token=token)
|
| 32 |
model = model.to(DEVICE)
|
| 33 |
model.eval()
|
|
@@ -62,8 +71,8 @@ demo = gr.Interface(
|
|
| 62 |
fn=predict_trading_decision,
|
| 63 |
inputs=gr.Textbox(label="Input Prompt (News + Prices)"),
|
| 64 |
outputs=gr.Textbox(label="Trading Decision"),
|
| 65 |
-
title="CryptoTrader-LM
|
| 66 |
-
description="Predict buy/sell/hold for BTC/ETH
|
| 67 |
)
|
| 68 |
|
| 69 |
# Launch with MCP support
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from peft import AutoPeftModelForCausalLM, PeftConfig
|
| 3 |
+
from transformers import AutoTokenizer, AutoConfig
|
| 4 |
from huggingface_hub import login, snapshot_download
|
| 5 |
import torch
|
| 6 |
import os
|
|
|
|
| 9 |
# Login using secret (secure, no hardcode)
|
| 10 |
login(os.environ["HF_TOKEN"])
|
| 11 |
|
| 12 |
+
# Model setup (loads once on Space startup)
|
| 13 |
+
model_id = "agarkovv/CryptoTrader-LM"
|
| 14 |
+
base_model_id = "mistralai/Ministral-8B-Instruct-2410"
|
| 15 |
MAX_LENGTH = 32768
|
| 16 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available (ZeroGPU on HF)
|
| 17 |
|
| 18 |
+
# Download adapter files
|
| 19 |
+
adapter_local_dir = snapshot_download(repo_id=model_id)
|
| 20 |
+
config_path = os.path.join(adapter_local_dir, "adapter_config.json")
|
| 21 |
with open(config_path, 'r') as f:
|
| 22 |
+
adapter_config = json.load(f)
|
| 23 |
+
if 'model_type' in adapter_config:
|
| 24 |
+
del adapter_config['model_type']
|
| 25 |
with open(config_path, 'w') as f:
|
| 26 |
+
json.dump(adapter_config, f)
|
| 27 |
|
| 28 |
+
# Download base model config locally to avoid gated access issues
|
| 29 |
+
base_local_dir = snapshot_download(repo_id=base_model_id, allow_patterns="config.json")
|
| 30 |
+
base_config_path = os.path.join(base_local_dir, "config.json")
|
| 31 |
+
base_config = AutoConfig.from_pretrained(base_config_path)
|
| 32 |
+
|
| 33 |
+
# Load model with explicit base config
|
| 34 |
token = os.environ["HF_TOKEN"]
|
| 35 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
| 36 |
+
adapter_local_dir,
|
| 37 |
+
config=base_config,
|
| 38 |
+
token=token
|
| 39 |
+
)
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained(base_model_id, token=token)
|
| 41 |
model = model.to(DEVICE)
|
| 42 |
model.eval()
|
|
|
|
| 71 |
fn=predict_trading_decision,
|
| 72 |
inputs=gr.Textbox(label="Input Prompt (News + Prices)"),
|
| 73 |
outputs=gr.Textbox(label="Trading Decision"),
|
| 74 |
+
title="CryptoTrader-LM MCP Tool",
|
| 75 |
+
description="Predict buy/sell/hold for BTC/ETH."
|
| 76 |
)
|
| 77 |
|
| 78 |
# Launch with MCP support
|