|
import numpy as np |
|
import pandas as pd |
|
from sklearn.preprocessing import MinMaxScaler |
|
from keras.models import load_model |
|
import requests |
|
import gradio as gr |
|
|
|
|
|
model = load_model('eurusdModel2.h5') |
|
|
|
|
|
def get_live_price(): |
|
url = 'https://api.exchangerate-api.com/v4/latest/EUR' |
|
response = requests.get(url) |
|
if response.status_code == 200: |
|
data = response.json() |
|
return data['rates']['USD'] |
|
else: |
|
print("Failed to retrieve data:", response.status_code) |
|
return None |
|
|
|
|
|
def prepare_input(scaled_data, today_price): |
|
time_step = 60 |
|
last_prices = scaled_data[-(time_step - 1):] |
|
|
|
|
|
scaler = MinMaxScaler(feature_range=(0, 1)) |
|
today_scaled = scaler.fit_transform([[today_price]]) |
|
|
|
|
|
input_data = np.concatenate((last_prices, today_scaled), axis=0) |
|
input_data = input_data.reshape(1, input_data.shape[0], 1) |
|
return input_data, scaler |
|
|
|
|
|
def predict_eur_usd(): |
|
today_price = get_live_price() |
|
if today_price is None: |
|
return "Failed to retrieve the current price." |
|
|
|
|
|
historical_data = pd.read_csv('EUR_USD Historical Data.csv', parse_dates=['Date'], index_col='Date') |
|
historical_data = historical_data[['Price']] |
|
|
|
|
|
scaler = MinMaxScaler(feature_range=(0, 1)) |
|
scaled_data = scaler.fit_transform(historical_data) |
|
|
|
|
|
input_data, scaler = prepare_input(scaled_data, today_price) |
|
|
|
|
|
predicted_price = model.predict(input_data) |
|
predicted_price = scaler.inverse_transform(predicted_price) |
|
|
|
return f"Today's Price: {today_price:.4f}, Predicted EUR/USD Price for Tomorrow: {predicted_price[0][0]:.4f}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_eur_usd, |
|
inputs=[], |
|
outputs="text", |
|
title="EUR/USD Price Predictor", |
|
description="Fetches the latest EUR/USD price and predicts the price for tomorrow." |
|
) |
|
|
|
|
|
iface.launch() |