File size: 2,280 Bytes
ea7820c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea314d3
 
3b81cd2
ea7820c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b81cd2
 
 
ea7820c
 
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
import React, { useState } from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  const [ticker, setTicker] = useState('GOOGL'); // Default ticker
  const [prediction, setPrediction] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState('');

  const handleForecast = async () => {
    setIsLoading(true);
    setError('');
    setPrediction('');

    try {
      // The Gradio API endpoint is at /run/predict
      const response = await fetch('/run/predict', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        // The data payload must be in this specific format for Gradio
        body: JSON.stringify({
          data: [ticker],
        }),
      });

      if (!response.ok) {
        throw new Error(`HTTP error! Status: ${response.status}`);
      }

      const result = await response.json();
      
      // The prediction is in the 'data' array of the response
      const forecastText = result.data[0];
      setPrediction(forecastText);

    } catch (err) {
      console.error("Failed to fetch prediction:", err);
      setError('Failed to get forecast. Check the ticker or try again later.');
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <h1>Stock Forecaster</h1>
        <p>Enter a stock ticker (e.g., AAPL, GOOGL, TSLA) to predict the next day's closing price.</p>
        
        <div className="input-container">
          <input
            type="text"
            value={ticker}
            onChange={(e) => setTicker(e.target.value.toUpperCase())}
            placeholder="Enter Ticker"
            className="ticker-input"
          />
          <button onClick={handleForecast} disabled={isLoading} className="forecast-button">
            {isLoading ? 'Forecasting...' : 'Get Forecast'}
          </button>
        </div>

        {error && <p className="error-message">{error}</p>}
        
        {prediction && (
          <div className="prediction-result">
            <pre>{prediction}</pre>
          </div>
        )}
      </header>
    </div>
  );
}

export default App;