try
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141 unmodified line116117118119120import gradio as grimport randomimport osfrom 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 variablecleaned_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 listmodel = 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)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)# using matmul (matrix multiplication method) to compare query to chunkstop_indices = torch.topk(similarities, k=3).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(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})response = ""for message in client.chat_completion(#max_tokens controls how many words your responses ismessages,max_tokens = 500,# stream = True,#temperature = 0.8, #code a decimal between 0-2#top_p = .65 #code a decimal between 0-1):token = message.choices[0].delta.contentresponse += tokenyield response#print(response["choices"][0]["message"]["content"].strip())#yield response["choices"][0]["message"]["content"].strip()chatbot = gr.ChatInterface(respond, type='messages')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 messagesby 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 linemsg.submit(respond, [msg, chatbot], [msg, chatbot])send.click(respond, [msg, chatbot], [msg, chatbot])chatbot.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859601 unmodified line62636465import gradio as grimport 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()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)# Hugging Face Inference Client (add token if required)client = InferenceClient(model='Qwen/Qwen2.5-72B-Instruct',# token=os.getenv("HF_TOKEN"))# 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).indicesreturn [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 part in client.chat_completion(messages, max_tokens=500):token = part.choices[0].delta.contentif token:response += tokenchat_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.✨ *All you have to do is wrap it.*""")chatbot = gr.Chatbot()msg = gr.Textbox(placeholder="Say hi to WrapIT here!", label="Message")send = gr.Button("Send")1 unmodified linemsg.submit(respond, [msg, chatbot], [msg, chatbot])send.click(respond, [msg, chatbot], [msg, chatbot])demo.launch()
app.py+42-97