Quote-Generator / app.py
evacado's picture
Update app.py
b19fcb2 verified
import gradio as gr
from sentence_transformers import SentenceTransformer
import torch
# Load and process your knowledge base text file.
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())]
# Chunk the knowledge base and use an embedding model to convert the chunks to embeddings.
model = SentenceTransformer('all-MiniLM-L6-v2')
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
# Create a function that converts the user’s query to an embedding and calculates similarity
#scores to return the most relevant context from your knowledge base.
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]
# Pass this context to the LLM along with the user’s query to generate the response!
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()