try
Browse files122345678910111213141516171819202122232425262791011121314313233343536373839404142434445464748495051525354555657585915166162631718192065666768697071727374752122232425262728293031323334353637383978798081824041424344454647484950515253548485868788899091929394555657969798991001011021031041051061071081091101115859601 unmodified line62636411912065import gradio as grimport randomimport osfrom huggingface_hub import InferenceClientfrom sentence_transformers import SentenceTransformerimport torch
# Load and prepare knowledge basewith 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
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 list
recent = file.read()
chunks = [chunk.strip() for chunk in recent.strip().split("\n") if chunk.strip()]model = SentenceTransformer('all-MiniLM-L6-v2')chunk_embeddings = model.encode(chunks, convert_to_tensor=True)
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)
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)
# using matmul (matrix multiplication method) to compare query to chunks top_indices = torch.topk(similarities, k=3).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 # Hugging Face Inference Client (add token if required)client = InferenceClient( model='Qwen/Qwen2.5-72B-Instruct' #token = os.getenv('HF_TOKEN')) model='Qwen/Qwen2.5-72B-Instruct', # token=os.getenv("HF_TOKEN"))
#client is where you can change the LLM model!def respond(message,history): if not message.strip(): return "Hello!" gift_ideas = get_top_chunks(message) messages = [{'role': 'system', 'content': f'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. Use the following database for gift ideas: {gift_ideas}'}] if history: messages.extend(history) messages.append({"role": "user", "content": message})# Top similar chunksdef get_top_chunks(query): query_embedding = model.encode(query, convert_to_tensor=True) 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) top_indices = torch.topk(similarities, k=3).indices return [chunks[i] for i in top_indices]
# Main response functiondef respond(user_message, chat_history): gift_ideas = get_top_chunks(user_message) messages = [{"role": "system", "content": f"You are helpful and give 5 gift ideas with prices. Use this database: {gift_ideas}"}] for user, bot in chat_history: messages.append({"role": "user", "content": user}) messages.append({"role": "assistant", "content": bot}) messages.append({"role": "user", "content": user_message})
response = "" for message in client.chat_completion( #max_tokens controls how many words your responses is messages, max_tokens = 500, # stream = True, for part in client.chat_completion(messages, max_tokens=500): token = part.choices[0].delta.content if token: response += token
chat_history.append((user_message, response)) return "", chat_history
# Gradio appwith gr.Blocks(theme='hmb/amethyst') as demo: gr.Markdown(""" # 🎁 Introducing WrapIT! **WrapIT** helps users find personalized gift ideas and craft thoughtful card messages** by inputting details like the recipient's interests, celebration type, and budget. #temperature = 0.8, #code a decimal between 0-2 #top_p = .65 #code a decimal between 0-1 ): token = message.choices[0].delta.content response += token yield response #print(response["choices"][0]["message"]["content"].strip()) #yield response["choices"][0]["message"]["content"].strip() chatbot = gr.ChatInterface(respond, type='messages') ✨ *All you have to do is wrap it.* """)
with gr.Blocks(theme='hmb/amethyst') as chatbot: with gr.Row(equal_height=True): with gr.Column(scale=10): gr.Markdown( """ # 🎁 Introducing WrapIT! **WrapIT** helps users find personalized gift ideas and craft thoughtful card messages by inputting details like the recipient's interests, celebration type, and budget. ✨ *All you have to do is wrap it.* """ ) #gr.chatInterface()
chatbot = gr.Chatbot() msg = gr.Textbox(placeholder="Say hi to WrapIT here!", label="Message") send = gr.Button("Send")1 unmodified line msg.submit(respond, [msg, chatbot], [msg, chatbot]) send.click(respond, [msg, chatbot], [msg, chatbot])
chatbot.launch()demo.launch()app.py+42-97