Update app.py
Browse files1234567891011import gradio as grimport randomfrom huggingface_hub import InferenceClientclient = 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'}]if history:messages.extend(history)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465import gradio as grimport randomfrom huggingface_hub import InferenceClientfrom sentence_transformers import SentenceTransformerimport torchwith 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 variableprint(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 infor 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 parameterquery_embedding = model.encode(query, convert_to_tensor=True)# encode query to vector embedding for comparisonquery_embedding_normalized = query_embedding / query_embedding.norm()# normalize my query to 1; allows for comparison of meaningchunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)# normailizing chunks for comparison of meaningsimilarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)print(similarities)# using matmul (matrix multiplication method) to compare query to chunkstop_indices = torch.topk(similarities, k=3).indicesprint(top_indices)# get the indices of the chunks that are most similar to querytop_chunks = []for i in top_indices:chunk = chunks[i]# for each index number in top_indices, get back the texttop_chunks.append(chunk)# values of each index number is added to top_chunksreturn top_chunksclient = 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}]if history:messages.extend(history)
app.py+55-1