First commit
Browse files- .ipynb_checkpoints/README-checkpoint.md +85 -0
- .ipynb_checkpoints/app-checkpoint.py +65 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +5 -0
- README.md +85 -14
- app.py +52 -51
- requirements.txt +5 -1
.ipynb_checkpoints/README-checkpoint.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# EcoWise Chatbot
|
| 2 |
+
|
| 3 |
+
π **EcoWise** is a climate-savvy chatbot designed to answer questions related to climate change, sustainability, and carbon footprint using a language model fine-tuned for these topics.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The EcoWise chatbot leverages a Keras NLP model hosted on Hugging Face to provide intelligent responses to user queries. This tool aims to educate and inform users on important environmental issues.
|
| 8 |
+
|
| 9 |
+
## Features
|
| 10 |
+
|
| 11 |
+
- Interactive chat interface using Gradio.
|
| 12 |
+
- Responses generated by an AI model fine-tuned on climate-related data.
|
| 13 |
+
- Environmentally focused, providing insight into sustainability and climate change.
|
| 14 |
+
|
| 15 |
+
## Prerequisites
|
| 16 |
+
|
| 17 |
+
Before running the application, ensure you have the following:
|
| 18 |
+
|
| 19 |
+
- Python 3.7 or later
|
| 20 |
+
- A valid Hugging Face API key. You can obtain one by creating an account on [Hugging Face](https://huggingface.co/) and visiting your account settings.
|
| 21 |
+
|
| 22 |
+
## Installation
|
| 23 |
+
|
| 24 |
+
1. **Clone the repository**:
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
git clone https://huggingface.co/spaces/MNLobago/ECOWISE
|
| 28 |
+
cd ECOWISE
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
2. **Install the required packages**:
|
| 32 |
+
|
| 33 |
+
You can install the necessary packages using pip. If you have a `requirements.txt` file, run:
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
pip install -r requirements.txt
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
If you don't have a `requirements.txt`, you can install the required packages like this:
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
pip install gradio keras-nlp huggingface_hub
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Setting Up the Environment
|
| 46 |
+
|
| 47 |
+
To run the application, you need to set up your Hugging Face API key as an environment variable.
|
| 48 |
+
|
| 49 |
+
On your command line, set the environment variable before running the app:
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
export HUGGINGFACE_API_KEY="your_api_key_here"
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
Replace `"your_api_key_here"` with your actual Hugging Face API key.
|
| 56 |
+
|
| 57 |
+
## Running the Application
|
| 58 |
+
|
| 59 |
+
After installing all dependencies and setting the environment variable, you can launch the chatbot with the following command:
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
python app.py
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
This will start a Gradio web interface, where you can interact with the EcoWise chatbot directly.
|
| 66 |
+
|
| 67 |
+
## Using the Chatbot
|
| 68 |
+
|
| 69 |
+
1. Open the provided local URL in your web browser (usually http://127.0.0.1:7860).
|
| 70 |
+
2. Type your questions about climate change, sustainability, or carbon footprint into the chat interface.
|
| 71 |
+
3. Receive responses generated by the model.
|
| 72 |
+
|
| 73 |
+
## Code Structure
|
| 74 |
+
|
| 75 |
+
- `app.py`: Main application file that initializes and runs the EcoWise chatbot.
|
| 76 |
+
- `requirements.txt`: (If present) Lists all the required packages for the application.
|
| 77 |
+
|
| 78 |
+
## License
|
| 79 |
+
|
| 80 |
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
| 81 |
+
|
| 82 |
+
## Acknowledgments
|
| 83 |
+
|
| 84 |
+
- [Hugging Face](https://huggingface.co/): For providing the model and API services.
|
| 85 |
+
- [Gradio](https://gradio.app/): For creating beautiful user interfaces easily.
|
.ipynb_checkpoints/app-checkpoint.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gc
|
| 3 |
+
import psutil
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import keras_nlp
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
|
| 8 |
+
# Get the API key from environment variable
|
| 9 |
+
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
raise ValueError("Please set the 'HUGGINGFACE_API_KEY' environment variable.")
|
| 12 |
+
|
| 13 |
+
# Log in with the provided Hugging Face API token
|
| 14 |
+
login(api_key)
|
| 15 |
+
|
| 16 |
+
# Load the Keras NLP model from Hugging Face
|
| 17 |
+
model_path = "MNLobago/EcoWise_model"
|
| 18 |
+
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(f"hf://{model_path}")
|
| 19 |
+
|
| 20 |
+
class GemmaChat:
|
| 21 |
+
def __init__(self, model, max_length=150, system=""):
|
| 22 |
+
self.model = model
|
| 23 |
+
self.max_length = max_length
|
| 24 |
+
self.system = system
|
| 25 |
+
self.history = []
|
| 26 |
+
|
| 27 |
+
def get_full_prompt(self, user_input):
|
| 28 |
+
return f"User: {user_input}\nModel:"
|
| 29 |
+
|
| 30 |
+
def query(self, question):
|
| 31 |
+
if not self.history:
|
| 32 |
+
prompt = self.system + "\n" + self.get_full_prompt(question) if self.system else self.get_full_prompt(question)
|
| 33 |
+
else:
|
| 34 |
+
prompt = self.get_full_prompt(question)
|
| 35 |
+
|
| 36 |
+
response = self.model.generate(prompt, max_length=self.max_length)
|
| 37 |
+
model_response = response.replace(prompt, "").strip()
|
| 38 |
+
|
| 39 |
+
# Sanitize the response
|
| 40 |
+
if model_response.endswith('?'):
|
| 41 |
+
model_response = model_response.rstrip('?') + '.'
|
| 42 |
+
|
| 43 |
+
gc.collect()
|
| 44 |
+
return model_response
|
| 45 |
+
|
| 46 |
+
# Initialize the chat object
|
| 47 |
+
chat = GemmaChat(
|
| 48 |
+
model=gemma_lm,
|
| 49 |
+
system="""You are an intelligent chatbot focused on answering questions related to climate change, sustainability, and carbon footprint."""
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def chat_with_model(input_text):
|
| 53 |
+
chat.history = []
|
| 54 |
+
answer = chat.query(input_text)
|
| 55 |
+
return [("user", input_text), ("model", answer)]
|
| 56 |
+
|
| 57 |
+
# Create and launch the Gradio interface
|
| 58 |
+
demo = gr.Interface(
|
| 59 |
+
fn=chat_with_model,
|
| 60 |
+
inputs="text",
|
| 61 |
+
outputs="chatbot",
|
| 62 |
+
description="π Welcome to EcoWise, your go-to climate-savvy chatbot! I'm here to help you."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
gradio
|
| 3 |
+
keras-nlp
|
| 4 |
+
huggingface-hub
|
| 5 |
+
psutil
|
README.md
CHANGED
|
@@ -1,14 +1,85 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# EcoWise Chatbot
|
| 2 |
+
|
| 3 |
+
π **EcoWise** is a climate-savvy chatbot designed to answer questions related to climate change, sustainability, and carbon footprint using a language model fine-tuned for these topics.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
The EcoWise chatbot leverages a Keras NLP model hosted on Hugging Face to provide intelligent responses to user queries. This tool aims to educate and inform users on important environmental issues.
|
| 8 |
+
|
| 9 |
+
## Features
|
| 10 |
+
|
| 11 |
+
- Interactive chat interface using Gradio.
|
| 12 |
+
- Responses generated by an AI model fine-tuned on climate-related data.
|
| 13 |
+
- Environmentally focused, providing insight into sustainability and climate change.
|
| 14 |
+
|
| 15 |
+
## Prerequisites
|
| 16 |
+
|
| 17 |
+
Before running the application, ensure you have the following:
|
| 18 |
+
|
| 19 |
+
- Python 3.7 or later
|
| 20 |
+
- A valid Hugging Face API key. You can obtain one by creating an account on [Hugging Face](https://huggingface.co/) and visiting your account settings.
|
| 21 |
+
|
| 22 |
+
## Installation
|
| 23 |
+
|
| 24 |
+
1. **Clone the repository**:
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
git clone https://huggingface.co/spaces/MNLobago/ECOWISE
|
| 28 |
+
cd ECOWISE
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
2. **Install the required packages**:
|
| 32 |
+
|
| 33 |
+
You can install the necessary packages using pip. If you have a `requirements.txt` file, run:
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
pip install -r requirements.txt
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
If you don't have a `requirements.txt`, you can install the required packages like this:
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
pip install gradio keras-nlp huggingface_hub
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Setting Up the Environment
|
| 46 |
+
|
| 47 |
+
To run the application, you need to set up your Hugging Face API key as an environment variable.
|
| 48 |
+
|
| 49 |
+
On your command line, set the environment variable before running the app:
|
| 50 |
+
|
| 51 |
+
```bash
|
| 52 |
+
export HUGGINGFACE_API_KEY="your_api_key_here"
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
Replace `"your_api_key_here"` with your actual Hugging Face API key.
|
| 56 |
+
|
| 57 |
+
## Running the Application
|
| 58 |
+
|
| 59 |
+
After installing all dependencies and setting the environment variable, you can launch the chatbot with the following command:
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
python app.py
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
This will start a Gradio web interface, where you can interact with the EcoWise chatbot directly.
|
| 66 |
+
|
| 67 |
+
## Using the Chatbot
|
| 68 |
+
|
| 69 |
+
1. Open the provided local URL in your web browser (usually http://127.0.0.1:7860).
|
| 70 |
+
2. Type your questions about climate change, sustainability, or carbon footprint into the chat interface.
|
| 71 |
+
3. Receive responses generated by the model.
|
| 72 |
+
|
| 73 |
+
## Code Structure
|
| 74 |
+
|
| 75 |
+
- `app.py`: Main application file that initializes and runs the EcoWise chatbot.
|
| 76 |
+
- `requirements.txt`: (If present) Lists all the required packages for the application.
|
| 77 |
+
|
| 78 |
+
## License
|
| 79 |
+
|
| 80 |
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
| 81 |
+
|
| 82 |
+
## Acknowledgments
|
| 83 |
+
|
| 84 |
+
- [Hugging Face](https://huggingface.co/): For providing the model and API services.
|
| 85 |
+
- [Gradio](https://gradio.app/): For creating beautiful user interfaces easily.
|
app.py
CHANGED
|
@@ -1,64 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gc
|
| 3 |
+
import psutil
|
| 4 |
import gradio as gr
|
| 5 |
+
import keras_nlp
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
|
| 8 |
+
# Get the API key from environment variable
|
| 9 |
+
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
raise ValueError("Please set the 'HUGGINGFACE_API_KEY' environment variable.")
|
| 12 |
|
| 13 |
+
# Log in with the provided Hugging Face API token
|
| 14 |
+
login(api_key)
|
| 15 |
|
| 16 |
+
# Load the Keras NLP model from Hugging Face
|
| 17 |
+
model_path = "MNLobago/EcoWise_model"
|
| 18 |
+
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(f"hf://{model_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
class GemmaChat:
|
| 21 |
+
def __init__(self, model, max_length=150, system=""):
|
| 22 |
+
self.model = model
|
| 23 |
+
self.max_length = max_length
|
| 24 |
+
self.system = system
|
| 25 |
+
self.history = []
|
| 26 |
|
| 27 |
+
def get_full_prompt(self, user_input):
|
| 28 |
+
return f"User: {user_input}\nModel:"
|
| 29 |
|
| 30 |
+
def query(self, question):
|
| 31 |
+
if not self.history:
|
| 32 |
+
prompt = self.system + "\n" + self.get_full_prompt(question) if self.system else self.get_full_prompt(question)
|
| 33 |
+
else:
|
| 34 |
+
prompt = self.get_full_prompt(question)
|
| 35 |
+
|
| 36 |
+
response = self.model.generate(prompt, max_length=self.max_length)
|
| 37 |
+
model_response = response.replace(prompt, "").strip()
|
| 38 |
+
|
| 39 |
+
# Sanitize the response
|
| 40 |
+
if model_response.endswith('?'):
|
| 41 |
+
model_response = model_response.rstrip('?') + '.'
|
| 42 |
|
| 43 |
+
gc.collect()
|
| 44 |
+
return model_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Initialize the chat object
|
| 47 |
+
chat = GemmaChat(
|
| 48 |
+
model=gemma_lm,
|
| 49 |
+
system="""You are an intelligent chatbot focused on answering questions related to climate change, sustainability, and carbon footprint."""
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
def chat_with_model(input_text):
|
| 53 |
+
chat.history = []
|
| 54 |
+
answer = chat.query(input_text)
|
| 55 |
+
return [("user", input_text), ("model", answer)]
|
| 56 |
|
| 57 |
+
# Create and launch the Gradio interface
|
| 58 |
+
demo = gr.Interface(
|
| 59 |
+
fn=chat_with_model,
|
| 60 |
+
inputs="text",
|
| 61 |
+
outputs="chatbot",
|
| 62 |
+
description="π Welcome to EcoWise, your go-to climate-savvy chatbot! I'm here to help you."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
+
demo.launch()
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
gradio
|
| 3 |
+
keras-nlp
|
| 4 |
+
huggingface-hub
|
| 5 |
+
psutil
|