Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from peft import AutoPeftModelForCausalLM
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Login to HF (use your READ token)
|
| 8 |
+
login("YOUR_HF_READ_TOKEN_HERE") # Replace with your token
|
| 9 |
+
|
| 10 |
+
# Model setup (loads once on Space startup)
|
| 11 |
+
model_id = "agarkovv/CryptoTrader-LM"
|
| 12 |
+
base_model_id = "mistralai/Ministral-8B-Instruct-2410"
|
| 13 |
+
MAX_LENGTH = 32768
|
| 14 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # Use GPU if available (ZeroGPU on HF)
|
| 15 |
+
|
| 16 |
+
model = AutoPeftModelForCausalLM.from_pretrained(model_id)
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
| 18 |
+
model = model.to(DEVICE)
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
def predict_trading_decision(prompt: str) -> str:
|
| 22 |
+
"""Predict daily trading decision (buy, sell, or hold) for BTC or ETH based on news and historical prices.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
prompt: Input prompt containing cryptocurrency news and historical price data (format: [INST]YOUR PROMPT HERE[/INST]).
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
Generated trading decision as text (e.g., 'Buy BTC at $62k').
|
| 29 |
+
"""
|
| 30 |
+
# Format prompt as required
|
| 31 |
+
formatted_prompt = f"[INST]{prompt}[/INST]"
|
| 32 |
+
|
| 33 |
+
inputs = tokenizer(
|
| 34 |
+
formatted_prompt, return_tensors="pt", padding=False, max_length=MAX_LENGTH, truncation=True
|
| 35 |
+
)
|
| 36 |
+
inputs = {key: value.to(model.device) for key, value in inputs.items()}
|
| 37 |
+
|
| 38 |
+
res = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
use_cache=True,
|
| 41 |
+
max_new_tokens=MAX_LENGTH,
|
| 42 |
+
)
|
| 43 |
+
output = tokenizer.decode(res[0], skip_special_tokens=True)
|
| 44 |
+
return output
|
| 45 |
+
|
| 46 |
+
# Gradio Interface
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=predict_trading_decision,
|
| 49 |
+
inputs=gr.Textbox(label="Input Prompt (News + Prices)"),
|
| 50 |
+
outputs=gr.Textbox(label="Trading Decision"),
|
| 51 |
+
title="CryptoTrader-LM MCP Tool",
|
| 52 |
+
description="Predict buy/sell/hold for BTC/ETH."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch with MCP support
|
| 56 |
+
demo.launch(mcp_server=True)
|