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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
from groq import Groq
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import plotly.express as px
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
|
| 15 |
+
|
| 16 |
+
# Custom CSS to make the page full-windowed
|
| 17 |
+
st.markdown(
|
| 18 |
+
"""
|
| 19 |
+
<style>
|
| 20 |
+
.stApp {
|
| 21 |
+
max-width: 100%;
|
| 22 |
+
padding: 0;
|
| 23 |
+
}
|
| 24 |
+
.stButton>button {
|
| 25 |
+
width: 100%;
|
| 26 |
+
}
|
| 27 |
+
</style>
|
| 28 |
+
""",
|
| 29 |
+
unsafe_allow_html=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Helper functions for calculations (from previous_app.py)
|
| 33 |
+
def calculate_h2_production(method, water_quantity, energy_input, current_density, voltage):
|
| 34 |
+
"""Calculate hydrogen production based on input parameters"""
|
| 35 |
+
method_efficiencies = {
|
| 36 |
+
"Alkaline Electrolysis": 0.65,
|
| 37 |
+
"PEM Electrolysis": 0.75,
|
| 38 |
+
"SOEC": 0.85
|
| 39 |
+
}
|
| 40 |
+
faraday_constant = 96485 # C/mol
|
| 41 |
+
molar_mass_h2 = 2.02 # g/mol
|
| 42 |
+
efficiency = method_efficiencies[method]
|
| 43 |
+
surface_area = water_quantity * 0.1
|
| 44 |
+
current = current_density * surface_area
|
| 45 |
+
time_hours = energy_input / (voltage * current)
|
| 46 |
+
moles_h2 = (current * time_hours * 3600 * efficiency) / (2 * faraday_constant)
|
| 47 |
+
mass_h2 = moles_h2 * molar_mass_h2
|
| 48 |
+
volume_h2 = moles_h2 * 22.4
|
| 49 |
+
return {
|
| 50 |
+
"production_rate_g_per_hour": mass_h2 / time_hours,
|
| 51 |
+
"total_production_g": mass_h2,
|
| 52 |
+
"total_production_L": volume_h2,
|
| 53 |
+
"efficiency": efficiency,
|
| 54 |
+
"operation_time_hours": time_hours
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
def calculate_cost(method, water_cost, water_purification_cost, energy_source, energy_input, h2_production):
|
| 58 |
+
"""Calculate the cost of hydrogen production"""
|
| 59 |
+
energy_costs = {
|
| 60 |
+
"Grid Electricity": 0.12,
|
| 61 |
+
"Solar": 0.08,
|
| 62 |
+
"Wind": 0.06,
|
| 63 |
+
"Nuclear": 0.10,
|
| 64 |
+
"Hydroelectric": 0.07
|
| 65 |
+
}
|
| 66 |
+
operational_costs = {
|
| 67 |
+
"Alkaline Electrolysis": 1.2,
|
| 68 |
+
"PEM Electrolysis": 1.5,
|
| 69 |
+
"SOEC": 1.8
|
| 70 |
+
}
|
| 71 |
+
total_water_cost = water_cost * (h2_production["total_production_g"] / 1000)
|
| 72 |
+
total_purification_cost = water_purification_cost * (h2_production["total_production_g"] / 1000)
|
| 73 |
+
energy_cost_rate = energy_costs[energy_source]
|
| 74 |
+
total_energy_cost = energy_cost_rate * energy_input
|
| 75 |
+
operational_cost_rate = operational_costs[method]
|
| 76 |
+
total_operational_cost = operational_cost_rate * (h2_production["total_production_g"] / 1000)
|
| 77 |
+
total_cost = total_water_cost + total_purification_cost + total_energy_cost + total_operational_cost
|
| 78 |
+
cost_per_kg = total_cost / (h2_production["total_production_g"] / 1000) if h2_production["total_production_g"] > 0 else 0
|
| 79 |
+
return {
|
| 80 |
+
"water_cost": total_water_cost,
|
| 81 |
+
"purification_cost": total_purification_cost,
|
| 82 |
+
"energy_cost": total_energy_cost,
|
| 83 |
+
"operational_cost": total_operational_cost,
|
| 84 |
+
"total_cost": total_cost,
|
| 85 |
+
"cost_per_kg": cost_per_kg
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
def call_groq_api(user_inputs, production_data, cost_data):
|
| 89 |
+
"""Call Groq API with Llama 3 to analyze production parameters and provide recommendations"""
|
| 90 |
+
try:
|
| 91 |
+
client = Groq(api_key=os.environ.get("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"))
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return {"error": f"Failed to initialize Groq client: {str(e)}"}
|
| 94 |
+
|
| 95 |
+
prompt = f"""
|
| 96 |
+
As a hydrogen production expert, analyze the following electrolysis parameters and provide recommendations for optimization:
|
| 97 |
+
|
| 98 |
+
Input Parameters:
|
| 99 |
+
- Water Source: {user_inputs['water_source']}
|
| 100 |
+
- Production Method: {user_inputs['production_method']}
|
| 101 |
+
- Energy Source: {user_inputs['energy_source']}
|
| 102 |
+
- Current Density: {user_inputs['current_density']} A/cm²
|
| 103 |
+
- Voltage: {user_inputs['voltage']} V
|
| 104 |
+
- Membrane Material: {user_inputs['membrane']}
|
| 105 |
+
- Electrode Materials: {user_inputs['electrodes']}
|
| 106 |
+
|
| 107 |
+
Production Results:
|
| 108 |
+
- Production Rate: {production_data['production_rate_g_per_hour']:.2f} g/hour
|
| 109 |
+
- Total Production: {production_data['total_production_g']:.2f} g
|
| 110 |
+
- Efficiency: {production_data['efficiency'] * 100:.1f}%
|
| 111 |
+
- Operation Time: {production_data['operation_time_hours']:.2f} hours
|
| 112 |
+
|
| 113 |
+
Cost Analysis:
|
| 114 |
+
- Water Cost: ${cost_data['water_cost']:.2f}
|
| 115 |
+
- Purification Cost: ${cost_data['purification_cost']:.2f}
|
| 116 |
+
- Energy Cost: ${cost_data['energy_cost']:.2f}
|
| 117 |
+
- Operational Cost: ${cost_data['operational_cost']:.2f}
|
| 118 |
+
- Total Cost: ${cost_data['total_cost']:.2f}
|
| 119 |
+
- Cost per kg H₂: ${cost_data['cost_per_kg']:.2f}
|
| 120 |
+
|
| 121 |
+
Please provide:
|
| 122 |
+
1. An efficiency assessment of the current setup
|
| 123 |
+
2. Three specific recommendations to improve efficiency
|
| 124 |
+
3. Three specific recommendations to reduce costs
|
| 125 |
+
4. An ideal parameter configuration based on the provided inputs
|
| 126 |
+
|
| 127 |
+
Format your response as a structured JSON with these fields:
|
| 128 |
+
{
|
| 129 |
+
"efficiency_assessment": "text analysis",
|
| 130 |
+
"efficiency_recommendations": ["recommendation1", "recommendation2", "recommendation3"],
|
| 131 |
+
"cost_recommendations": ["recommendation1", "recommendation2", "recommendation3"],
|
| 132 |
+
"ideal_parameters": {
|
| 133 |
+
"current_density": value,
|
| 134 |
+
"voltage": value,
|
| 135 |
+
"membrane": "recommendation",
|
| 136 |
+
"electrodes": "recommendation",
|
| 137 |
+
"energy_source": "recommendation"
|
| 138 |
+
},
|
| 139 |
+
"estimated_improvement": {
|
| 140 |
+
"efficiency_increase": "percentage",
|
| 141 |
+
"cost_reduction": "percentage"
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
"""
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
chat_completion = client.chat.completions.create(
|
| 148 |
+
messages=[{"role": "user", "content": prompt}],
|
| 149 |
+
model="llama-3.3-70b-versatile",
|
| 150 |
+
temperature=0.5,
|
| 151 |
+
max_tokens=1024,
|
| 152 |
+
response_format={"type": "json_object"}
|
| 153 |
+
)
|
| 154 |
+
response_content = chat_completion.choices[0].message.content
|
| 155 |
+
return json.loads(response_content)
|
| 156 |
+
except Exception as e:
|
| 157 |
+
return {"error": f"Error calling Groq API: {str(e)}"}
|
| 158 |
+
|
| 159 |
+
# Function to display the app interface
|
| 160 |
+
def show_app_interface():
|
| 161 |
+
"""Function to display the app interface"""
|
| 162 |
+
st.title("Hydrogen Production Analysis & Optimization")
|
| 163 |
+
st.write("This is the app interface.")
|
| 164 |
+
|
| 165 |
+
# Function to display the AI chatbot
|
| 166 |
+
def show_chatbot():
|
| 167 |
+
"""Function to display the AI chatbot"""
|
| 168 |
+
st.title("AI Chatbot")
|
| 169 |
+
user_input = st.text_input("Ask me anything about hydrogen production:")
|
| 170 |
+
if user_input:
|
| 171 |
+
response = call_groq_api({}, {}, {})
|
| 172 |
+
st.write(response)
|
| 173 |
+
|
| 174 |
+
# Function to display the landing page
|
| 175 |
+
def show_landing_page():
|
| 176 |
+
"""Function to display the landing page"""
|
| 177 |
+
st.markdown(
|
| 178 |
+
"""
|
| 179 |
+
<div style="text-align: center; padding: 50px;">
|
| 180 |
+
<h1>Ready to Transform Your Hydrogen Projects?</h1>
|
| 181 |
+
<p>Join the hydrogen revolution with AI-powered techno-economic analysis that gives you the competitive edge.</p>
|
| 182 |
+
<button style="padding: 10px 20px; margin: 10px; background-color: #4CAF50; color: white; border: none; border-radius: 5px;" onclick="window.location.href='?page=app'">Request a Demo</button>
|
| 183 |
+
<button style="padding: 10px 20px; margin: 10px; background-color: #1E88E5; color: white; border: none; border-radius: 5px;" onclick="window.location.href='?page=chatbot'">Learn More</button>
|
| 184 |
+
</div>
|
| 185 |
+
""",
|
| 186 |
+
unsafe_allow_html=True
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
# Main function to handle navigation
|
| 190 |
+
def main():
|
| 191 |
+
"""Main function to handle navigation between pages"""
|
| 192 |
+
query_params = st.experimental_get_query_params()
|
| 193 |
+
page = query_params.get("page", ["landing"])[0]
|
| 194 |
+
|
| 195 |
+
if page == "app":
|
| 196 |
+
show_app_interface()
|
| 197 |
+
elif page == "chatbot":
|
| 198 |
+
show_chatbot()
|
| 199 |
+
else:
|
| 200 |
+
show_landing_page()
|
| 201 |
+
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
main()
|