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()