import gradio as gr from huggingface_hub import InferenceClient from sentence_transformers import SentenceTransformer ##import humaniites dataset: from datasets import load_dataset dataset = load_dataset("HumanLLMs/Human-Like-DPO-Dataset") client = InferenceClient("Qwen/Qwen2.5-7B-Instruct") def respond(message, history): ##can change to humanitites when needed messages = [{ "role": "system", "content": """ You are a chatbot tutor made to give students humanities, history, or english practice problems and assist them with questions they have. """ }] if history: messages.extend(history) messages.append({"role": "user", "content": message}) response = client.chat_completion( messages, max_tokens=None, temperature=1.0 ) return response.choices[0].message.content.strip() with open("sampleknowledge.txt", "r", encoding="utf-8") as file: # Read the entire contents of the file and store it in a variable sampleknowlegde = file.read() #Color/Title/Description def create_humanities(): with gr.Column(visible=False) as humanities: theme=gr.themes.Soft( primary_hue="sky", secondary_hue="cyan", neutral_hue="slate" ), css=""" body { background-color: #EEF6FF; font-family: Arial, sans-serif; } .gradio-container { background-color: #EEF6FF !important; } .bot, .bot *, .message.bot, .message.bot * { color: #2D4A66 !important; } .user, .user *, .message.user, .message.user * { color: #2D4A66 !important; } .message, .message * { color: #2D4A66 !important; } /* Headings */ h1 { color: #4A6FA5 !important; text-align: center; font-size: 42px; font-weight: bold; } h3 { color: #6B8DB5 !important; text-align: center; margin-bottom: 20px; } /* Buttons */ button { background-color: #B8E3F9 !important; color: #2D4A66 !important; border-radius: 12px !important; border: none !important; } button:hover { background-color: #A8D9F4 !important; } /* Text input */ textarea, input, textarea::placeholder { color: #2D4A66 !important; background-color: #FFFFFF !important; border: 2px solid #D7EAF8 !important; border-radius: 12px !important; } textarea{ color: #2D4A66 !important; caret-color: #2D4A66 !important; } /* Chat messages */ .message { background-color: #F8FBFF !important; color: #2D4A66 !important; border-radius: 12px !important; } /* Example buttons */ .examples button { background-color: #E7F4FD !important; color: #2D4A66 !important; } /* Rounded cards */ .block { border-radius: 16px !important; } """ gr.Markdown(""" HUMANITIESbot Ask me questions about humanities! """) gr.ChatInterface( fn=respond, examples=["When was the American Revolution?", "Can you tell me how to cite my sources using MLA 8?"] ) return humanities def preprocess_text(text): cleaned_text = text.strip() chunks = cleaned_text.split("\n") cleaned_chunks = [] for chunk in chunks: stripped_chunk = chunk.strip() if len(stripped_chunk) > 0: cleaned_chunks.append(stripped_chunk) return cleaned_chunks cleaned_chunks = preprocess_text(sampleknowlegde) # Load the pre-trained embedding model that converts text to vectors model = SentenceTransformer('all-MiniLM-L6-v2') def create_embeddings(text_chunks): # Convert each text chunk into a vector embedding and store as a tensor chunk_embeddings = model.encode( text_chunks, convert_to_tensor=True ) # Replace ... with the text_chunks list # Return the chunk_embeddings return chunk_embeddings # Call the create_embeddings function and store the result in a new chunk_embeddings variable chunk_embeddings = create_embeddings(cleaned_chunks) # Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks def get_top_chunks(query, chunk_embeddings, text_chunks): # Convert the query text into a vector embedding query_embedding = model.encode( query, convert_to_tensor=True ) # Normalize the query embedding to unit length for accurate similarity comparison query_embedding_normalized = query_embedding / query_embedding.norm() # Normalize all chunk embeddings to unit length for consistent comparison chunk_embeddings_normalized = ( chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) ) # Calculate cosine similarity between all chunks and the query using matrix multiplication similarities = torch.matmul( chunk_embeddings_normalized, query_embedding_normalized ) # Find the indices of the 3 chunks with highest similarity scores top_indices = torch.topk(similarities, k=3).indices # Create an empty list to store the most relevant chunks top_chunks = [] # Loop through the top indices and retrieve the corresponding text chunks for i in top_indices: chunk = text_chunks[i] top_chunks.append(chunk) # Return the list of most relevant chunks return top_chunks chatbot = gr.ChatInterface(respond) chatbot.launch()