Update
Browse files
app.py
CHANGED
|
@@ -1,65 +1,76 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
from huggingface_hub import login
|
|
|
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
-
from functools import lru_cache
|
| 7 |
-
import keras_nlp
|
| 8 |
-
|
| 9 |
-
# Function to convert Markdown to HTML (preserving formatting)
|
| 10 |
-
def markdown_to_html(markdown_text):
|
| 11 |
-
# Convert Markdown to HTML
|
| 12 |
-
html = markdown.markdown(markdown_text)
|
| 13 |
-
return html # Return HTML as is (preserving the formatting)
|
| 14 |
|
| 15 |
# Get the API key from environment variable
|
| 16 |
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
| 17 |
if not api_key:
|
| 18 |
raise ValueError("Please set the 'HUGGINGFACE_API_KEY' environment variable.")
|
| 19 |
|
| 20 |
-
# Log in with the provided Hugging Face API token
|
| 21 |
login(api_key)
|
| 22 |
|
| 23 |
-
# Load the Keras NLP model from Hugging Face
|
| 24 |
model_path = "MNLobago/EcoWise_model"
|
| 25 |
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(f"hf://{model_path}")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
class GemmaChat:
|
| 28 |
def __init__(self, model, max_length=150, system=""):
|
| 29 |
self.model = model
|
| 30 |
self.max_length = max_length
|
| 31 |
self.system = system
|
|
|
|
| 32 |
|
| 33 |
def get_full_prompt(self, user_input):
|
| 34 |
return f"User: {user_input}\nModel:"
|
| 35 |
|
| 36 |
def query(self, question):
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
response = self.model.generate(prompt, max_length=self.max_length)
|
| 39 |
model_response = response.replace(prompt, "").strip()
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return model_response
|
| 42 |
|
| 43 |
-
# Initialize the chat object
|
| 44 |
chat = GemmaChat(
|
| 45 |
model=gemma_lm,
|
| 46 |
-
system="You are an intelligent chatbot focused on answering questions related to climate change, sustainability, and carbon footprint."
|
| 47 |
)
|
| 48 |
|
| 49 |
-
@lru_cache(maxsize=100)
|
| 50 |
-
def cached_query(question):
|
| 51 |
-
return chat.query(question)
|
| 52 |
-
|
| 53 |
-
# Gradio interface function (faster with caching)
|
| 54 |
def chat_with_model(input_text):
|
| 55 |
-
|
|
|
|
| 56 |
return [("user", input_text), ("model", answer)]
|
| 57 |
|
| 58 |
# Create and launch the Gradio interface
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=chat_with_model,
|
| 61 |
inputs="text",
|
| 62 |
-
outputs="html", #
|
| 63 |
description="π Welcome to EcoWise, your go-to climate-savvy chatbot! I'm here to help you."
|
| 64 |
)
|
| 65 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gc
|
| 3 |
import gradio as gr
|
| 4 |
+
import keras_nlp
|
| 5 |
from huggingface_hub import login
|
| 6 |
+
import markdown
|
| 7 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Get the API key from environment variable
|
| 10 |
api_key = os.getenv("HUGGINGFACE_API_KEY")
|
| 11 |
if not api_key:
|
| 12 |
raise ValueError("Please set the 'HUGGINGFACE_API_KEY' environment variable.")
|
| 13 |
|
| 14 |
+
# Log in with the provided Hugging Face API token
|
| 15 |
login(api_key)
|
| 16 |
|
| 17 |
+
# Load the Keras NLP model from Hugging Face
|
| 18 |
model_path = "MNLobago/EcoWise_model"
|
| 19 |
gemma_lm = keras_nlp.models.GemmaCausalLM.from_preset(f"hf://{model_path}")
|
| 20 |
|
| 21 |
+
# Function to convert Markdown to HTML (preserving the formatting)
|
| 22 |
+
def markdown_to_html(markdown_text):
|
| 23 |
+
html = markdown.markdown(markdown_text) # Convert Markdown to HTML
|
| 24 |
+
return html # Return the HTML with formatting preserved
|
| 25 |
+
|
| 26 |
class GemmaChat:
|
| 27 |
def __init__(self, model, max_length=150, system=""):
|
| 28 |
self.model = model
|
| 29 |
self.max_length = max_length
|
| 30 |
self.system = system
|
| 31 |
+
self.history = []
|
| 32 |
|
| 33 |
def get_full_prompt(self, user_input):
|
| 34 |
return f"User: {user_input}\nModel:"
|
| 35 |
|
| 36 |
def query(self, question):
|
| 37 |
+
if not self.history:
|
| 38 |
+
prompt = self.get_full_prompt(question)
|
| 39 |
+
else:
|
| 40 |
+
prompt = self.get_full_prompt(question)
|
| 41 |
+
|
| 42 |
response = self.model.generate(prompt, max_length=self.max_length)
|
| 43 |
model_response = response.replace(prompt, "").strip()
|
| 44 |
+
|
| 45 |
+
# Convert the Markdown response to HTML
|
| 46 |
+
model_response = markdown_to_html(model_response)
|
| 47 |
+
|
| 48 |
+
# Sanitize the response if necessary (optional)
|
| 49 |
+
model_response = model_response.rstrip('?')
|
| 50 |
+
|
| 51 |
+
# Ensure the response ends with a period if it doesn't end with a punctuation mark
|
| 52 |
+
if model_response and model_response[-1] not in '.!?':
|
| 53 |
+
model_response += '.'
|
| 54 |
+
|
| 55 |
+
gc.collect()
|
| 56 |
return model_response
|
| 57 |
|
| 58 |
+
# Initialize the chat object
|
| 59 |
chat = GemmaChat(
|
| 60 |
model=gemma_lm,
|
| 61 |
+
system="""You are an intelligent chatbot focused on answering questions related to climate change, sustainability, and carbon footprint."""
|
| 62 |
)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
def chat_with_model(input_text):
|
| 65 |
+
chat.history = [] # Clear history for each new query
|
| 66 |
+
answer = chat.query(input_text)
|
| 67 |
return [("user", input_text), ("model", answer)]
|
| 68 |
|
| 69 |
# Create and launch the Gradio interface
|
| 70 |
demo = gr.Interface(
|
| 71 |
fn=chat_with_model,
|
| 72 |
inputs="text",
|
| 73 |
+
outputs="html", # Output as HTML to render the formatted response
|
| 74 |
description="π Welcome to EcoWise, your go-to climate-savvy chatbot! I'm here to help you."
|
| 75 |
)
|
| 76 |
|