crypto-walk / app.py
pymmdrza's picture
Update app.py
0630c49 verified
import gradio as gr
from config import SessionManager, validate_all_keys
from services.market import get_market_data, get_current_price
from ai.service import AIService
session = SessionManager()
ai = AIService()
def set_keys(
openai_key, kucoin_key, kucoin_secret, kucoin_passphrase, provider, hf_token
):
session.set_keys(
openai_key, kucoin_key, kucoin_secret, kucoin_passphrase, provider, hf_token
)
ok, msg = validate_all_keys(session)
return msg
def clear_keys():
session.clear()
return "βœ… Session cleared"
def generate(symbol, timeframe, leverage, amount):
if not session.is_ready():
return {"error": "πŸ” Enter API keys first."}
market_data = get_market_data(symbol, timeframe, session)
current_price = get_current_price(symbol, session)
result = ai.generate(
symbol=symbol,
leverage=leverage,
trade_amount=amount,
current_price=current_price,
market_data=market_data,
provider=session.provider,
openai_key=session.openai_key,
hf_token=session.hf_token,
)
return (
result.to_prompt_dict()
if result
else {"error": "Failed to generate suggestion"}
)
with gr.Blocks(theme=gr.themes.Monochrome()) as ui:
gr.Markdown("# πŸ’Ή AI Crypto Trader")
with gr.Accordion("πŸ” API Keys", open=True):
openai_key = gr.Textbox(label="OpenAI Key", type="password")
kucoin_key = gr.Textbox(label="KuCoin Key", type="password")
kucoin_secret = gr.Textbox(label="KuCoin Secret", type="password")
kucoin_pass = gr.Textbox(label="KuCoin Passphrase", type="password")
hf_token = gr.Textbox(label="HF Token (if using HuggingFace)", type="password")
provider = gr.Radio(
["OpenAI", "HuggingFace"], value="OpenAI", label="AI Provider"
)
connect = gr.Button("Connect")
status = gr.Textbox(label="Connection Status")
connect.click(
set_keys,
inputs=[
openai_key,
kucoin_key,
kucoin_secret,
kucoin_pass,
provider,
hf_token,
],
outputs=status,
)
gr.Button("❌ Clear Session").click(clear_keys, outputs=status)
gr.Markdown("---")
with gr.Row():
symbol = gr.Textbox(label="Symbol", value="SOLUSDTM")
timeframe = gr.Dropdown(
["15m", "1h", "4h", "1d"], label="Timeframe", value="15m"
)
leverage = gr.Slider(10, 75, step=1, value=20, label="Leverage")
amount = gr.Number(label="Trade Amount ($)", value=100)
output = gr.JSON(label="AI Suggestion")
gr.Button("πŸš€ Generate Suggestion").click(
generate, inputs=[symbol, timeframe, leverage, amount], outputs=output
)
ui.launch()