Team Ai
Apppublic

typesdigital/CryptoUpdate

sourceHugging Facecc0-1.0updated 3y agoView on Hugging Face
1likes
app.py56 linesDownload Raw Back to root
1import requests2import openai3 4# Set up the CoinMarketCap API endpoint and parameters5url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'6parameters = {7  'start': '1',8  'limit': '10',9  'convert': 'USD'10}11 12# Set up the OpenAI API credentials13openai.api_key = '<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>'14 15# Fetch the latest cryptocurrency prices from the CoinMarketCap API16response = requests.get(url, headers={'X-CMC_PRO_API_KEY': '<03365e9f-2220-4083-90a7-151a70bb40ae>'}, params=parameters)17 18# Check if the response is successful19if response.status_code == 200:20  # Extract the data from the API response21  data = response.json()22  # Extract the relevant data from the API response23  bitcoin_price = data['data']['cryptocurrency_list'][0]['quote']['USD']['price']24  ethereum_price = data['data']['cryptocurrency_list'][1]['quote']['USD']['price']25  bitcoin_margin = data['data']['cryptocurrency_list'][0]['quote']['USD']['percent_change_24h']26  ethereum_margin = data['data']['cryptocurrency_list'][1]['quote']['USD']['percent_change_24h']27 28  # Use the OpenAI API to generate a prediction for the margin29  prompt = f"Based on the latest cryptocurrency prices, what will be the margin for Bitcoin and Ethereum in the next 24 hours?"30  model = "text-davinci-002"31  response = openai.Completion.create(32    engine=model,33    prompt=prompt,34    temperature=0.5,35    max_tokens=50,36    n=1,37    stop=None,38    timeout=20,39  )40 41  # Extract the predicted margin from the OpenAI API response42  predicted_margin = response.choices[0].text.strip()43 44  # Print the results45  print("Latest cryptocurrency prices:")46  print(f"Bitcoin: ${bitcoin_price:.2f}")47  print(f"Ethereum: ${ethereum_price:.2f}")48  print("24-hour margin:")49  print(f"Bitcoin: {bitcoin_margin:.2f}%")50  print(f"Ethereum: {ethereum_margin:.2f}%")51  print("Predicted margin for the next 24 hours:")52  print(predicted_margin)53 54else:55  print("Error: API request failed")56