indraw111 commited on
Commit
d79130d
·
verified ·
1 Parent(s): c9544f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -32
app.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  import numpy as np
3
  import tensorflow as tf
4
  import joblib
 
 
 
5
 
6
  # Load the model and scalers
7
  lstm_model = tf.keras.models.load_model('retrained_lstm_model.h5', compile=False)
@@ -12,21 +15,29 @@ scaler_y = joblib.load('scaler_y.joblib')
12
  # Define window size
13
  window_size = 7
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # Create Gradio app
16
  with gr.Blocks(fill_height=True, title="Rainfall Prediction with LSTM and Gemma 2") as demo:
17
- # Set up Gemma login
18
- with gr.Sidebar():
19
- gr.Markdown("# Inference Provider")
20
- gr.Markdown("This Space showcases the google/gemma-2-2b-it model, served by the Nebius API. Sign in with your Hugging Face account to use this API.")
21
- login_button = gr.LoginButton("Sign in with Hugging Face")
22
- model_provider = gr.Dropdown(
23
- ["nebius"],
24
- label="Provider",
25
- value="nebius",
26
- visible=False
27
- )
28
-
29
- # Main content
30
  gr.Markdown("# Rainfall Prediction with LSTM and Gemma 2 Interpretation")
31
  gr.Markdown("Enter temperature, humidity, and sunshine duration to predict rainfall.")
32
 
@@ -41,13 +52,6 @@ with gr.Blocks(fill_height=True, title="Rainfall Prediction with LSTM and Gemma
41
  rainfall_output = gr.Textbox(label="Predicted Rainfall (mm)")
42
  interpretation_output = gr.Markdown(label="Interpretation by Gemma 2")
43
 
44
- # Load Gemma model using gr.load - this returns a function to call the model
45
- gemma_model = gr.load(
46
- "models/google/gemma-2-2b-it",
47
- provider=model_provider,
48
- accept_token=login_button
49
- )
50
-
51
  def predict_rainfall(tavg, rh_avg, ss):
52
  # Create sequential input
53
  user_input = np.array([[tavg, rh_avg, ss]] * window_size)
@@ -77,18 +81,8 @@ with gr.Blocks(fill_height=True, title="Rainfall Prediction with LSTM and Gemma
77
  Provide a concise interpretation with practical implications.
78
  """
79
 
80
- # Request Gemma model for interpretation
81
- try:
82
- # Call the loaded model directly with the prompt
83
- response = gemma_model(
84
- prompt,
85
- max_new_tokens=300,
86
- temperature=0.7,
87
- top_p=0.9
88
- )
89
- interpretation = response
90
- except Exception as e:
91
- interpretation = f"Could not generate interpretation. Please make sure you're signed in with your Hugging Face account. Error: {str(e)}"
92
 
93
  return f"{rainfall_mm:.2f}", interpretation
94
 
 
2
  import numpy as np
3
  import tensorflow as tf
4
  import joblib
5
+ import requests
6
+ import json
7
+ import os
8
 
9
  # Load the model and scalers
10
  lstm_model = tf.keras.models.load_model('retrained_lstm_model.h5', compile=False)
 
15
  # Define window size
16
  window_size = 7
17
 
18
+ # Function to query Gemma model using Hugging Face Inference API
19
+ def query_gemma(prompt):
20
+ API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-2b-it"
21
+ headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
22
+
23
+ payload = {
24
+ "inputs": prompt,
25
+ "parameters": {
26
+ "max_new_tokens": 300,
27
+ "temperature": 0.7,
28
+ "top_p": 0.9,
29
+ "do_sample": True
30
+ }
31
+ }
32
+
33
+ try:
34
+ response = requests.post(API_URL, headers=headers, json=payload)
35
+ return response.json()[0]["generated_text"].replace(prompt, "").strip()
36
+ except Exception as e:
37
+ return f"Error generating interpretation: {str(e)}"
38
+
39
  # Create Gradio app
40
  with gr.Blocks(fill_height=True, title="Rainfall Prediction with LSTM and Gemma 2") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  gr.Markdown("# Rainfall Prediction with LSTM and Gemma 2 Interpretation")
42
  gr.Markdown("Enter temperature, humidity, and sunshine duration to predict rainfall.")
43
 
 
52
  rainfall_output = gr.Textbox(label="Predicted Rainfall (mm)")
53
  interpretation_output = gr.Markdown(label="Interpretation by Gemma 2")
54
 
 
 
 
 
 
 
 
55
  def predict_rainfall(tavg, rh_avg, ss):
56
  # Create sequential input
57
  user_input = np.array([[tavg, rh_avg, ss]] * window_size)
 
81
  Provide a concise interpretation with practical implications.
82
  """
83
 
84
+ # Get interpretation from Gemma
85
+ interpretation = query_gemma(prompt)
 
 
 
 
 
 
 
 
 
 
86
 
87
  return f"{rainfall_mm:.2f}", interpretation
88