MLDeveloper commited on
Commit
372b3f3
·
verified ·
1 Parent(s): 16277c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -2,33 +2,31 @@ import streamlit as st
2
  import pandas as pd
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
- import openai
6
-
7
- import streamlit as st
8
- import pandas as pd
9
  import requests
 
10
 
11
- # Load the solar data CSV file
12
- df = pd.read_csv('https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/main/solar_data_india_2024.csv')
 
 
 
13
 
14
- # Set up the Gemini API key (replace with your actual Gemini API key)
15
- GEMINI_API_KEY = 'AIzaSyAGGP8I7c0YmA8xKZsEdAF9AF3ElaPoEn4'
16
 
17
- # Set the API endpoint for Gemini
18
- GEMINI_API_URL = "https://gemini.googleapis.com/v1/completions" # Example URL, please check the actual URL
19
 
20
  # Streamlit UI
21
  st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
22
  st.title("AI-based Solar Project Estimation Tool")
23
 
24
- # Center all input widgets
25
  st.write("### Enter Your Details Below:")
26
 
27
  with st.form("solar_form"):
28
- # Get the list of unique states from the dataset
29
  state_options = df['State'].dropna().unique()
30
 
31
- # Input widgets
32
  location = st.selectbox("Select your State", options=sorted(state_options))
33
  roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
34
  electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
@@ -36,21 +34,19 @@ with st.form("solar_form"):
36
  submitted = st.form_submit_button("Get Estimate")
37
 
38
  if submitted and location and roof_size > 0 and electricity_bill >= 0:
39
- # Fetch state data from the dataset
40
- state_data = df[df['State'].str.contains(location, case=False)].iloc[0] # Get the first match
41
 
42
  if state_data is not None:
43
  ghi = state_data['Avg_GHI (kWh/m²/day)']
44
  solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
45
 
46
- # Use Gemini API to generate solar project estimate (cost, savings, payback period)
47
- prompt = f"""
48
  Estimate the solar system for the location '{location}' based on the following details:
49
- Roof size: {roof_size} sq meters
50
- Monthly electricity bill: ₹{electricity_bill}
51
- Average GHI (solar radiation) for {location}: {ghi} kWh/m²/day
52
- Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
53
-
54
  Provide:
55
  1. Estimated solar system size in kW
56
  2. Estimated daily solar output in kWh
@@ -59,31 +55,33 @@ if submitted and location and roof_size > 0 and electricity_bill >= 0:
59
  5. Payback period in years
60
  """
61
 
62
- # Request to Gemini API (Adjust headers and body according to Gemini API documentation)
63
- response = requests.post(
64
- GEMINI_API_URL,
65
- headers={
66
- "Authorization": f"Bearer {GEMINI_API_KEY}",
67
- "Content-Type": "application/json"
68
- },
69
- json={
70
- "model": "gemini-model-xyz", # Use the actual Gemini model name
71
- "prompt": prompt,
72
- "max_tokens": 250,
73
- "temperature": 0.7,
74
- "top_p": 1.0,
75
- "n": 1
76
- }
77
- )
78
-
79
- # Check for successful response
80
  if response.status_code == 200:
81
- result = response.json().get("choices", [])[0].get("text", "").strip()
82
- # Display the response from the Gemini model
83
- st.subheader("Estimated Solar System Details:")
84
- st.write(result)
 
 
85
  else:
86
- st.error(f"Error: {response.status_code}. Unable to get response from Gemini API.")
87
 
88
  else:
89
  st.error("Sorry, the location entered does not match any available data.")
 
2
  import pandas as pd
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
+ import os
 
 
 
6
  import requests
7
+ from dotenv import load_dotenv
8
 
9
+ # Load environment variables from .env file
10
+ load_dotenv()
11
+
12
+ # Get the Gemini API key from the environment
13
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
14
 
15
+ # Gemini API URL
16
+ GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
17
 
18
+ # Load the solar data CSV file
19
+ df = pd.read_csv('https://huggingface.co/spaces/MLDeveloper/AI_based_Solar_Project_Estimation_Tool/resolve/main/solar_data_india_2024.csv')
20
 
21
  # Streamlit UI
22
  st.set_page_config(page_title="AI-based Solar Project Estimation Tool", layout="centered")
23
  st.title("AI-based Solar Project Estimation Tool")
24
 
 
25
  st.write("### Enter Your Details Below:")
26
 
27
  with st.form("solar_form"):
 
28
  state_options = df['State'].dropna().unique()
29
 
 
30
  location = st.selectbox("Select your State", options=sorted(state_options))
31
  roof_size = st.number_input("Enter your roof size (in sq meters)", min_value=1)
32
  electricity_bill = st.number_input("Enter your monthly electricity bill (₹)", min_value=0)
 
34
  submitted = st.form_submit_button("Get Estimate")
35
 
36
  if submitted and location and roof_size > 0 and electricity_bill >= 0:
37
+ state_data = df[df['State'].str.contains(location, case=False)].iloc[0]
 
38
 
39
  if state_data is not None:
40
  ghi = state_data['Avg_GHI (kWh/m²/day)']
41
  solar_cost_per_kw = state_data['Solar_Cost_per_kW (₹)']
42
 
43
+ prompt_text = f"""
 
44
  Estimate the solar system for the location '{location}' based on the following details:
45
+ - Roof size: {roof_size} sq meters
46
+ - Monthly electricity bill: ₹{electricity_bill}
47
+ - Average GHI (solar radiation) for {location}: {ghi} kWh/m²/day
48
+ - Solar system cost per kW in {location}: ₹{solar_cost_per_kw}
49
+
50
  Provide:
51
  1. Estimated solar system size in kW
52
  2. Estimated daily solar output in kWh
 
55
  5. Payback period in years
56
  """
57
 
58
+ # Define the headers for the API request
59
+ headers = {
60
+ "Authorization": f"Bearer {GEMINI_API_KEY}",
61
+ "Content-Type": "application/json"
62
+ }
63
+
64
+ # Define the request payload
65
+ payload = {
66
+ "model": "gemini-1.5-flash",
67
+ "messages": [{"role": "system", "content": "You are a helpful assistant."},
68
+ {"role": "user", "content": prompt_text}],
69
+ "temperature": 0.7
70
+ }
71
+
72
+ # Make the API request
73
+ response = requests.post(GEMINI_API_URL, json=payload, headers=headers)
74
+
75
+ # Check the response from the API
76
  if response.status_code == 200:
77
+ result = response.json()
78
+ generated_content = result['choices'][0]['message']['content']
79
+
80
+ # Display the generated content (solar estimates)
81
+ st.subheader("Solar Project Estimate")
82
+ st.write(generated_content)
83
  else:
84
+ st.error(f"Error: {response.status_code} - {response.text}")
85
 
86
  else:
87
  st.error("Sorry, the location entered does not match any available data.")