| import gradio as gr |
| from sentence_transformers import SentenceTransformer |
| import torch |
|
|
|
|
| |
| with open("knowledge.txt", "r", encoding="utf-8") as file: |
| quote_text = file.read() |
|
|
| cleaned_text = quote_text.strip() |
| chunks = cleaned_text.split("\n") |
| cleaned_chunks = [] |
| cleaned_chunks = [chunk.strip() for chunk in chunks if (chunk.strip())] |
|
|
| |
| model = SentenceTransformer('all-MiniLM-L6-v2') |
| chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True) |
|
|
| |
| |
| def get_top_chunks(message): |
| query_embedding = model.encode(message, 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=3).indices |
| print(top_indices) |
|
|
| top_chunks = [] |
|
|
| top_chunks = [cleaned_chunks[i.item()] for i in top_indices] |
|
|
| return top_chunks[0] |
| |
| def main(message, history): |
| return get_top_chunks(message) |
|
|
| chatbot = gr.ChatInterface(main, type="messages", examples=["Motivate me", "Give me a happy quote", "Quote about travel"], |
| title="Quotes!", description="Ask for a famous quote!") |
| chatbot.launch() |