File size: 12,453 Bytes
28da4cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44dd835
 
28da4cd
44dd835
 
28da4cd
44dd835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28da4cd
44dd835
 
28da4cd
44dd835
 
 
28da4cd
44dd835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28da4cd
44dd835
 
 
 
 
 
 
 
28da4cd
44dd835
 
 
28da4cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44dd835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Author: Fred Okorio
# Date: 2024-01-01
# Description: A Streamlit app for a Climate Change Awareness Chatbot using the ClimateGPT-7B model.


# Have to SWITCH to this more expressive model before the deadline.


# # necessary libraries
# import streamlit as st
# import accelerate
# from transformers import AutoTokenizer, AutoModelForCausalLM
# import torch

# # page configuration
# st.set_page_config(page_title="Climate Change Awareness Chatbot", layout="wide")

# # ClimateGPT-7B model and tokenizer
# @st.cache_resource
# def load_climategpt():
#     tokenizer = AutoTokenizer.from_pretrained("eci-io/climategpt-7b")
#     model = AutoModelForCausalLM.from_pretrained("eci-io/climategpt-7b", device_map="auto")
#     return tokenizer, model

# tokenizer, model = load_climategpt()

# # generate responses
# def generate_response(user_input):
#     prompt = f"""
# <|im_start|>system
# You are ClimateGPT, a large language model trained to provide information on climate change.<|im_end|>
# <|im_start|>user
# {user_input}<|im_end|>
# <|im_start|>assistant
# """
#     inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
#     outputs = model.generate(**inputs, max_new_tokens=200)
#     response = tokenizer.decode(outputs[0], skip_special_tokens=True)
#     return response.split("<|im_end|>")[-1].strip()

# # initialize session state for chat history
# if "history" not in st.session_state:
#     st.session_state.history = []

# # sidebar for chat history
# with st.sidebar:
#     st.title("Chat History")
#     for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
#         with st.expander(f"πŸ’¬ {question}"):
#             st.write(f"**Chatbot:** {answer}")

#     st.markdown("---")
#     st.info("🌱 *Ask me anything about climate change, sustainability, or eco-friendly living.*")

# # main chat interface
# st.title("Climate Change Awareness Chatbot")
# st.subheader("Get answers, tips, and climate change facts for Uganda & East Africa")

# # Display chat history
# for question, answer in st.session_state.history:
#     st.markdown(f"**You:** {question}")
#     st.success(f"**Chatbot:** {answer}")
#     st.markdown("---")

# # User input
# user_input = st.text_input("πŸ’¬ Type your message and press Enter", key="text_input")

# if user_input:
#     response = generate_response(user_input)

#     # Append conversation to history
#     st.session_state.history.append((user_input, response))

#     # Clear input field after processing
#     st.session_state.text_input = ""

#     # Rerun the app to display the updated chat history
#     st.experimental_rerun()

# # Clear chat history button
# if st.button("Clear Chat History"):
#     st.session_state.history = []
#     st.experimental_rerun()

# # Footer
# st.markdown("""
# ---
# *Educational Purpose Only* | 🌱 **SDG Guardians AI - 2024** | *For a greener East Africa*
# """)

# import streamlit as st
# from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering

# # page configuration
# st.set_page_config(page_title="Climate Chatbot - Uganda & East Africa", layout="wide")

# # model loading...
# @st.cache_resource
# def load_climate_bert():
#     tokenizer = AutoTokenizer.from_pretrained("NinaErlacher/ClimateBERTqa")
#     model = AutoModelForQuestionAnswering.from_pretrained("NinaErlacher/ClimateBERTqa")
#     qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
#     return qa_pipeline

# qa_pipeline = load_climate_bert()

# def generate_response(user_question, context):
#     result = qa_pipeline(question=user_question, context=context)
#     return result['answer']

# # Initialize session state variables
# if "history" not in st.session_state:
#     st.session_state.history = []

