KAPtechies commited on
Commit
e4075a6
·
verified ·
1 Parent(s): dec2f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -1,14 +1,13 @@
1
- import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
- import seaborn as sns
6
  from sklearn.preprocessing import MinMaxScaler
7
  from tensorflow.keras.models import Sequential
8
  from tensorflow.keras.layers import LSTM, Dense, Dropout
9
 
10
  # Load the dataset
11
- df = pd.read_csv("/content/TEMP_ANNUAL_SEASONAL_MEAN.csv")
12
 
13
  # Convert 'YEAR' column to datetime
14
  df.columns = df.columns.str.upper() # Ensure column names are uppercase
@@ -55,20 +54,33 @@ model.compile(optimizer='adam', loss='mse')
55
  # Train the model
56
  model.fit(X_train, y_train, epochs=50, batch_size=8, validation_data=(X_test, y_test))
57
 
58
- # Make predictions
59
- predictions = model.predict(X_test)
60
- predictions = scaler.inverse_transform(predictions) # Convert back to original scale
 
 
61
 
62
- # Streamlit UI
63
- st.title("India Climate Change Forecast")
64
 
65
- st.write("### Actual vs Predicted Annual Mean Temperature")
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- fig, ax = plt.subplots(figsize=(12, 6))
68
- ax.plot(df.index[-len(y_test):], scaler.inverse_transform(y_test.reshape(-1, 1)), label="Actual")
69
- ax.plot(df.index[-len(y_test):], predictions, label="Predicted", linestyle='dashed')
70
- ax.set_xlabel("Year")
71
- ax.set_ylabel("Temperature (°C)")
72
- ax.set_title("Actual vs Predicted Annual Mean Temperature in India")
73
- ax.legend()
74
- st.pyplot(fig)
 
1
+ import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
  import matplotlib.pyplot as plt
 
5
  from sklearn.preprocessing import MinMaxScaler
6
  from tensorflow.keras.models import Sequential
7
  from tensorflow.keras.layers import LSTM, Dense, Dropout
8
 
9
  # Load the dataset
10
+ df = pd.read_csv("TEMP_ANNUAL_SEASONAL_MEAN.csv") # Ensure file is in the repo
11
 
12
  # Convert 'YEAR' column to datetime
13
  df.columns = df.columns.str.upper() # Ensure column names are uppercase
 
54
  # Train the model
55
  model.fit(X_train, y_train, epochs=50, batch_size=8, validation_data=(X_test, y_test))
56
 
57
+ # Function to predict temperature
58
+ def predict_temperature(year):
59
+ try:
60
+ last_10_years = data_scaled[-sequence_length:] # Take last 10 years of data
61
+ last_10_years = last_10_years.reshape(1, sequence_length, 1)
62
 
63
+ pred_scaled = model.predict(last_10_years)
64
+ pred_temp = scaler.inverse_transform(pred_scaled)[0][0]
65
 
66
+ # Plot actual vs predicted
67
+ fig, ax = plt.subplots(figsize=(8, 4))
68
+ ax.plot(df.index[-len(y_test):], scaler.inverse_transform(y_test.reshape(-1, 1)), label="Actual")
69
+ ax.plot(df.index[-len(y_test):], scaler.inverse_transform(model.predict(X_test)), label="Predicted", linestyle='dashed')
70
+ ax.set_xlabel("Year")
71
+ ax.set_ylabel("Temperature (°C)")
72
+ ax.set_title("Actual vs Predicted Annual Mean Temperature")
73
+ ax.legend()
74
+
75
+ return pred_temp, fig
76
+ except Exception as e:
77
+ return f"Error: {str(e)}"
78
 
79
+ # Gradio Interface
80
+ iface = gr.Interface(
81
+ fn=predict_temperature,
82
+ inputs=gr.Number(label="Enter Year (e.g., 2027)"),
83
+ outputs=[gr.Textbox(label="Predicted Temperature (°C)"), gr.Plot(label="Actual vs Predicted")]
84
+ )
85
+
86
+ iface.launch()