ddededstger commited on
Commit
c92f73a
Β·
verified Β·
1 Parent(s): ace15b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -16
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; switched to Llama 3 base)
13
- model_id = "agarkovv/CryptoTrader-LM" # Keep PEFT adapter if compatible; otherwise fine-tune on Llama
14
- base_model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
15
  MAX_LENGTH = 32768
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available (ZeroGPU on HF)
17
 
18
- # Workaround: Download model files, edit adapter_config.json to remove 'model_type' if present
19
- local_dir = snapshot_download(repo_id=model_id)
20
- config_path = os.path.join(local_dir, "adapter_config.json")
21
  with open(config_path, 'r') as f:
22
- config = json.load(f)
23
- if 'model_type' in config:
24
- del config['model_type']
25
  with open(config_path, 'w') as f:
26
- json.dump(config, f)
27
 
28
- # Now load the model from modified local dir, passing token for gated base model
 
 
 
 
 
29
  token = os.environ["HF_TOKEN"]
30
- model = AutoPeftModelForCausalLM.from_pretrained(local_dir, token=token)
 
 
 
 
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 with Llama MCP Tool",
66
- description="Predict buy/sell/hold for BTC/ETH using Llama 3 base."
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