Spaces:
Sleeping
Sleeping
| from sentence_transformers import SentenceTransformer | |
| import torch | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| #theme of the UI | |
| theme = gr.themes.Soft( | |
| primary_hue="green", | |
| secondary_hue="blue", | |
| neutral_hue="gray", | |
| font=["Roboto", "sans-serif"] | |
| ) | |
| #semantic search application | |
| with open("sustainability_tips.txt", "r", encoding="utf-8") as file: | |
| sustainability_tips_text = file.read() | |
| def preprocess_text(text): | |
| cleaned_text = text.strip() | |
| chunks = cleaned_text.split("\n") | |
| cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip() != ''] | |
| return cleaned_chunks | |
| cleaned_chunks = preprocess_text(sustainability_tips_text) | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| def create_embeddings(text_chunks): | |
| chunk_embeddings = model.encode(text_chunks, convert_to_tensor = True) | |
| 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) | |
| top_indices = torch.topk(similarities, k=3).indices | |
| top_chunks = [text_chunks[i] for i in top_indices] | |
| return top_chunks | |
| client = InferenceClient("meta-llama/Llama-3.1-8B-Instruct") | |
| # applying RAG | |
| def respond(message, history): | |
| top_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks) | |
| context = "\n".join(top_chunks) | |
| messages = [{ | |
| "role": "system", | |
| "content": ( | |
| "You are EcoBot, a sustainability assistant.\n" | |
| "You help users reduce their environmental impact.\n\n" | |
| "You don't know anything about the current user." | |
| "Introduce yourself as EcoBot and share what you can do." | |
| "IMPORTANT RULES:\n" | |
| "- The context below is a list of sustainability tips from OTHER PEOPLE.\n" | |
| "- These are NOT things the user has done or mentioned.\n" | |
| "- Do NOT assume anything about the user's lifestyle.\n" | |
| "- Only use the context as suggestions or ideas to recommend.\n" | |
| "- If needed, ask a clarifying question before giving advice.\n\n" | |
| f"Context:\n{context}" | |
| ) | |
| }] | |
| if history: | |
| messages.extend(history) | |
| messages.append({"role": "user", "content": message}) | |
| response = "" | |
| stream = client.chat_completion( | |
| messages, | |
| max_tokens=250, | |
| top_p=0.2, | |
| stream=True | |
| ) | |
| for event in stream: | |
| token = event.choices[0].delta.content | |
| if token: | |
| response += token | |
| yield response | |
| #launching chatbot | |
| #chatbot = gr.ChatInterface( | |
| #respond, | |
| #title="EcoBot 🌿", | |
| #description="Your sustainability assistant" | |
| #).launch(theme=theme) | |
| with gr.Blocks() as demo: | |
| chatbot = gr.Chatbot(placeholder="<strong>Hi, I'm EcoBot 🌱</strong><br>Ask me about living more sustainably") | |
| gr.ChatInterface(respond, chatbot=chatbot,title="EcoBot 🌿",description="Your sustainability assistant") | |
| demo.launch(theme = theme) |