Commit
ยท
44dd835
1
Parent(s):
28da4cd
app refactored
Browse files
app.py
CHANGED
@@ -164,116 +164,116 @@
|
|
164 |
# *Educational Purpose Only* | ๐ฑ **SDG Guardians AI - 2024** | *For a greener East Africa*
|
165 |
# """)
|
166 |
|
167 |
-
import streamlit as st
|
168 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
|
169 |
|
170 |
-
# Page configuration
|
171 |
-
st.set_page_config(page_title="Climate Chatbot - Uganda", layout="wide")
|
172 |
|
173 |
-
# Custom CSS for shadow effect
|
174 |
-
st.markdown(
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
)
|
195 |
-
|
196 |
-
# Load model
|
197 |
-
@st.cache_resource
|
198 |
-
def load_climate_bert():
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
qa_pipeline = load_climate_bert()
|
204 |
-
|
205 |
-
# Function to check if question is climate-related
|
206 |
-
def is_climate_related(question):
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
# Function to check if Uganda is mentioned
|
212 |
-
def is_uganda_related(question):
|
213 |
-
|
214 |
-
|
215 |
-
# Function to generate response
|
216 |
-
def generate_response(user_question, context):
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
|
222 |
-
|
223 |
-
|
224 |
|
225 |
-
# Session state for chat history
|
226 |
-
if "history" not in st.session_state:
|
227 |
-
|
228 |
|
229 |
-
# Sidebar - Chat History & Clear Button
|
230 |
-
with st.sidebar:
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
-
#
|
242 |
-
st.
|
243 |
-
|
244 |
-
|
245 |
-
#
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
- **What are sustainable farming methods?**
|
250 |
-
- **How can I reduce my energy use?**
|
251 |
-
- **What are the risks of deforestation?**
|
252 |
-
- **Why is tree planting important?**
|
253 |
-
- **How can youth take action?**
|
254 |
-
""")
|
255 |
-
|
256 |
-
# Chat container with avatars
|
257 |
-
chat_container = st.container()
|
258 |
-
with chat_container:
|
259 |
-
for question, answer in st.session_state.history:
|
260 |
-
with st.chat_message("user"):
|
261 |
-
st.write(question)
|
262 |
-
with st.chat_message("assistant"):
|
263 |
-
st.write(answer)
|
264 |
-
|
265 |
-
# User input field with shadow effect
|
266 |
-
user_input = st.chat_input("Ask about climate change in Uganda...")
|
267 |
-
if user_input:
|
268 |
-
context = """
|
269 |
-
Climate change is affecting Uganda and East Africa in various ways, including unpredictable rainfall,
|
270 |
-
rising temperatures, and prolonged droughts. Sustainable farming, afforestation, and renewable energy
|
271 |
-
adoption are key solutions to mitigate these effects.
|
272 |
-
""" # Placeholder context
|
273 |
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
|
278 |
|
279 |
# seems to be overcrowding the page, so we can remove it for now.
|
@@ -303,3 +303,64 @@ if user_input:
|
|
303 |
# """,
|
304 |
# unsafe_allow_html=True
|
305 |
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
# *Educational Purpose Only* | ๐ฑ **SDG Guardians AI - 2024** | *For a greener East Africa*
|
165 |
# """)
|
166 |
|
167 |
+
# import streamlit as st
|
168 |
+
# from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
|
169 |
|
170 |
+
# # Page configuration
|
171 |
+
# st.set_page_config(page_title="Climate Chatbot - Uganda", layout="wide")
|
172 |
|
173 |
+
# # Custom CSS for shadow effect
|
174 |
+
# st.markdown(
|
175 |
+
# """
|
176 |
+
# <style>
|
177 |
+
# .stChatInput {
|
178 |
+
# box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.4); /* Strong shadow */
|
179 |
+
# border-radius: 10px;
|
180 |
+
# padding: 12px;
|
181 |
+
# background: white;
|
182 |
+
# }
|
183 |
+
# .stChatInput::before {
|
184 |
+
# content: "";
|
185 |
+
# position: absolute;
|
186 |
+
# width: 100%;
|
187 |
+
# height: 15px;
|
188 |
+
# left: 0;
|
189 |
+
# background: linear-gradient(to top, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0)); /* Fading effect */
|
190 |
+
# }
|
191 |
+
# </style>
|
192 |
+
# """,
|
193 |
+
# unsafe_allow_html=True
|
194 |
+
# )
|
195 |
+
|
196 |
+
# # Load model
|
197 |
+
# @st.cache_resource
|
198 |
+
# def load_climate_bert():
|
199 |
+
# tokenizer = AutoTokenizer.from_pretrained("NinaErlacher/ClimateBERTqa")
|
200 |
+
# model = AutoModelForQuestionAnswering.from_pretrained("NinaErlacher/ClimateBERTqa")
|
201 |
+
# return pipeline("question-answering", model=model, tokenizer=tokenizer)
|
202 |
+
|
203 |
+
# qa_pipeline = load_climate_bert()
|
204 |
+
|
205 |
+
# # Function to check if question is climate-related
|
206 |
+
# def is_climate_related(question):
|
207 |
+
# climate_keywords = ["climate", "global warming", "deforestation", "carbon", "sustainability",
|
208 |
+
# "renewable", "pollution", "green energy", "climate action", "afforestation"]
|
209 |
+
# return any(keyword in question.lower() for keyword in climate_keywords)
|
210 |
+
|
211 |
+
# # Function to check if Uganda is mentioned
|
212 |
+
# def is_uganda_related(question):
|
213 |
+
# return "uganda" in question.lower() or "east africa" in question.lower()
|
214 |
+
|
215 |
+
# # Function to generate response
|
216 |
+
# def generate_response(user_question, context):
|
217 |
+
# if not is_climate_related(user_question):
|
218 |
+
# return "I'm here to discuss climate change. Try asking about Uganda's climate, sustainability, or environmental issues."
|
219 |
+
# if not is_uganda_related(user_question):
|
220 |
+
# return "This chatbot focuses on climate change in Uganda. Try asking about Uganda's environmental challenges."
|
221 |
|
222 |
+
# result = qa_pipeline(question=user_question, context=context)
|
223 |
+
# return result['answer']
|
224 |
|
225 |
+
# # Session state for chat history
|
226 |
+
# if "history" not in st.session_state:
|
227 |
+
# st.session_state.history = []
|
228 |
|
229 |
+
# # Sidebar - Chat History & Clear Button
|
230 |
+
# with st.sidebar:
|
231 |
+
# st.title("Chat History")
|
232 |
+
# for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
|
233 |
+
# with st.expander(f"๐ฌ {question}"):
|
234 |
+
# st.write(f"**Chatbot:** {answer}")
|
235 |
+
# st.markdown("---")
|
236 |
+
# if st.button("๐๏ธ Clear Chat History"):
|
237 |
+
# st.session_state.history = []
|
238 |
+
# st.rerun()
|
239 |
+
# st.info("๐ฑ *Ask about climate change in Uganda.*")
|
240 |
+
|
241 |
+
# # Main UI
|
242 |
+
# st.title("Climate Change Chatbot")
|
243 |
+
# st.subheader("Explore climate action and sustainability in Uganda")
|
244 |
+
|
245 |
+
# # Sample questions section
|
246 |
+
# with st.expander("Need ideas? (Click to expand)"):
|
247 |
+
# st.markdown("""
|
248 |
+
# - **How is Uganda affected by climate change?**
|
249 |
+
# - **What are sustainable farming methods?**
|
250 |
+
# - **How can I reduce my energy use?**
|
251 |
+
# - **What are the risks of deforestation?**
|
252 |
+
# - **Why is tree planting important?**
|
253 |
+
# - **How can youth take action?**
|
254 |
+
# """)
|
255 |
+
|
256 |
+
# # Chat container with avatars
|
257 |
+
# chat_container = st.container()
|
258 |
+
# with chat_container:
|
259 |
+
# for question, answer in st.session_state.history:
|
260 |
+
# with st.chat_message("user"):
|
261 |
+
# st.write(question)
|
262 |
+
# with st.chat_message("assistant"):
|
263 |
+
# st.write(answer)
|
264 |
|
265 |
+
# # User input field with shadow effect
|
266 |
+
# user_input = st.chat_input("Ask about climate change in Uganda...")
|
267 |
+
# if user_input:
|
268 |
+
# context = """
|
269 |
+
# Climate change is affecting Uganda and East Africa in various ways, including unpredictable rainfall,
|
270 |
+
# rising temperatures, and prolonged droughts. Sustainable farming, afforestation, and renewable energy
|
271 |
+
# adoption are key solutions to mitigate these effects.
|
272 |
+
# """ # Placeholder context
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
+
# response = generate_response(user_input, context)
|
275 |
+
# st.session_state.history.append((user_input, response))
|
276 |
+
# st.rerun()
|
277 |
|
278 |
|
279 |
# seems to be overcrowding the page, so we can remove it for now.
|
|
|
303 |
# """,
|
304 |
# unsafe_allow_html=True
|
305 |
# )
|
306 |
+
|
307 |
+
import streamlit as st
|
308 |
+
import torch
|
309 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
310 |
+
|
311 |
+
# Page configuration
|
312 |
+
st.set_page_config(page_title="ClimateGPT Chatbot - Uganda", layout="wide")
|
313 |
+
|
314 |
+
# Load ClimateGPT model
|
315 |
+
@st.cache_resource
|
316 |
+
def load_climate_gpt():
|
317 |
+
model_name = "eci-io/climategpt-7b"
|
318 |
+
|
319 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
320 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
|
321 |
+
|
322 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=512)
|
323 |
+
|
324 |
+
climate_gpt_pipeline = load_climate_gpt()
|
325 |
+
|
326 |
+
# Function to generate response using ClimateGPT
|
327 |
+
def generate_response(user_question):
|
328 |
+
prompt = f"""
|
329 |
+
<|im_start|>system
|
330 |
+
You are ClimateGPT, an expert in climate change. Provide accurate and fact-based responses about Ugandaโs climate issues.<|im_end|>
|
331 |
+
<|im_start|>user
|
332 |
+
{user_question}<|im_end|>
|
333 |
+
<|im_start|>assistant
|
334 |
+
"""
|
335 |
+
|
336 |
+
response = climate_gpt_pipeline(prompt)[0]["generated_text"]
|
337 |
+
|
338 |
+
# Extract only the assistant's response
|
339 |
+
response = response.split("<|im_start|>assistant")[-1].strip()
|
340 |
+
return response
|
341 |
+
|
342 |
+
# Chat history
|
343 |
+
if "history" not in st.session_state:
|
344 |
+
st.session_state.history = []
|
345 |
+
|
346 |
+
# Sidebar - Chat History & Clear Button
|
347 |
+
with st.sidebar:
|
348 |
+
st.title("Chat History")
|
349 |
+
for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
|
350 |
+
with st.expander(f"๐ฌ {question}"):
|
351 |
+
st.write(f"**Chatbot:** {answer}")
|
352 |
+
|
353 |
+
if st.button("๐๏ธ Clear Chat History"):
|
354 |
+
st.session_state.history = []
|
355 |
+
st.rerun()
|
356 |
+
|
357 |
+
# Main UI
|
358 |
+
st.title("๐ ClimateGPT - Uganda")
|
359 |
+
st.subheader("Ask about climate change, sustainability, and environmental action in Uganda!")
|
360 |
+
|
361 |
+
# User input field
|
362 |
+
user_input = st.chat_input("Ask me anything about Uganda's climate...")
|
363 |
+
if user_input:
|
364 |
+
response = generate_response(user_input)
|
365 |
+
st.session_state.history.append((user_input, response))
|
366 |
+
st.rerun()
|