Svngoku commited on
Commit
aea1032
·
verified ·
1 Parent(s): 8b1dba0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -60
app.py CHANGED
@@ -1,100 +1,139 @@
1
- import os
2
- from dotenv import load_dotenv
3
  import gradio as gr
4
  import requests
5
 
6
- load_dotenv()
7
- COINGECKO_API_KEY = os.environ.get("COINGECKO_API_KEY")
8
- # Use your enterprise/pro API endpoint. For illustration, let's assume:
9
- COINGECKO_PRO_API_BASE = "https://pro-api.coingecko.com/api/v3"
10
 
11
- def get_current_price(coin_id: str = "bitcoin", vs_currency: str = "usd") -> dict:
 
 
 
12
  """
13
- Get current price for a cryptocurrency from CoinGecko PRO/Enterprise.
14
  """
15
- url = f"{COINGECKO_PRO_API_BASE}/simple/price"
16
  params = {
17
- "ids": coin_id,
18
- "vs_currencies": vs_currency,
19
  "include_market_cap": "true",
20
- "x_cg_pro_api_key": COINGECKO_API_KEY # As per CoinGecko PRO API docs
 
 
21
  }
22
  resp = requests.get(url, params=params)
23
- if resp.status_code == 200:
24
- data = resp.json()
25
- if coin_id in data:
26
- return {
27
- "coin": coin_id,
28
- "currency": vs_currency,
29
- "price": data[coin_id][vs_currency],
30
- "market_cap": data[coin_id].get(f"{vs_currency}_market_cap"),
31
- }
32
- else:
33
- return {"error": "Coin not found"}
34
- else:
35
- return {"error": f"Failed to fetch data ({resp.status_code}): {resp.text}"}
36
 
37
- def get_market_info() -> dict:
38
  """
39
- Get global crypto market info from CoinGecko PRO/Enterprise.
40
  """
41
- url = f"{COINGECKO_PRO_API_BASE}/global"
42
- params = {"x_cg_pro_api_key": COINGECKO_API_KEY}
43
- resp = requests.get(url, params=params)
44
- if resp.status_code == 200:
45
- data = resp.json()["data"]
46
  return {
47
- "active_cryptocurrencies": data["active_cryptocurrencies"],
48
- "markets": data["markets"],
49
- "total_market_cap_usd": data["total_market_cap"]["usd"],
50
- "market_cap_change_percentage_24h_usd": data["market_cap_change_percentage_24h_usd"],
51
- "updated_at": data["updated_at"]
 
 
 
52
  }