# # Sidebar for chat history
# with st.sidebar:
#     st.title("Chat History")
#     for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
#         with st.expander(f"πŸ’¬ {question}"):
#             st.write(f"**Chatbot:** {answer}")

#     st.markdown("---")
#     st.info("🌱 *Ask me anything about climate change, sustainability, or eco-friendly living.*")

# # main chat UI
# st.title("Climate Change Awareness Chatbot")
# st.subheader("Get answers, tips, and climate change facts for Uganda & East Africa")

# # chat display
# chat_container = st.container()
# with chat_container:
#     for question, answer in st.session_state.history:
#         st.markdown(f"**You:** {question}")
#         st.success(f"**Chatbot:** {answer}")
#         st.markdown("---")

# User input
# user_input = st.text_input("πŸ’¬ Type your message and press Enter", key="text_input")

# if user_input:  
#     context = """
#     Climate change is affecting Uganda and East Africa in various ways, including unpredictable rainfall patterns, 
#     increased temperatures, and prolonged droughts. Sustainable farming practices, afforestation, and renewable 
#     energy adoption are key solutions to mitigate these effects.
#     """  # Placeholder context

#     response = generate_response(user_input, context)

#     # append conversation to history
#    /' ' st.session_state.history.append((user_input, response))

#     # Clear stored input after processing
#     st.session_state.pop("text_input", None)  
#     st.rerun()

# # Clear chat history button
# if st.button("Clear Chat History"):
#     st.session_state.history = []
#     st.rerun()

# # footer
# st.markdown("""
# ---
#  *Educational Purpose Only* | 🌱 **SDG Guardians AI - 2024** |  *For a greener East Africa*
# """)

# import streamlit as st
# from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering

# # Page configuration
# st.set_page_config(page_title="Climate Chatbot - Uganda", layout="wide")

# # Custom CSS for shadow effect
# st.markdown(
#     """
#     <style>
#     .stChatInput {
#         box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.4);  /* Strong shadow */
#         border-radius: 10px;
#         padding: 12px;
#         background: white;
#     }
#     .stChatInput::before {
#         content: "";
#         position: absolute;
#         width: 100%;
#         height: 15px;
#         left: 0;
#         background: linear-gradient(to top, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0)); /* Fading effect */
#     }
#     </style>
#     """,
#     unsafe_allow_html=True
# )

# # Load model
# @st.cache_resource
# def load_climate_bert():
#     tokenizer = AutoTokenizer.from_pretrained("NinaErlacher/ClimateBERTqa")
#     model = AutoModelForQuestionAnswering.from_pretrained("NinaErlacher/ClimateBERTqa")
#     return pipeline("question-answering", model=model, tokenizer=tokenizer)

# qa_pipeline = load_climate_bert()

# # Function to check if question is climate-related
# def is_climate_related(question):
#     climate_keywords = ["climate", "global warming", "deforestation", "carbon", "sustainability", 
#                         "renewable", "pollution", "green energy", "climate action", "afforestation"]
#     return any(keyword in question.lower() for keyword in climate_keywords)

# # Function to check if Uganda is mentioned
# def is_uganda_related(question):
#     return "uganda" in question.lower() or "east africa" in question.lower()

# # Function to generate response
# def generate_response(user_question, context):
#     if not is_climate_related(user_question):
#         return "I'm here to discuss climate change. Try asking about Uganda's climate, sustainability, or environmental issues."
#     if not is_uganda_related(user_question):
#         return "This chatbot focuses on climate change in Uganda. Try asking about Uganda's environmental challenges."
    
#     result = qa_pipeline(question=user_question, context=context)
#     return result['answer']

# # Session state for chat history
# if "history" not in st.session_state:
#     st.session_state.history = []

