Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061862636465import gradio as grimport randomfrom huggingface_hub import InferenceClientfrom sentence_transformers import SentenceTransformerimport torch
with open("knowledge.txt", "r", encoding="utf-8") as file: recent = file.read() # opens the text, saves as "file" # reads the text and saves as water_cycle_text variable
print(recent)
cleaned_text = recent.strip()# cleaning up the textchunks = cleaned_text.split("\n")# seperating the text into one sentence piecescleaned_chunks = []# creating an empty list to put the cleaned chunks in
for chunk in chunks: stripped_chunk = chunk.strip() if stripped_chunk: cleaned_chunks.append(stripped_chunk) # loop through chunks and add not empty chunks to cleaned_chunks listprint(cleaned_chunks)
model = SentenceTransformer('all-MiniLM-L6-v2')
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)# encode the model, pass through my cleaned chunks and convert to vector embeddings (not arrays)print(chunk_embeddings)
def get_top_chunks(query): # create my function taking query as parameter query_embedding = model.encode(query, convert_to_tensor=True) # encode query to vector embedding for comparison query_embedding_normalized = query_embedding / query_embedding.norm() # normalize my query to 1; allows for comparison of meaning chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) # normailizing chunks for comparison of meaning
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) print(similarities) # using matmul (matrix multiplication method) to compare query to chunks top_indices = torch.topk(similarities, k=3).indices print(top_indices) # get the indices of the chunks that are most similar to query
top_chunks = []
for i in top_indices: chunk = chunks[i] # for each index number in top_indices, get back the text top_chunks.append(chunk) # values of each index number is added to top_chunks return top_chunks client = InferenceClient('Qwen/Qwen2.5-72B-Instruct')#client is where you can change the LLM model!def respond(message,history): messages = [{'role': 'system', 'content': 'You give really good gift ideas and are super helpful! You also tell me the price of each item. Give me 5 gift ideas if I ask'}] messages = [{'role': 'system', 'content': 'You give really good gift ideas and are super helpful! You also tell me the price of each item. Give me 5 gift ideas if I ask}] if history: messages.extend(history)
app.py+55-1