Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,9 @@ import gradio as gr
|
|
| 9 |
from datetime import datetime, timedelta, timezone
|
| 10 |
from groq import Groq
|
| 11 |
|
| 12 |
-
# =====================================
|
| 13 |
-
#
|
| 14 |
-
# =====================================
|
| 15 |
|
| 16 |
OKX_URL = "https://www.okx.com/api/v5/market/history-candles"
|
| 17 |
|
|
@@ -19,12 +19,12 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
| 19 |
|
| 20 |
GROQ_MODEL = os.getenv(
|
| 21 |
"GROQ_MODEL",
|
| 22 |
-
"
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# =====================================
|
| 26 |
-
# OKX
|
| 27 |
-
# =====================================
|
| 28 |
|
| 29 |
def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
| 30 |
|
|
@@ -68,7 +68,9 @@ def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
|
| 68 |
result = response.json()
|
| 69 |
|
| 70 |
if result.get("code") != "0":
|
| 71 |
-
raise RuntimeError(
|
|
|
|
|
|
|
| 72 |
|
| 73 |
data = result.get("data", [])
|
| 74 |
|
|
@@ -94,7 +96,7 @@ def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
|
| 94 |
max(1, end_ms - start_ms)
|
| 95 |
)
|
| 96 |
),
|
| 97 |
-
desc=f"OKX
|
| 98 |
)
|
| 99 |
|
| 100 |
if oldest <= start_ms:
|
|
@@ -119,7 +121,10 @@ def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
|
| 119 |
"confirm"
|
| 120 |
]
|
| 121 |
|
| 122 |
-
df = pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
df["timestamp"] = pd.to_datetime(
|
| 125 |
df["timestamp"].astype("int64"),
|
|
@@ -128,8 +133,13 @@ def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
|
| 128 |
)
|
| 129 |
|
| 130 |
numeric_columns = [
|
| 131 |
-
"open",
|
| 132 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
]
|
| 134 |
|
| 135 |
for col in numeric_columns:
|
|
@@ -146,32 +156,38 @@ def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
|
| 146 |
return df
|
| 147 |
|
| 148 |
|
| 149 |
-
# =====================================
|
| 150 |
# TECHNICAL INDICATORS
|
| 151 |
-
# =====================================
|
| 152 |
|
| 153 |
def add_indicators(df):
|
| 154 |
|
| 155 |
df = df.copy()
|
| 156 |
|
| 157 |
-
df["return_pct"] =
|
|
|
|
|
|
|
| 158 |
|
| 159 |
df["EMA20"] = df["close"].ewm(
|
| 160 |
-
span=20,
|
|
|
|
| 161 |
).mean()
|
| 162 |
|
| 163 |
df["EMA50"] = df["close"].ewm(
|
| 164 |
-
span=50,
|
|
|
|
| 165 |
).mean()
|
| 166 |
|
| 167 |
delta = df["close"].diff()
|
| 168 |
|
| 169 |
gain = delta.clip(lower=0).ewm(
|
| 170 |
-
alpha=1/14,
|
|
|
|
| 171 |
).mean()
|
| 172 |
|
| 173 |
loss = (-delta.clip(upper=0)).ewm(
|
| 174 |
-
alpha=1/14,
|
|
|
|
| 175 |
).mean()
|
| 176 |
|
| 177 |
rs = gain / loss.replace(0, float("nan"))
|
|
@@ -190,27 +206,32 @@ def add_indicators(df):
|
|
| 190 |
).max(axis=1)
|
| 191 |
|
| 192 |
df["ATR"] = tr.ewm(
|
| 193 |
-
alpha=1/14,
|
|
|
|
| 194 |
).mean()
|
| 195 |
|
| 196 |
-
df["volume_sma"] =
|
|
|
|
|
|
|
| 197 |
|
| 198 |
return df
|
| 199 |
|
| 200 |
|
| 201 |
-
# =====================================
|
| 202 |
-
# GROQ
|
| 203 |
-
# =====================================
|
| 204 |
|
| 205 |
def groq_analysis(df):
|
| 206 |
|
| 207 |
if not GROQ_API_KEY:
|
| 208 |
raise ValueError(
|
| 209 |
"GROQ_API_KEY bulunamadı. "
|
| 210 |
-
"Hugging Face
|
| 211 |
)
|
| 212 |
|
| 213 |
-
client = Groq(
|
|
|
|
|
|
|
| 214 |
|
| 215 |
latest = df.tail(30).copy()
|
| 216 |
|
|
@@ -231,45 +252,49 @@ def groq_analysis(df):
|
|
| 231 |
].round(4).to_string(index=False)
|
| 232 |
|
| 233 |
prompt = f"""
|
| 234 |
-
BTC-USDT piyasa
|
| 235 |
-
|
| 236 |
-
Aşağıdaki veriler OKX Public API üzerinden alınmıştır.
|
| 237 |
|
| 238 |
VERİLER:
|
| 239 |
{market_data}
|
| 240 |
|
| 241 |
-
ANALİZ:
|
| 242 |
|
| 243 |
-
1. Genel
|
| 244 |
-
2. EMA20 ve EMA50.
|
| 245 |
-
3. RSI momentum.
|
| 246 |
-
4. ATR
|
| 247 |
-
5. Hacim
|
| 248 |
-
6. Destek
|
| 249 |
-
7.
|
| 250 |
-
8.
|
| 251 |
-
9.
|
| 252 |
-
10.
|
| 253 |
-
11. Risk analizi.
|
| 254 |
|
| 255 |
-
ÇIKTI:
|
| 256 |
|
| 257 |
YÖN: BULLISH / BEARISH / NEUTRAL
|
|
|
|
| 258 |
MODEL GÜVENİ: 0-100
|
|
|
|
| 259 |
TREND:
|
| 260 |
MOMENTUM:
|
|
|
|
| 261 |
DESTEK:
|
| 262 |
DİRENÇ:
|
|
|
|
| 263 |
YÜKSELİŞ SENARYOSU:
|
| 264 |
DÜŞÜŞ SENARYOSU:
|
| 265 |
-
|
| 266 |
-
NİHAİ ANALİZ:
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
"""
|
| 274 |
|
| 275 |
response = client.chat.completions.create(
|
|
@@ -278,8 +303,9 @@ NİHAİ ANALİZ:
|
|
| 278 |
{
|
| 279 |
"role": "system",
|
| 280 |
"content": (
|
| 281 |
-
"
|
| 282 |
-
"
|
|
|
|
| 283 |
)
|
| 284 |
},
|
| 285 |
{
|
|
@@ -288,15 +314,15 @@ NİHAİ ANALİZ:
|
|
| 288 |
}
|
| 289 |
],
|
| 290 |
temperature=0.2,
|
| 291 |
-
max_completion_tokens=
|
| 292 |
)
|
| 293 |
|
| 294 |
return response.choices[0].message.content
|
| 295 |
|
| 296 |
|
| 297 |
-
# =====================================
|
| 298 |
-
# MAIN
|
| 299 |
-
# =====================================
|
| 300 |
|
| 301 |
def run_system(days, timeframe, symbol):
|
| 302 |
|
|
@@ -321,21 +347,25 @@ def run_system(days, timeframe, symbol):
|
|
| 321 |
csv_path = temp.name
|
| 322 |
temp.close()
|
| 323 |
|
| 324 |
-
df.to_csv(
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
last = df.iloc[-1]
|
| 327 |
|
| 328 |
info = f"""
|
| 329 |
-
## MARKET
|
| 330 |
|
| 331 |
- Symbol: {symbol}
|
| 332 |
- Timeframe: {timeframe}
|
| 333 |
-
-
|
| 334 |
-
- Last
|
| 335 |
- RSI: {last['RSI']:.2f}
|
| 336 |
- EMA20: {last['EMA20']:.2f}
|
| 337 |
- EMA50: {last['EMA50']:.2f}
|
| 338 |
- ATR: {last['ATR']:.2f}
|
|
|
|
| 339 |
"""
|
| 340 |
|
| 341 |
return (
|
|
@@ -355,20 +385,20 @@ def run_system(days, timeframe, symbol):
|
|
| 355 |
)
|
| 356 |
|
| 357 |
|
| 358 |
-
# =====================================
|
| 359 |
-
# GRADIO
|
| 360 |
-
# =====================================
|
| 361 |
|
| 362 |
with gr.Blocks(
|
| 363 |
-
title="OKX
|
| 364 |
) as demo:
|
| 365 |
|
| 366 |
gr.Markdown("""
|
| 367 |
# BTC-USDT AI ANALYSIS ENGINE
|
| 368 |
|
| 369 |
-
### OKX Public API + Groq
|
| 370 |
|
| 371 |
-
Historical Data | Technical
|
| 372 |
""")
|
| 373 |
|
| 374 |
with gr.Row():
|
|
@@ -380,9 +410,17 @@ with gr.Blocks(
|
|
| 380 |
|
| 381 |
timeframe = gr.Dropdown(
|
| 382 |
choices=[
|
| 383 |
-
"1m",
|
| 384 |
-
"
|
| 385 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
],
|
| 387 |
value="1H",
|
| 388 |
label="Timeframe"
|
|
@@ -397,7 +435,7 @@ with gr.Blocks(
|
|
| 397 |
)
|
| 398 |
|
| 399 |
run = gr.Button(
|
| 400 |
-
"RUN
|
| 401 |
variant="primary"
|
| 402 |
)
|
| 403 |
|
|
@@ -406,11 +444,11 @@ with gr.Blocks(
|
|
| 406 |
ai_output = gr.Markdown()
|
| 407 |
|
| 408 |
csv_output = gr.File(
|
| 409 |
-
label="Download
|
| 410 |
)
|
| 411 |
|
| 412 |
-
|
| 413 |
-
label="
|
| 414 |
interactive=False
|
| 415 |
)
|
| 416 |
|
|
@@ -425,7 +463,7 @@ with gr.Blocks(
|
|
| 425 |
market_info,
|
| 426 |
ai_output,
|
| 427 |
csv_output,
|
| 428 |
-
|
| 429 |
]
|
| 430 |
)
|
| 431 |
|
|
|
|
| 9 |
from datetime import datetime, timedelta, timezone
|
| 10 |
from groq import Groq
|
| 11 |
|
| 12 |
+
# ==========================================
|
| 13 |
+
# CONFIGURATION
|
| 14 |
+
# ==========================================
|
| 15 |
|
| 16 |
OKX_URL = "https://www.okx.com/api/v5/market/history-candles"
|
| 17 |
|
|
|
|
| 19 |
|
| 20 |
GROQ_MODEL = os.getenv(
|
| 21 |
"GROQ_MODEL",
|
| 22 |
+
"openai/gpt-oss-120b"
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ==========================================
|
| 26 |
+
# OKX PUBLIC DATA
|
| 27 |
+
# ==========================================
|
| 28 |
|
| 29 |
def get_okx_data(days, timeframe, symbol, progress=gr.Progress()):
|
| 30 |
|
|
|
|
| 68 |
result = response.json()
|
| 69 |
|
| 70 |
if result.get("code") != "0":
|
| 71 |
+
raise RuntimeError(
|
| 72 |
+
result.get("msg", "OKX API error")
|
| 73 |
+
)
|
| 74 |
|
| 75 |
data = result.get("data", [])
|
| 76 |
|
|
|
|
| 96 |
max(1, end_ms - start_ms)
|
| 97 |
)
|
| 98 |
),
|
| 99 |
+
desc=f"OKX candles: {len(all_data)}"
|
| 100 |
)
|
| 101 |
|
| 102 |
if oldest <= start_ms:
|
|
|
|
| 121 |
"confirm"
|
| 122 |
]
|
| 123 |
|
| 124 |
+
df = pd.DataFrame(
|
| 125 |
+
all_data,
|
| 126 |
+
columns=columns
|
| 127 |
+
)
|
| 128 |
|
| 129 |
df["timestamp"] = pd.to_datetime(
|
| 130 |
df["timestamp"].astype("int64"),
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
numeric_columns = [
|
| 136 |
+
"open",
|
| 137 |
+
"high",
|
| 138 |
+
"low",
|
| 139 |
+
"close",
|
| 140 |
+
"volume",
|
| 141 |
+
"volume_currency",
|
| 142 |
+
"volume_quote"
|
| 143 |
]
|
| 144 |
|
| 145 |
for col in numeric_columns:
|
|
|
|
| 156 |
return df
|
| 157 |
|
| 158 |
|
| 159 |
+
# ==========================================
|
| 160 |
# TECHNICAL INDICATORS
|
| 161 |
+
# ==========================================
|
| 162 |
|
| 163 |
def add_indicators(df):
|
| 164 |
|
| 165 |
df = df.copy()
|
| 166 |
|
| 167 |
+
df["return_pct"] = (
|
| 168 |
+
df["close"].pct_change() * 100
|
| 169 |
+
)
|
| 170 |
|
| 171 |
df["EMA20"] = df["close"].ewm(
|
| 172 |
+
span=20,
|
| 173 |
+
adjust=False
|
| 174 |
).mean()
|
| 175 |
|
| 176 |
df["EMA50"] = df["close"].ewm(
|
| 177 |
+
span=50,
|
| 178 |
+
adjust=False
|
| 179 |
).mean()
|
| 180 |
|
| 181 |
delta = df["close"].diff()
|
| 182 |
|
| 183 |
gain = delta.clip(lower=0).ewm(
|
| 184 |
+
alpha=1/14,
|
| 185 |
+
adjust=False
|
| 186 |
).mean()
|
| 187 |
|
| 188 |
loss = (-delta.clip(upper=0)).ewm(
|
| 189 |
+
alpha=1/14,
|
| 190 |
+
adjust=False
|
| 191 |
).mean()
|
| 192 |
|
| 193 |
rs = gain / loss.replace(0, float("nan"))
|
|
|
|
| 206 |
).max(axis=1)
|
| 207 |
|
| 208 |
df["ATR"] = tr.ewm(
|
| 209 |
+
alpha=1/14,
|
| 210 |
+
adjust=False
|
| 211 |
).mean()
|
| 212 |
|
| 213 |
+
df["volume_sma"] = (
|
| 214 |
+
df["volume"].rolling(20).mean()
|
| 215 |
+
)
|
| 216 |
|
| 217 |
return df
|
| 218 |
|
| 219 |
|
| 220 |
+
# ==========================================
|
| 221 |
+
# GROQ GPT-OSS AI
|
| 222 |
+
# ==========================================
|
| 223 |
|
| 224 |
def groq_analysis(df):
|
| 225 |
|
| 226 |
if not GROQ_API_KEY:
|
| 227 |
raise ValueError(
|
| 228 |
"GROQ_API_KEY bulunamadı. "
|
| 229 |
+
"Hugging Face Secrets bölümüne ekle."
|
| 230 |
)
|
| 231 |
|
| 232 |
+
client = Groq(
|
| 233 |
+
api_key=GROQ_API_KEY
|
| 234 |
+
)
|
| 235 |
|
| 236 |
latest = df.tail(30).copy()
|
| 237 |
|
|
|
|
| 252 |
].round(4).to_string(index=False)
|
| 253 |
|
| 254 |
prompt = f"""
|
| 255 |
+
BTC-USDT teknik piyasa analizi yap.
|
|
|
|
|
|
|
| 256 |
|
| 257 |
VERİLER:
|
| 258 |
{market_data}
|
| 259 |
|
| 260 |
+
ANALİZ GÖREVLERİ:
|
| 261 |
|
| 262 |
+
1. Genel piyasa trendi.
|
| 263 |
+
2. EMA20 ve EMA50 karşılaştırması.
|
| 264 |
+
3. RSI momentum analizi.
|
| 265 |
+
4. ATR volatilite analizi.
|
| 266 |
+
5. Hacim değerlendirmesi.
|
| 267 |
+
6. Destek ve direnç seviyeleri.
|
| 268 |
+
7. Bullish senaryosu.
|
| 269 |
+
8. Bearish senaryosu.
|
| 270 |
+
9. Neutral senaryosu.
|
| 271 |
+
10. Risk analizi.
|
|
|
|
| 272 |
|
| 273 |
+
ÇIKTI FORMATI:
|
| 274 |
|
| 275 |
YÖN: BULLISH / BEARISH / NEUTRAL
|
| 276 |
+
|
| 277 |
MODEL GÜVENİ: 0-100
|
| 278 |
+
|
| 279 |
TREND:
|
| 280 |
MOMENTUM:
|
| 281 |
+
VOLATİLİTE:
|
| 282 |
DESTEK:
|
| 283 |
DİRENÇ:
|
| 284 |
+
|
| 285 |
YÜKSELİŞ SENARYOSU:
|
| 286 |
DÜŞÜŞ SENARYOSU:
|
| 287 |
+
YATAY SENARYO:
|
|
|
|
| 288 |
|
| 289 |
+
RİSK:
|
| 290 |
+
NİHAİ KARAR:
|
| 291 |
+
|
| 292 |
+
Kurallar:
|
| 293 |
+
- Sadece verilen verilere dayan.
|
| 294 |
+
- Gelecekteki fiyatları uydurma.
|
| 295 |
+
- Güven puanını kalibre edilmiş olasılık gibi sunma.
|
| 296 |
+
- Kesin kazanç garantisi verme.
|
| 297 |
+
- Türkçe yanıt ver.
|
| 298 |
"""
|
| 299 |
|
| 300 |
response = client.chat.completions.create(
|
|
|
|
| 303 |
{
|
| 304 |
"role": "system",
|
| 305 |
"content": (
|
| 306 |
+
"You are a professional quantitative "
|
| 307 |
+
"crypto market analyst. "
|
| 308 |
+
"Analyze data carefully and respond in Turkish."
|
| 309 |
)
|
| 310 |
},
|
| 311 |
{
|
|
|
|
| 314 |
}
|
| 315 |
],
|
| 316 |
temperature=0.2,
|
| 317 |
+
max_completion_tokens=2000
|
| 318 |
)
|
| 319 |
|
| 320 |
return response.choices[0].message.content
|
| 321 |
|
| 322 |
|
| 323 |
+
# ==========================================
|
| 324 |
+
# MAIN ENGINE
|
| 325 |
+
# ==========================================
|
| 326 |
|
| 327 |
def run_system(days, timeframe, symbol):
|
| 328 |
|
|
|
|
| 347 |
csv_path = temp.name
|
| 348 |
temp.close()
|
| 349 |
|
| 350 |
+
df.to_csv(
|
| 351 |
+
csv_path,
|
| 352 |
+
index=False
|
| 353 |
+
)
|
| 354 |
|
| 355 |
last = df.iloc[-1]
|
| 356 |
|
| 357 |
info = f"""
|
| 358 |
+
## MARKET INFORMATION
|
| 359 |
|
| 360 |
- Symbol: {symbol}
|
| 361 |
- Timeframe: {timeframe}
|
| 362 |
+
- Candles: {len(df)}
|
| 363 |
+
- Last Close: {last['close']:.2f}
|
| 364 |
- RSI: {last['RSI']:.2f}
|
| 365 |
- EMA20: {last['EMA20']:.2f}
|
| 366 |
- EMA50: {last['EMA50']:.2f}
|
| 367 |
- ATR: {last['ATR']:.2f}
|
| 368 |
+
- Volume: {last['volume']:.4f}
|
| 369 |
"""
|
| 370 |
|
| 371 |
return (
|
|
|
|
| 385 |
)
|
| 386 |
|
| 387 |
|
| 388 |
+
# ==========================================
|
| 389 |
+
# GRADIO INTERFACE
|
| 390 |
+
# ==========================================
|
| 391 |
|
| 392 |
with gr.Blocks(
|
| 393 |
+
title="OKX GPT-OSS BTC AI"
|
| 394 |
) as demo:
|
| 395 |
|
| 396 |
gr.Markdown("""
|
| 397 |
# BTC-USDT AI ANALYSIS ENGINE
|
| 398 |
|
| 399 |
+
### OKX Public API + Groq GPT-OSS 120B
|
| 400 |
|
| 401 |
+
Historical Data | Technical Indicators | AI Analysis | CSV
|
| 402 |
""")
|
| 403 |
|
| 404 |
with gr.Row():
|
|
|
|
| 410 |
|
| 411 |
timeframe = gr.Dropdown(
|
| 412 |
choices=[
|
| 413 |
+
"1m",
|
| 414 |
+
"3m",
|
| 415 |
+
"5m",
|
| 416 |
+
"15m",
|
| 417 |
+
"30m",
|
| 418 |
+
"1H",
|
| 419 |
+
"2H",
|
| 420 |
+
"4H",
|
| 421 |
+
"6H",
|
| 422 |
+
"12H",
|
| 423 |
+
"1D"
|
| 424 |
],
|
| 425 |
value="1H",
|
| 426 |
label="Timeframe"
|
|
|
|
| 435 |
)
|
| 436 |
|
| 437 |
run = gr.Button(
|
| 438 |
+
"RUN GPT-OSS BTC AI",
|
| 439 |
variant="primary"
|
| 440 |
)
|
| 441 |
|
|
|
|
| 444 |
ai_output = gr.Markdown()
|
| 445 |
|
| 446 |
csv_output = gr.File(
|
| 447 |
+
label="Download CSV"
|
| 448 |
)
|
| 449 |
|
| 450 |
+
preview = gr.Dataframe(
|
| 451 |
+
label="Last 20 Candles",
|
| 452 |
interactive=False
|
| 453 |
)
|
| 454 |
|
|
|
|
| 463 |
market_info,
|
| 464 |
ai_output,
|
| 465 |
csv_output,
|
| 466 |
+
preview
|
| 467 |
]
|
| 468 |
)
|
| 469 |
|