Spaces:
Running
Running
Update src/App.js
Browse files- src/App.js +25 -71
src/App.js
CHANGED
|
@@ -1,77 +1,31 @@
|
|
| 1 |
-
import React, { useState } from
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
const
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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
|
| 47 |
-
<
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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;
|
|
|
|
| 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 |
}
|
|
|
|
|
|