Commit
Β·
28da4cd
1
Parent(s):
d830964
app complete
Browse files- .gitignore +174 -0
- README.md +74 -13
- app.py +305 -0
- model_utils.py +0 -0
- requirements.txt +0 -0
.gitignore
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# UV
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
#uv.lock
|
102 |
+
|
103 |
+
# poetry
|
104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
106 |
+
# commonly ignored for libraries.
|
107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
108 |
+
#poetry.lock
|
109 |
+
|
110 |
+
# pdm
|
111 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
112 |
+
#pdm.lock
|
113 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
114 |
+
# in version control.
|
115 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
116 |
+
.pdm.toml
|
117 |
+
.pdm-python
|
118 |
+
.pdm-build/
|
119 |
+
|
120 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
121 |
+
__pypackages__/
|
122 |
+
|
123 |
+
# Celery stuff
|
124 |
+
celerybeat-schedule
|
125 |
+
celerybeat.pid
|
126 |
+
|
127 |
+
# SageMath parsed files
|
128 |
+
*.sage.py
|
129 |
+
|
130 |
+
# Environments
|
131 |
+
.env
|
132 |
+
.venv
|
133 |
+
env/
|
134 |
+
venv/
|
135 |
+
ENV/
|
136 |
+
env.bak/
|
137 |
+
venv.bak/
|
138 |
+
|
139 |
+
# Spyder project settings
|
140 |
+
.spyderproject
|
141 |
+
.spyproject
|
142 |
+
|
143 |
+
# Rope project settings
|
144 |
+
.ropeproject
|
145 |
+
|
146 |
+
# mkdocs documentation
|
147 |
+
/site
|
148 |
+
|
149 |
+
# mypy
|
150 |
+
.mypy_cache/
|
151 |
+
.dmypy.json
|
152 |
+
dmypy.json
|
153 |
+
|
154 |
+
# Pyre type checker
|
155 |
+
.pyre/
|
156 |
+
|
157 |
+
# pytype static type analyzer
|
158 |
+
.pytype/
|
159 |
+
|
160 |
+
# Cython debug symbols
|
161 |
+
cython_debug/
|
162 |
+
|
163 |
+
# PyCharm
|
164 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
165 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
166 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
167 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
168 |
+
#.idea/
|
169 |
+
|
170 |
+
# Ruff stuff:
|
171 |
+
.ruff_cache/
|
172 |
+
|
173 |
+
# PyPI configuration file
|
174 |
+
.pypirc
|
README.md
CHANGED
@@ -1,13 +1,74 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Climate Change Awareness Chatbot
|
2 |
+
|
3 |
+
## Overview
|
4 |
+
The app is now live! Try playing with it here: [QuantumCoders-TheSDGGuardiansAI](https://quantumcoders-thesdgguardiansai.streamlit.app/).
|
5 |
+
|
6 |
+
The Climate Change Awareness Chatbot is an AI-powered chatbot designed to educate users in Uganda about climate change, sustainability, and eco-friendly practices. It provides factual answers to questions on climate change impacts, sustainable farming, afforestation, renewable energy, and more.
|
7 |
+
|
8 |
+
## Features
|
9 |
+
|
10 |
+
- **Interactive Chat**: Users can ask climate-related questions and receive AI-generated responses.
|
11 |
+
- **Localized Context**: Focuses on climate issues specific to Uganda.
|
12 |
+
- **Chat History**: Keeps track of past conversations in the sidebar.
|
13 |
+
- **Avatars**: Displays user and chatbot messages with appropriate icons.
|
14 |
+
- **Sample Questions**: Provides predefined questions to guide users.
|
15 |
+
- **Clear Chat History**: Users can reset conversations from the sidebar.
|
16 |
+
|
17 |
+
|
18 |
+
## Technologies Used
|
19 |
+
|
20 |
+
- **Python**
|
21 |
+
- **Streamlit** (for UI and chat interaction)
|
22 |
+
- **Hugging Face Transformers** (for NLP model)
|
23 |
+
- **ClimateBERT** (for climate-related question answering)
|
24 |
+
|
25 |
+
## Installation
|
26 |
+
|
27 |
+
Clone the repository:
|
28 |
+
|
29 |
+
```sh
|
30 |
+
git clone https://github.com/codingxperience/QuantumCoders-TheSDGGuardiansAI.git
|
31 |
+
cd QuantumCoders-TheSDGGuardiansAI
|
32 |
+
```
|
33 |
+
|
34 |
+
Install dependencies:
|
35 |
+
|
36 |
+
```sh
|
37 |
+
pip install -r requirements.txt
|
38 |
+
```
|
39 |
+
|
40 |
+
Run the application:
|
41 |
+
|
42 |
+
```sh
|
43 |
+
streamlit run app.py
|
44 |
+
```
|
45 |
+
|
46 |
+
## Usage
|
47 |
+
|
48 |
+
1. Type a message in the input box and press **Enter**.
|
49 |
+
2. View responses in the chat window.
|
50 |
+
3. Click on **Sample Questions** to see common queries.
|
51 |
+
4. Use the **Clear Chat History** button to reset the conversation.
|
52 |
+
|
53 |
+
## Future Enhancements
|
54 |
+
|
55 |
+
- **Expanding coverage to include other African regions** in future versions.
|
56 |
+
|
57 |
+
- **Retraining the model with Ugandan-specific climate data** to improve relevance and accuracy for local environmental challenges.
|
58 |
+
|
59 |
+
- **Integration with real-time climate data APIs**.
|
60 |
+
- **Visualization of climate trends and statistics**.
|
61 |
+
- **Multilingual support in English, Luganda, and Swahili**.
|
62 |
+
- **Offline and USSD capabilities for users in low or no-internet areas**.
|
63 |
+
- **Support for voice, image, and video interactions**.
|
64 |
+
- **Integration with social media platforms**.
|
65 |
+
|
66 |
+
## Contributing
|
67 |
+
|
68 |
+
Pull requests are welcome! If you'd like to contribute, please fork the repository and create a new branch for your changes.
|
69 |
+
|
70 |
+
## License
|
71 |
+
|
72 |
+
This project is for educational purposes and is open for improvement and collaboration.
|
73 |
+
|
74 |
+
Developed as part of the **QuantumCoders-TheSDGGuardiansAI - 2024** initiative for a greener Uganda.
|
app.py
ADDED
@@ -0,0 +1,305 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Author: Fred Okorio
|
2 |
+
# Date: 2024-01-01
|
3 |
+
# Description: A Streamlit app for a Climate Change Awareness Chatbot using the ClimateGPT-7B model.
|
4 |
+
|
5 |
+
|
6 |
+
# Have to SWITCH to this more expressive model before the deadline.
|
7 |
+
|
8 |
+
|
9 |
+
# # necessary libraries
|
10 |
+
# import streamlit as st
|
11 |
+
# import accelerate
|
12 |
+
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
13 |
+
# import torch
|
14 |
+
|
15 |
+
# # page configuration
|
16 |
+
# st.set_page_config(page_title="Climate Change Awareness Chatbot", layout="wide")
|
17 |
+
|
18 |
+
# # ClimateGPT-7B model and tokenizer
|
19 |
+
# @st.cache_resource
|
20 |
+
# def load_climategpt():
|
21 |
+
# tokenizer = AutoTokenizer.from_pretrained("eci-io/climategpt-7b")
|
22 |
+
# model = AutoModelForCausalLM.from_pretrained("eci-io/climategpt-7b", device_map="auto")
|
23 |
+
# return tokenizer, model
|
24 |
+
|
25 |
+
# tokenizer, model = load_climategpt()
|
26 |
+
|
27 |
+
# # generate responses
|
28 |
+
# def generate_response(user_input):
|
29 |
+
# prompt = f"""
|
30 |
+
# <|im_start|>system
|
31 |
+
# You are ClimateGPT, a large language model trained to provide information on climate change.<|im_end|>
|
32 |
+
# <|im_start|>user
|
33 |
+
# {user_input}<|im_end|>
|
34 |
+
# <|im_start|>assistant
|
35 |
+
# """
|
36 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
37 |
+
# outputs = model.generate(**inputs, max_new_tokens=200)
|
38 |
+
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
# return response.split("<|im_end|>")[-1].strip()
|
40 |
+
|
41 |
+
# # initialize session state for chat history
|
42 |
+
# if "history" not in st.session_state:
|
43 |
+
# st.session_state.history = []
|
44 |
+
|
45 |
+
# # sidebar for chat history
|
46 |
+
# with st.sidebar:
|
47 |
+
# st.title("Chat History")
|
48 |
+
# for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
|
49 |
+
# with st.expander(f"π¬ {question}"):
|
50 |
+
# st.write(f"**Chatbot:** {answer}")
|
51 |
+
|
52 |
+
# st.markdown("---")
|
53 |
+
# st.info("π± *Ask me anything about climate change, sustainability, or eco-friendly living.*")
|
54 |
+
|
55 |
+
# # main chat interface
|
56 |
+
# st.title("Climate Change Awareness Chatbot")
|
57 |
+
# st.subheader("Get answers, tips, and climate change facts for Uganda & East Africa")
|
58 |
+
|
59 |
+
# # Display chat history
|
60 |
+
# for question, answer in st.session_state.history:
|
61 |
+
# st.markdown(f"**You:** {question}")
|
62 |
+
# st.success(f"**Chatbot:** {answer}")
|
63 |
+
# st.markdown("---")
|
64 |
+
|
65 |
+
# # User input
|
66 |
+
# user_input = st.text_input("π¬ Type your message and press Enter", key="text_input")
|
67 |
+
|
68 |
+
# if user_input:
|
69 |
+
# response = generate_response(user_input)
|
70 |
+
|
71 |
+
# # Append conversation to history
|
72 |
+
# st.session_state.history.append((user_input, response))
|
73 |
+
|
74 |
+
# # Clear input field after processing
|
75 |
+
# st.session_state.text_input = ""
|
76 |
+
|
77 |
+
# # Rerun the app to display the updated chat history
|
78 |
+
# st.experimental_rerun()
|
79 |
+
|
80 |
+
# # Clear chat history button
|
81 |
+
# if st.button("Clear Chat History"):
|
82 |
+
# st.session_state.history = []
|
83 |
+
# st.experimental_rerun()
|
84 |
+
|
85 |
+
# # Footer
|
86 |
+
# st.markdown("""
|
87 |
+
# ---
|
88 |
+
# *Educational Purpose Only* | π± **SDG Guardians AI - 2024** | *For a greener East Africa*
|
89 |
+
# """)
|
90 |
+
|
91 |
+
# import streamlit as st
|
92 |
+
# from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering
|
93 |
+
|
94 |
+
# # page configuration
|
95 |
+
# st.set_page_config(page_title="Climate Chatbot - Uganda & East Africa", layout="wide")
|
96 |
+
|
97 |
+
# # model loading...
|
98 |
+
# @st.cache_resource
|
99 |
+
# def load_climate_bert():
|
100 |
+
# tokenizer = AutoTokenizer.from_pretrained("NinaErlacher/ClimateBERTqa")
|
101 |
+
# model = AutoModelForQuestionAnswering.from_pretrained("NinaErlacher/ClimateBERTqa")
|
102 |
+
# qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
103 |
+
# return qa_pipeline
|
104 |
+
|
105 |
+
# qa_pipeline = load_climate_bert()
|
106 |
+
|
107 |
+
# def generate_response(user_question, context):
|
108 |
+
# result = qa_pipeline(question=user_question, context=context)
|
109 |
+
# return result['answer']
|
110 |
+
|
111 |
+
# # Initialize session state variables
|
112 |
+
# if "history" not in st.session_state:
|
113 |
+
# st.session_state.history = []
|
114 |
+
|
115 |
+
# # Sidebar for chat history
|
116 |
+
# with st.sidebar:
|
117 |
+
# st.title("Chat History")
|
118 |
+
# for idx, (question, answer) in enumerate(st.session_state.history[::-1]):
|
119 |
+
# with st.expander(f"π¬ {question}"):
|
120 |
+
# st.write(f"**Chatbot:** {answer}")
|
121 |
+
|
122 |
+
# st.markdown("---")
|
123 |
+
# st.info("π± *Ask me anything about climate change, sustainability, or eco-friendly living.*")
|
124 |
+
|
125 |
+
# # main chat UI
|
126 |
+
# st.title("Climate Change Awareness Chatbot")
|
127 |
+
# st.subheader("Get answers, tips, and climate change facts for Uganda & East Africa")
|
128 |
+
|
129 |
+
# # chat display
|
130 |
+
# chat_container = st.container()
|
131 |
+
# with chat_container:
|
132 |
+
# for question, answer in st.session_state.history:
|
133 |
+
# st.markdown(f"**You:** {question}")
|
134 |
+
# st.success(f"**Chatbot:** {answer}")
|
135 |
+
# st.markdown("---")
|
136 |
+
|
137 |
+
# User input
|
138 |
+
# user_input = st.text_input("π¬ Type your message and press Enter", key="text_input")
|
139 |
+
|
140 |
+
# if user_input:
|
141 |
+
# context = """
|
142 |
+
# Climate change is affecting Uganda and East Africa in various ways, including unpredictable rainfall patterns,
|
143 |
+
# increased temperatures, and prolonged droughts. Sustainable farming practices, afforestation, and renewable
|
144 |
+
# energy adoption are key solutions to mitigate these effects.
|
145 |
+
# """ # Placeholder context
|
146 |
+
|
147 |
+
# response = generate_response(user_input, context)
|
148 |
+
|
149 |
+
# # append conversation to history
|
150 |
+
# /' ' st.session_state.history.append((user_input, response))
|
151 |
+
|
152 |
+
# # Clear stored input after processing
|
153 |
+
# st.session_state.pop("text_input", None)
|
154 |
+
# st.rerun()
|
155 |
+
|
156 |
+
# # Clear chat history button
|
157 |
+
# if st.button("Clear Chat History"):
|
158 |
+
# st.session_state.history = []
|
159 |
+
# st.rerun()
|
160 |
+
|
161 |
+
# # footer
|
162 |
+
# st.markdown("""
|
163 |
+
# ---
|
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.
|
280 |
+
|
281 |
+
# # footer fixed at the bottom
|
282 |
+
# st.markdown(
|
283 |
+
# """
|
284 |
+
# <style>
|
285 |
+
# .footer {
|
286 |
+
# position: fixed;
|
287 |
+
# bottom: 0;
|
288 |
+
# left: 100px;
|
289 |
+
# font-size: 14px;
|
290 |
+
# font-weight: 900;
|
291 |
+
# width: 100%;
|
292 |
+
# background-color: white;
|
293 |
+
# text-align: center;
|
294 |
+
# padding: 10px;
|
295 |
+
# box-shadow: 0px -2px 5px rgba(0, 0, 0, 0.1);
|
296 |
+
# z-index: 999;
|
297 |
+
# }
|
298 |
+
# </style>
|
299 |
+
# <div class="footer">
|
300 |
+
# ---
|
301 |
+
# *Educational Purpose Only* | π± **SDG Guardians AI - 2024** | *For a greener East Africa*
|
302 |
+
# </div>
|
303 |
+
# """,
|
304 |
+
# unsafe_allow_html=True
|
305 |
+
# )
|
model_utils.py
ADDED
File without changes
|
requirements.txt
ADDED
Binary file (3.86 kB). View file
|
|