File size: 6,062 Bytes
be35101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cryptocurrency Price Prediction</title>

  <link rel="shortcut icon" type="image/x-icon" href="https://raw.githubusercontent.com/belajarqywok/belajarqywok.github.io/main/favicon.ico">

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns@3.0.0/dist/chartjs-adapter-date-fns.bundle.min.js"></script>
</head>

<body onload="predict()">

  <header class="bg-dark py-3 mb-4">
    <div class="container">
      <div class="row">
        <div class="col">
          <h1 class="text-white">Cryptocurrency Price Prediction</h1>
        </div>
      </div>
    </div>
  </header>

  <div class="container">
    <div class="card shadow-sm">
      <div class="card-body">
        <div class="row mb-3">
          <div class="col-md-3">
            <label for="days" class="form-label">Days to Predict:</label>
            <input type="number" id="days" name="days" class="form-control" min="1" value="7" onchange="predict()">
          </div>
          <div class="col-md-3">
            <label for="crypto" class="form-label">Cryptocurrency:</label>
            <select id="crypto" class="form-select" onchange="predict()">
              <option value="ADA-USD">ADA-USD</option>
            </select>
          </div>
          <div class="col-md-3 align-self-end">
            <div class="spinner-border text-primary d-none" role="status" id="loadingSpinner">
              <span class="visually-hidden">Loading...</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="row mt-4">
      <div class="col">
        <canvas id="myChart" width="400" height="200"></canvas>
      </div>
    </div>
  </div>
  <br \>

  <script>
    fetch('https://qywok-cryptocurrency-prediction.hf.space/crypto/lists')
      .then(response => response.json())
      .then(data => {
        const cryptoList = data.data
        const selectCrypto = document.getElementById('crypto')

        selectCrypto.innerHTML = ''
        cryptoList.forEach(crypto => {
          const option = document.createElement('option')
          option.value = crypto
          option.text  = crypto

          selectCrypto.appendChild(option)
        })
      })
      .catch(error => console.error('Error fetching crypto list:', error))
  </script>

  <script>
    let myChart

    const predict = async () => {
      const days = document.getElementById('days').value
      const crypto = document.getElementById('crypto').value
      const loadingSpinner = document.getElementById('loadingSpinner')
      loadingSpinner.classList.remove('d-none')

      const apiUrl = 'https://qywok-cryptocurrency-prediction.hf.space/crypto/prediction'

      try {
        const response = await fetch(apiUrl, {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            days: days,
            currency: crypto
          })
        })

        if (!response.ok) throw new Error('Network response was not ok')

        const data = await response.json()
        updateChart(data)

      } catch (error) {
        console.error('Error fetching data:', error)
      } finally {
        loadingSpinner.classList.add('d-none')
      }
    }
  </script>

  <script>
    const updateChart = (data) => {
      const actualDates = data.data.predictions
        .actuals.map(entry => new Date(entry.date))

      const actualPrices = data.data.predictions
        .actuals.map(entry => entry.price)

      const predictionDates = data.data.predictions
        .predictions.map(entry => new Date(entry.date))

      const predictionPrices = data.data.predictions
        .predictions.map(entry => entry.price)

      const dates = [...actualDates, ...predictionDates]
      const prices = [...actualPrices, ...predictionPrices]

      if (myChart) {
        myChart.data.labels = dates
        myChart.data.datasets[0].data = actualDates.map(
          (date, index) => ({ x: date, y: actualPrices[index] })
        )

        myChart.data.datasets[1].data = predictionDates.map(
          (date, index) => ({ x: date, y: predictionPrices[index] })
        )

        myChart.update()
      } else {
        const ctx = document.getElementById('myChart').getContext('2d')
        myChart = new Chart(ctx, {
          type: 'line',
          data: {
            labels: dates,
            datasets: [
              {
                label: 'Actual',
                data: actualDates.map(
                  (date, index) => ({ x: date, y: actualPrices[index] })
                ),
                borderColor: 'rgba(75, 192, 192, 1)',
                borderWidth: 1,
                fill: false,
                tension: 0.1
              },
              {
                label: 'Prediction',
                data: predictionDates.map(
                  (date, index) => ({ x: date, y: predictionPrices[index] })
                ),
                borderColor: 'rgba(255, 99, 132, 1)',
                borderWidth: 1,
                fill: false,
                tension: 0.1
              }
            ]
          },
          options: {
            scales: {
              x: {
                type: 'time',
                time: {
                  unit: 'day',
                  tooltipFormat: 'yyyy-MM-dd'
                },
                title: {
                  display: true,
                  text: 'Date'
                }
              },
              y: {
                beginAtZero: false,
                title: {
                  display: true,
                  text: 'Price (USD)'
                }
              }
            }
          }
        })
      }
    }
  </script>
</body>
</html>