NightPrince commited on
Commit
ef11559
·
verified ·
1 Parent(s): 1c9801b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from datetime import datetime
5
+ import matplotlib.pyplot as plt
6
+ import matplotlib.cm as cm
7
+
8
+ # Load the saved model
9
+ model = tf.keras.models.load_model('/kaggle/working/temperature_model.h5')
10
+
11
+ # Prediction logic
12
+ def predict_temperature(model, date):
13
+ # Convert the input date to a numerical feature (e.g., day of the year, timestamp)
14
+ try:
15
+ date_obj = datetime.strptime(date, '%Y-%m-%d')
16
+ day_of_year = date_obj.timetuple().tm_yday # Get the day of the year (1-366)
17
+ except ValueError:
18
+ return "Invalid date format! Please enter the date in 'YYYY-MM-DD' format."
19
+
20
+ # Create input data for the model (assuming the model accepts this as a feature)
21
+ input_data = np.array([[day_of_year]], dtype=np.float32) # Reshape to (1, 1) if necessary
22
+
23
+ # Predict temperature
24
+ forecast = model.predict(input_data).flatten()
25
+
26
+ # Create a color map for warm and cold temperatures
27
+ cmap = cm.get_cmap("coolwarm")
28
+ norm = plt.Normalize(vmin=-30, vmax=40) # Set min and max temperature values
29
+
30
+ # Plotting with enhanced aesthetics
31
+ fig, ax = plt.subplots(figsize=(8, 6))
32
+
33
+ # Map temperature value to color
34
+ color = cmap(norm(forecast[0]))
35
+
36
+ ax.plot([day_of_year], [forecast[0]], marker='o', color=color, linestyle='-', markersize=10, label=f"Predicted Temp: {forecast[0]:.2f}°C")
37
+
38
+ # Customize the plot for better visualization
39
+ ax.set_title(f"Temperature Forecast for {date}", fontsize=18, fontweight='bold', color='darkslategray')
40
+ ax.set_xlabel("Day of the Year", fontsize=14, color='slategray')
41
+ ax.set_ylabel("Temperature (°C)", fontsize=14, color='slategray')
42
+ ax.set_xticks([day_of_year])
43
+ ax.set_xticklabels([date], rotation=45, ha='right', fontsize=12, color='darkslategray')
44
+ ax.grid(True, which='both', linestyle='--', linewidth=0.5, color='lightgray')
45
+ ax.legend(loc="upper left", fontsize=12)
46
+ ax.margins(y=0.1)
47
+
48
+ # Adding color bar to indicate temperature range
49
+ sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
50
+ sm.set_array([]) # No data is required for colorbar
51
+ cbar = fig.colorbar(sm, ax=ax)
52
+ cbar.set_label("Temperature (°C)", fontsize=12)
53
+
54
+ # Adding interactive features with annotations
55
+ ax.annotate(f"{forecast[0]:.2f}°C", (day_of_year, forecast[0]), textcoords="offset points", xytext=(0,10), ha='center', fontsize=14, color='darkgreen')
56
+
57
+ # Return the plot and the forecasted temperature
58
+ return f"The forecasted temperature for {date} is {forecast[0]:.2f}°C.", fig
59
+
60
+ # Example Gradio interface
61
+ def gradio_interface(date):
62
+ return predict_temperature(model, date)
63
+
64
+ # Set up Gradio input (use Textbox for date entry)
65
+ date_input = gr.Textbox(label="Enter Date (YYYY-MM-DD)", placeholder="2024-12-31") # Allow user to input a date manually
66
+
67
+ # Custom CSS for enhanced interface
68
+ css = """
69
+ #interface {
70
+ background: linear-gradient(to right, #00b4db, #0083b0); /* Cool blue to warm orange gradient */
71
+ border-radius: 20px;
72
+ padding: 20px;
73
+ box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
74
+ }
75
+ #interface .output {
76
+ font-size: 18px;
77
+ color: #333;
78
+ font-family: Arial, sans-serif;
79
+ }
80
+ #interface .input-textbox {
81
+ font-size: 16px;
82
+ color: #333;
83
+ border: 2px solid #0083b0;
84
+ border-radius: 10px;
85
+ padding: 10px;
86
+ width: 300px;
87
+ }
88
+ #interface .input-textbox:focus {
89
+ border-color: #00b4db;
90
+ outline: none;
91
+ }
92
+ #interface .output-text {
93
+ font-size: 16px;
94
+ font-weight: bold;
95
+ color: #333;
96
+ }
97
+ #interface .output-plot {
98
+ border-radius: 10px;
99
+ box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
100
+ margin-top: 20px;
101
+ }
102
+ """
103
+
104
+ # Set up Gradio interface with more customization
105
+ iface = gr.Interface(
106
+ fn=gradio_interface,
107
+ inputs=date_input,
108
+ outputs=["text", "plot"], # Output both text and plot
109
+ title="Interactive Temperature Forecasting",
110
+ description="This application predicts the temperature for a given date based on historical data. Enter a date to get the prediction and see it plotted interactively.",
111
+ live=True, # Live updates if needed
112
+ theme="compact", # Compact theme for better aesthetics
113
+ allow_flagging="never", # Optional: Hide flagging options to improve UI
114
+ css=css # Apply custom CSS
115
+ )
116
+
117
+ # Launch the Gradio interface
118
+ iface.launch()