munem420 commited on
Commit
ea7820c
·
verified ·
1 Parent(s): 6d7a718

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +71 -25
src/App.js CHANGED
@@ -1,31 +1,77 @@
1
- import React, { useState } from "react";
2
-
3
- export default function App() {
4
- const [input, setInput] = useState("");
5
- const [prediction, setPrediction] = useState("");
6
-
7
- const handlePredict = async () => {
8
- const res = await fetch("/api/predict", {
9
- method: "POST",
10
- body: JSON.stringify({ input }),
11
- headers: { "Content-Type": "application/json" }
12
- });
13
- const data = await res.json();
14
- setPrediction(data.prediction);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  };
16
 
17
  return (
18
- <div style={{ padding: "2rem", fontFamily: "sans-serif" }}>
19
- <h1>Stock Price Forecaster (LSTM)</h1>
20
- <input
21
- type="text"
22
- placeholder="Enter Ticker or Company Name"
23
- value={input}
24
- onChange={(e) => setInput(e.target.value)}
25
- style={{ padding: "0.5rem", width: "300px" }}
26
- />
27
- <button onClick={handlePredict} style={{ marginLeft: "1rem", padding: "0.5rem" }}>Predict</button>
28
- {prediction && <p>Predicted Next Day Close: ${prediction}</p>}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  </div>
30
  );
31
  }
 
 
 
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;