|
|
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') |
|
|
|
|
|
theme_citrus = gr.themes.Citrus( |
|
|
primary_hue="green", |
|
|
secondary_hue="emerald", |
|
|
neutral_hue="lime", |
|
|
) |
|
|
|
|
|
|
|
|
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." |
|
|
] |
|
|
|
|
|
with open("eskb.txt", "r", encoding="utf-8") as file: |
|
|
|
|
|
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): |
|
|
|
|
|
cleaned_text = text.strip() |
|
|
|
|
|
chunks = cleaned_text.split('*') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for chunk in chunks: |
|
|
stripped_chunk = chunk.strip() |
|
|
cleaned_chunks.append(stripped_chunk) |
|
|
|
|
|
|
|
|
|
|
|
print(cleaned_chunks) |
|
|
|
|
|
|
|
|
print(len(cleaned_chunks)) |
|
|
|
|
|
|
|
|
return cleaned_chunks |
|
|
cleaned_chunks = preprocess_text(ECOsphere_text) |
|
|
cleaned_chunks.extend(SUSTAINABILITY_TIPS) |
|
|
|
|
|
model = SentenceTransformer('all-MiniLM-L6-v2') |
|
|
|
|
|
def create_embeddings(text_chunks): |
|
|
|
|
|
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) |
|
|
|
|
|
|
|
|
print(chunk_embeddings) |
|
|
|
|
|
|
|
|
print(chunk_embeddings.shape) |
|
|
|
|
|
|
|
|
return chunk_embeddings |
|
|
chunk_embeddings = create_embeddings(cleaned_chunks) |
|
|
|
|
|
def get_top_chunks(query, chunk_embeddings, text_chunks): |
|
|
|
|
|
query_embedding = model.encode(query, convert_to_tensor=True) |
|
|
|
|
|
|
|
|
query_embedding_normalized = query_embedding / query_embedding.norm() |
|
|
|
|
|
|
|
|
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) |
|
|
|
|
|
|
|
|
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized ) |
|
|
|
|
|
|
|
|
print(similarities) |
|
|
|
|
|
|
|
|
top_indices = torch.topk(similarities, k=5).indices |
|
|
|
|
|
|
|
|
print(top_indices) |
|
|
|
|
|
|
|
|
top_chunks = [] |
|
|
|
|
|
|
|
|
for indices in top_indices: |
|
|
relevant_info = cleaned_chunks[indices] |
|
|
top_chunks.append(relevant_info) |
|
|
return top_chunks |
|
|
|
|
|
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 |
|
|
|
|
|
def echo_bot(user_input, history): |
|
|
history.append((user_input, user_input)) |
|
|
return "", history |
|
|
|
|
|
|
|
|
with gr.Blocks(theme=theme_citrus) as demo: |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
|
|
|
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): |
|
|
|
|
|
gr.Image('ecosphere.png') |
|
|
|
|
|
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() |