munem420 commited on
Commit
ea314d3
·
verified ·
1 Parent(s): 4b9dc02

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +64 -12
src/App.js CHANGED
@@ -1,25 +1,77 @@
 
1
  import logo from './logo.svg';
2
  import './App.css';
3
 
4
  function App() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  return (
6
  <div className="App">
7
  <header className="App-header">
8
  <img src={logo} className="App-logo" alt="logo" />
9
- <p>
10
- Edit <code>src/App.js</code> and save to reload.
11
- </p>
12
- <a
13
- className="App-link"
14
- href="https://reactjs.org"
15
- target="_blank"
16
- rel="noopener noreferrer"
17
- >
18
- Learn React
19
- </a>
 
 
 
 
 
 
 
 
 
 
 
 
20
  </header>
21
  </div>
22
  );
23
  }
24
 
25
- export default App;
 
1
+ import React, { useState } from 'react';
2
  import logo from './logo.svg';
3
  import './App.css';
4
 
5
  function App() {
6
+ const [ticker, setTicker] = useState('GOOGL'); // Default ticker
7
+ const [prediction, setPrediction] = useState('');
8
+ const [isLoading, setIsLoading] = useState(false);
9
+ const [error, setError] = useState('');
10
+
11
+ const handleForecast = async () => {
12
+ setIsLoading(true);
13
+ setError('');
14
+ setPrediction('');
15
+
16
+ try {
17
+ // The Gradio API endpoint is at /run/predict
18
+ const response = await fetch('/run/predict', {
19
+ method: 'POST',
20
+ headers: { 'Content-Type': 'application/json' },
21
+ // The data payload must be in this specific format for Gradio
22
+ body: JSON.stringify({
23
+ data: [ticker],
24
+ }),
25
+ });
26
+
27
+ if (!response.ok) {
28
+ throw new Error(`HTTP error! Status: ${response.status}`);
29
+ }
30
+
31
+ const result = await response.json();
32
+
33
+ // The prediction is in the 'data' array of the response
34
+ const forecastText = result.data[0];
35
+ setPrediction(forecastText);
36
+
37
+ } catch (err) {
38
+ console.error("Failed to fetch prediction:", err);
39
+ setError('Failed to get forecast. Check the ticker or try again later.');
40
+ } finally {
41
+ setIsLoading(false);
42
+ }
43
+ };
44
+
45
  return (
46
  <div className="App">
47
  <header className="App-header">
48
  <img src={logo} className="App-logo" alt="logo" />
49
+ <h1>Stock Forecaster</h1>
50
+ <p>Enter a stock ticker (e.g., AAPL, GOOGL, TSLA) to predict the next day's closing price.</p>
51
+
52
+ <div className="input-container">
53
+ <input
54
+ type="text"
55
+ value={ticker}
56
+ onChange={(e) => setTicker(e.target.value.toUpperCase())}
57
+ placeholder="Enter Ticker"
58
+ className="ticker-input"
59
+ />
60
+ <button onClick={handleForecast} disabled={isLoading} className="forecast-button">
61
+ {isLoading ? 'Forecasting...' : 'Get Forecast'}
62
+ </button>
63
+ </div>
64
+
65
+ {error && <p className="error-message">{error}</p>}
66
+
67
+ {prediction && (
68
+ <div className="prediction-result">
69
+ <pre>{prediction}</pre>
70
+ </div>
71
+ )}
72
  </header>
73
  </div>
74
  );
75
  }
76
 
77
+ export default App;