Spaces:
Sleeping
Sleeping
File size: 2,422 Bytes
01418af b0b6fac 834e947 b0b6fac 9dd9b8e b0b6fac 9dd9b8e b0b6fac 834e947 b0b6fac 9dd9b8e 8990b42 5fe999b 8990b42 834e947 b0b6fac 834e947 40339f5 834e947 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | from sentence_transformers import SentenceTransformer
import torch
import gradio as gr
from huggingface_hub import InferenceClient
with open("hindu_yuva_knowledge_base.txt", "r", encoding="utf-8") as file:
yuva_knowledge = file.read()
def preprocess_text(text):
cleaned_text = text.strip()
chunks = cleaned_text.split("\n")
cleaned_chunks = []
for chunk in chunks:
chunk = chunk.strip()
if chunk != "":
cleaned_chunks.append(chunk)
return cleaned_chunks
cleaned_chunks = preprocess_text(yuva_knowledge)
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 = []
for index in top_indices:
top_chunks.append(text_chunks[index])
return top_chunks
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
def respond(message, history):
top_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
context = "\n\n".join(top_chunks)
messages = [{"role": "system", "content": f"Always greet the user first by asking 'Hello, how can I help you?'. You are an assistant answering users' questions about Hindu YUVA. You just need to pull information from the website to answer basic questions. \n{context}"}]
for turn in history:
if turn["role"] == "user":
messages.append({"role": "user", "content": turn["content"]})
elif turn["role"] == "assistant":
messages.append({"role": "assistant", "content": turn["content"]})
messages.append({"role": "user", "content": message})
response = ""
for msg in client.chat_completion(messages, stream=True):
token = msg.choices[0].delta.content
if token is not None:
response += token
yield response
chatbot = gr.ChatInterface(respond)
chatbot.launch(debug=True) |