ECOsphere / app.py
irmakakin's picture
Update app.py
c48afa4 verified
import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch
import numpy as np
client = InferenceClient('Qwen/Qwen2.5-72B-Instruct')
# Define the theme for the app.
theme_citrus = gr.themes.Citrus(
primary_hue="green",
secondary_hue="emerald",
neutral_hue="lime",
)
#sustainability tips
SUSTAINABILITY_TIPS = [
"Bring your own: Carry a reusable water bottle, coffee cup, and shopping bag.",
"Switch to LEDs: They use less energy and last longer than traditional bulbs.",
"Reduce food waste: Plan meals, store food properly, and compost scraps.",
"Choose sustainable transport: Walk, bike, carpool, or take public transit when possible.",
"Buy less, choose quality: Opt for durable items over disposable ones.",
"Unplug devices: Save energy by unplugging electronics when not in use.",
"Go paperless: Switch to digital bills, receipts, and notes.",
"Support eco-conscious brands: Choose companies with ethical sourcing and minimal packaging."
]
# Open the ECOsphere.txt file in read mode with UTF-8 encoding
with open("eskb.txt", "r", encoding="utf-8") as file:
# Read the entire contents of the file and store it in a variable
ECOsphere_text = file.read()
def respond(message, history):
top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
print("Top results:", top_results)
messages = [
{"role": "system", "content": f"You are a chatbot that encourages people to live more sustainably. Base your response on: {top_results}"}
]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat_completion(
model="Qwen/Qwen2.5-7B-Instruct",
messages=messages,
max_tokens=200,
temperature=0.5
)
return response.choices[0].message["content"].strip()
cleaned_chunks = []
def preprocess_text(text):
# Strip extra whitespace from the beginning and the end of the text
cleaned_text = text.strip()
chunks = cleaned_text.split('*')
# Create an empty list to store cleaned chunks
#cleaned_chunks = []
# Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
for chunk in chunks:
stripped_chunk = chunk.strip()
cleaned_chunks.append(stripped_chunk)
# Print cleaned_chunks
print(cleaned_chunks)
# Print the length of cleaned_chunks
print(len(cleaned_chunks))
# Return the cleaned_chunks
return cleaned_chunks
cleaned_chunks = preprocess_text(ECOsphere_text)
cleaned_chunks.extend(SUSTAINABILITY_TIPS)
# Load the pre-trained embedding model that converts text to vectors
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
# Convert each text chunk into a vector embedding and store as a tensor
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
# Print the chunk embeddings
print(chunk_embeddings)
# Print the shape of chunk_embeddings
print(chunk_embeddings.shape)
# Return the chunk_embeddings
return chunk_embeddings
chunk_embeddings = create_embeddings(cleaned_chunks)
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
# Convert the query text into a vector embedding
query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
# Normalize the query embedding to unit length for accurate similarity comparison
query_embedding_normalized = query_embedding / query_embedding.norm()
# Normalize all chunk embeddings to unit length for consistent comparison
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
# Calculate cosine similarity between query and all chunks using matrix multiplication
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized ) # Complete this line
# Print the similarities
print(similarities)
# Find the indices of the 3 chunks with highest similarity scores
top_indices = torch.topk(similarities, k=5).indices
# Print the top indices
print(top_indices)
# Create an empty list to store the most relevant chunks
top_chunks = []
# Loop through the top indices and retrieve the corresponding text chunks
for indices in top_indices:
relevant_info = cleaned_chunks[indices]
top_chunks.append(relevant_info)
return top_chunks
# Define the function to change the font based on the dropdown selection.
def change_font(font):
if font == "Open dyslexic":
gr.HTML('''
<style>
@fontface {
font-family: openDyslexic;
src: url(Open_Dyslexic-Regular.otf);
}
body {
font-family: openDyslexic;
}
<style/> ''')
else:
font_family = "Arial"
return
# Define the function to handle chatbot responses and maintain history.
def echo_bot(user_input, history):
history.append((user_input, user_input))
return "", history
# Create the Gradio app.
with gr.Blocks(theme=theme_citrus) as demo:
with gr.Row():
with gr.Column(scale=1):
# Dropdown menu for font selection.
font_dropdown = gr.Dropdown(choices=["Open dyslexic", "Normal"], label="Select Font")
apply_button = gr.Button("Apply Font")
text_output = gr.Markdown("Sample text here")
apply_button.click(fn=change_font, inputs=font_dropdown, outputs=text_output)
with gr.Column(scale=19):
# Image component.
gr.Image('ecosphere.png')
# Projects section.
with gr.Row(scale=9):
with gr.Column(scale=3):
with gr.Group():
gr.Markdown('GET INVOLVED')
with gr.Group():
gr.HTML('<a href="https://fridaysforfuture.org/" target="_blank">FRIDAYS FOR FUTURE</a>')
gr.HTML('<a href="https://www.sunrisemovement.org/" target="_blank">SUNRISE MOVEMENT</a>')
gr.HTML('<a href="https://thisiszerohour.org/" target="_blank">ZERO HOUR</a>')
gr.Markdown('Outside the US')
gr.HTML('<a href="https://community.youth4climate.info/homepage" target="_blank">Youth4Climate (EU)</a>')
gr.HTML('<a href="https://www.instagram.com/fridaysforfuturemapa/?hl=de" target="_blank">Fridays for Future MAPA (Africa, Latin America, Asia-Pacific) </a>')
with gr.Column(scale=7):
gr.ChatInterface(respond, type="messages")
demo.launch()