# # Sidebar - Chat History & Clear Button
# with st.sidebar:
#     st.title("Chat History")
#     for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
#         with st.expander(f"πŸ’¬ {question}"):
#             st.write(f"**Chatbot:** {answer}")
#     st.markdown("---")
#     if st.button("πŸ—‘οΈ Clear Chat History"):
#         st.session_state.history = []
#         st.rerun()
#     st.info("🌱 *Ask about climate change in Uganda.*")

# # Main UI
# st.title("Climate Change Chatbot")
# st.subheader("Explore climate action and sustainability in Uganda")

# # Sample questions section
# with st.expander("Need ideas? (Click to expand)"):
#     st.markdown("""
#     - **How is Uganda affected by climate change?**
#     - **What are sustainable farming methods?**
#     - **How can I reduce my energy use?**
#     - **What are the risks of deforestation?**
#     - **Why is tree planting important?**
#     - **How can youth take action?**
#     """)

# # Chat container with avatars
# chat_container = st.container()
# with chat_container:
#     for question, answer in st.session_state.history:
#         with st.chat_message("user"):
#             st.write(question)
#         with st.chat_message("assistant"):
#             st.write(answer)

# # User input field with shadow effect
# user_input = st.chat_input("Ask about climate change in Uganda...")
# if user_input:
#     context = """
#     Climate change is affecting Uganda and East Africa in various ways, including unpredictable rainfall, 
#     rising temperatures, and prolonged droughts. Sustainable farming, afforestation, and renewable energy 
#     adoption are key solutions to mitigate these effects.
#     """  # Placeholder context
    
#     response = generate_response(user_input, context)
#     st.session_state.history.append((user_input, response))
#     st.rerun()


# seems to be overcrowding the page, so we can remove it for now.

# # footer fixed at the bottom
# st.markdown(
#     """
#     <style>
#     .footer {
#         position: fixed;
#         bottom: 0;
#         left: 100px;
#         font-size: 14px;
#         font-weight: 900;
#         width: 100%;
#         background-color: white;
#         text-align: center;
#         padding: 10px;
#         box-shadow: 0px -2px 5px rgba(0, 0, 0, 0.1);
#         z-index: 999;
#     }
#     </style>
#     <div class="footer">
#     ---  
#     *Educational Purpose Only* | 🌱 **SDG Guardians AI - 2024** | *For a greener East Africa*
#     </div>
#     """,
#     unsafe_allow_html=True
# )

import streamlit as st
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

# Page configuration
st.set_page_config(page_title="ClimateGPT Chatbot - Uganda", layout="wide")

# Load ClimateGPT model
@st.cache_resource
def load_climate_gpt():
    model_name = "eci-io/climategpt-7b"
    
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")

    return pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=512)

climate_gpt_pipeline = load_climate_gpt()

# Function to generate response using ClimateGPT
def generate_response(user_question):
    prompt = f"""
    <|im_start|>system
    You are ClimateGPT, an expert in climate change. Provide accurate and fact-based responses about Uganda’s climate issues.<|im_end|>
    <|im_start|>user
    {user_question}<|im_end|>
    <|im_start|>assistant
    """
    
    response = climate_gpt_pipeline(prompt)[0]["generated_text"]
    
    # Extract only the assistant's response
    response = response.split("<|im_start|>assistant")[-1].strip()
    return response

# Chat history
if "history" not in st.session_state:
    st.session_state.history = []

# Sidebar - Chat History & Clear Button
with st.sidebar:
    st.title("Chat History")
    for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
        with st.expander(f"πŸ’¬ {question}"):
            st.write(f"**Chatbot:** {answer}")
    
    if st.button("πŸ—‘οΈ Clear Chat History"):
        st.session_state.history = []
        st.rerun()

# Main UI
st.title("🌍 ClimateGPT - Uganda")
st.subheader("Ask about climate change, sustainability, and environmental action in Uganda!")

# User input field
user_input = st.chat_input("Ask me anything about Uganda's climate...")
if user_input:
    response = generate_response(user_input)
    st.session_state.history.append((user_input, response))
    st.rerun()