53
- else:
54
- return {"error": f"Failed to fetch data ({resp.status_code}): {resp.text}"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- def get_coin_history(coin_id: str = "bitcoin", date: str = "01-01-2023") -> dict:
 
 
 
57
  """
58
- Get historical price for a coin on a date (dd-mm-yyyy) using CoinGecko PRO/Enterprise.
59
  """
60
- url = f"{COINGECKO_PRO_API_BASE}/coins/{coin_id}/history"
61
- params = {"date": date, "x_cg_pro_api_key": COINGECKO_API_KEY}
62
  resp = requests.get(url, params=params)
63
- if resp.status_code == 200:
64
  data = resp.json()
65
- try:
66
- price = data["market_data"]["current_price"]["usd"]
67
- except Exception:
68
- price = None
69
  return {
70
  "coin": coin_id,
71
  "date": date,
72
  "price_usd": price,
73
  "snapshot": data.get("market_data", {})
74
  }
75
- else:
76
- return {"error": f"Failed to fetch data ({resp.status_code}): {resp.text}"}
 
 
 
 
 
 
 
 
 
 
 
77
 
78
  with gr.Blocks() as demo:
79
- gr.Markdown("# 🪙 CoinGecko MCP API (PRO)\n\n**Live, authenticated crypto data via your CoinGecko PRO endpoints.**")
80
- with gr.Tab("Get Current Price"):
 
 
81
  gr.Interface(
82
- fn=get_current_price,
83
  inputs=[
84
- gr.Textbox(value="bitcoin", label="Coin ID (e.g. bitcoin, ethereum)"),
85
- gr.Textbox(value="usd", label="Fiat Currency (e.g. usd, eur)")
86
  ],
87
  outputs=gr.JSON(),
88
  allow_flagging="never"
89
  )
90
- with gr.Tab("Global Market Info"):
91
  gr.Interface(
92
- fn=get_market_info,
93
- inputs=[],
 
 
 
 
 
 
 
 
 
 
 
94
  outputs=gr.JSON(),
95
  allow_flagging="never"
96
  )
97
- with gr.Tab("Get Coin History"):
98
  gr.Interface(
99
  fn=get_coin_history,
100
  inputs=[
@@ -104,6 +143,13 @@ with gr.Blocks() as demo:
104
  outputs=gr.JSON(),
105
  allow_flagging="never"
106
  )
 
 
 
 
 
 
 
107
 
108
  if __name__ == "__main__":
109
- demo.launch(mcp_server=True)
 
 
 
1
  import gradio as gr
2
  import requests
3
 
4
+ COINGECKO_API = "https://api.coingecko.com/api/v3"
 
 
 
5
 
6
+ def get_simple_price(
7
+ ids: str = "bitcoin,ethereum",
8
+ vs_currencies: str = "usd"
9
+ ) -> dict:
10
  """
11
+ Get current price for one or more coins.
12
  """
13
+ url = f"{COINGECKO_API}/simple/price"
14
  params = {
15
+ "ids": ids,
16
+ "vs_currencies": vs_currencies,
17
  "include_market_cap": "true",
18
+ "include_24hr_vol": "true",
19
+ "include_24hr_change": "true",
20
+ "include_last_updated_at": "true"
21
  }
22
  resp = requests.get(url, params=params)
23
+ try:
24
+ return resp.json()
25
+ except Exception:
26
+ return {"error": resp.text}
 
 
 
 
 
 
 
 
 
27
 
28
+ def get_coin_metadata(coin_id: str = "bitcoin") -> dict:
29
  """
30
+ Get full metadata for a given coin.
31
  """
32
+ url = f"{COINGECKO_API}/coins/{coin_id}"
33
+ resp = requests.get(url)
34
+ try:
35
+ data = resp.json()
 
36
  return {
37
+ "id": data.get("id"),
38
+ "symbol": data.get("symbol"),
39
+ "name": data.get("name"),
40
+ "description": data.get("description", {}).get("en"),
41
+ "homepage": data.get("links", {}).get("homepage", [None])[0],
42
+ "genesis_date": data.get("genesis_date"),
43
+ "market_data": data.get("market_data"),
44
+ "image": data.get("image", {}).get("large")
45
  }
46
+ except Exception:
47
+ return {"error": resp.text}
48
+
49
+ def get_markets(
50
+ vs_currency: str = "usd",
51
+ per_page: int = 10,
52
+ page: int = 1
53
+ ) -> dict:
54
+ """
55
+ Get a list of coins with price/market data (like top 10 by market cap).
56
+ """
57
+ url = f"{COINGECKO_API}/coins/markets"
58
+ params = {
59
+ "vs_currency": vs_currency,
60
+ "order": "market_cap_desc",
61
+ "per_page": per_page,
62
+ "page": page,
63
+ "sparkline": "false"
64
+ }
65
+ resp = requests.get(url, params=params)
66
+ try:
67
+ return resp.json()
68
+ except Exception:
69
+ return {"error": resp.text}
70
 
71
+ def get_coin_history(
72
+ coin_id: str = "bitcoin",
73
+ date: str = "01-01-2023"
74
+ ) -> dict:
75
  """
76
+ Get historical data for a coin (dd-mm-yyyy).
77
  """
78
+ url = f"{COINGECKO_API}/coins/{coin_id}/history"
79
+ params = {"date": date}
80
  resp = requests.get(url, params=params)
81
+ try:
82
  data = resp.json()
83
+ price = data.get("market_data", {}).get("current_price", {}).get("usd")
 
 
 
84
  return {
85
  "coin": coin_id,
86
  "date": date,
87
  "price_usd": price,
88
  "snapshot": data.get("market_data", {})
89
  }
90
+ except Exception:
91
+ return {"error": resp.text}
92
+
93
+ def get_global() -> dict:
94
+ """
95
+ Get global crypto stats (total market cap, #coins, #markets, etc).
96
+ """
97
+ url = f"{COINGECKO_API}/global"
98
+ resp = requests.get(url)
99
+ try:
100
+ return resp.json()
101
+ except Exception:
102
+ return {"error": resp.text}
103
 
104
  with gr.Blocks() as demo:
105
+ gr.Markdown(
106
+ "# 🦎 CoinGecko MCP API\n\nPublic CoinGecko endpoints exposed as MCP tools!"
107
+ )
108
+ with gr.Tab("Simple Price"):
109
  gr.Interface(
110
+ fn=get_simple_price,
111
  inputs=[
112
+ gr.Textbox(value="bitcoin,ethereum", label="Coin IDs (comma separated)"),
113
+ gr.Textbox(value="usd", label="Vs Currencies (comma separated)")
114
  ],
115
  outputs=gr.JSON(),
116
  allow_flagging="never"
117
  )
118
+ with gr.Tab("Markets (Top N)"):
119
  gr.Interface(
120
+ fn=get_markets,
121
+ inputs=[
122
+ gr.Textbox(value="usd", label="Fiat Currency"),
123
+ gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Results per page"),
124
+ gr.Slider(minimum=1, maximum=10, value=1, step=1, label="Page Number")
125
+ ],
126
+ outputs=gr.JSON(),
127
+ allow_flagging="never"
128
+ )
129
+ with gr.Tab("Coin Metadata"):
130
+ gr.Interface(
131
+ fn=get_coin_metadata,
132
+ inputs=[gr.Textbox(value="bitcoin", label="Coin ID")],
133
  outputs=gr.JSON(),
134
  allow_flagging="never"
135
  )
136
+ with gr.Tab("Coin History"):
137
  gr.Interface(
138
  fn=get_coin_history,
139
  inputs=[
 
143
  outputs=gr.JSON(),
144
  allow_flagging="never"
145
  )
146
+ with gr.Tab("Global Stats"):
147
+ gr.Interface(
148
+ fn=get_global,
149
+ inputs=[],
150
+ outputs=gr.JSON(),
151
+ allow_flagging="never"
152
+ )
153
 
154
  if __name__ == "__main__":
155
+ demo.launch(mcp_server=True)