File size: 2,960 Bytes
ef11559
 
 
 
 
 
 
 
51fca9d
ef11559
 
 
e4fc11f
ef11559
 
 
 
 
 
e4fc11f
 
ef11559
 
 
 
 
 
 
 
e4fc11f
ef11559
 
 
 
 
e4fc11f
ef11559
 
 
 
 
 
 
 
 
e4fc11f
ef11559
e4fc11f
ef11559
 
 
e4fc11f
ef11559
 
e4fc11f
ef11559
 
 
e4fc11f
 
bd4667f
2fa9187
 
ef11559
e4fc11f
ef11559
 
e4fc11f
ef11559
c593075
 
0a02efa
c593075
 
ef11559
 
 
 
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
78
79
import gradio as gr
import tensorflow as tf
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.cm as cm

# Load the saved model
model = tf.keras.models.load_model('temperature_model.h5')

# Prediction logic
def predict_temperature(model, date):
    # Convert the input date to a numerical feature (e.g., day of the year)
    try:
        date_obj = datetime.strptime(date, '%Y-%m-%d')
        day_of_year = date_obj.timetuple().tm_yday  # Get the day of the year (1-366)
    except ValueError:
        return "Invalid date format! Please enter the date in 'YYYY-MM-DD' format."

    # Create input data for the model
    input_data = np.array([[day_of_year]], dtype=np.float32)
    
    # Predict temperature
    forecast = model.predict(input_data).flatten()
    
    # Create a color map for warm and cold temperatures
    cmap = cm.get_cmap("coolwarm")
    norm = plt.Normalize(vmin=-30, vmax=40)  # Set min and max temperature values
    
    # Plotting with temperature color mapping
    fig, ax = plt.subplots(figsize=(8, 6))
    color = cmap(norm(forecast[0]))
    
    ax.plot([day_of_year], [forecast[0]], marker='o', color=color, linestyle='-', markersize=10, label=f"Predicted Temp: {forecast[0]:.2f}°C")
    
    # Customize the plot
    ax.set_title(f"Temperature Forecast for {date}", fontsize=18, fontweight='bold', color='darkslategray')
    ax.set_xlabel("Day of the Year", fontsize=14, color='slategray')
    ax.set_ylabel("Temperature (°C)", fontsize=14, color='slategray')
    ax.set_xticks([day_of_year])
    ax.set_xticklabels([date], rotation=45, ha='right', fontsize=12, color='darkslategray')
    ax.grid(True, which='both', linestyle='--', linewidth=0.5, color='lightgray')
    ax.legend(loc="upper left", fontsize=12)
    ax.margins(y=0.1)
    
    # Add color bar to indicate temperature range
    sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
    sm.set_array([])  # No data needed for colorbar
    cbar = fig.colorbar(sm, ax=ax)
    cbar.set_label("Temperature (°C)", fontsize=12)
    
    # Return the forecasted temperature and plot
    return f"The forecasted temperature for {date} is {forecast[0]:.2f}°C.", fig

# Gradio interface function
def gradio_interface(date):
    return predict_temperature(model, date)

# Set up Gradio input (textbox for date entry)
date_input = gr.Textbox(label="Enter Date (YYYY-MM-DD)", placeholder="2024-12-31")

# Custom CSS for a warm blue color theme


# Set up the Gradio interface
iface = gr.Interface(
    fn=gradio_interface, 
    inputs=date_input, 
    outputs=["text", "plot"],  # Output both text and plot
    title="Temperature Forecasting 🌡️",
    description="Enter a date to predict the temperature and see it plotted with a color gradient🚨 ...Have a good day🤖🥵🥶",
    live=False,
    theme="huggingface",  # Keep the compact theme
    allow_flagging="never"
)

# Launch the Gradio interface
iface.launch()