Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283847 unmodified lines92939495969798991001011024 unmodified lines10710810911011111211311411511611711811912012112212312411 unmodified lines1361371381391401411421431441451461471481491501511521531541551561 unmodified linefrom sentence_transformers import SentenceTransformerimport torchfrom huggingface_hub import InferenceClient#import os#HF_TOKEN = os.environ.get("HF_TOKEN")with open("knowledge.txt", "r", encoding="utf-8") as file:knowledge = file.read()cleaned_text = knowledge.strip()# cleaning up the textchunks = cleaned_text.split("\n")# separating the text into one sentence chunkscleaned_chunks = []# creating a empty list called cleaned_chunksfor chunk in chunks:# for every chunk in the chunks list,stripped_chunk = chunk.strip()# the chunk is getting strippedif stripped_chunk:#if the chunk is not empty then it is being appended to the cleaned chunk list.cleaned_chunks.append(stripped_chunk)model = SentenceTransformer('all-MiniLM-L6-v2')chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)# encode the model, pass through my clean chunks and convert to vector embeddings (not arrays)def get_top_chunks(query): # store a function that gets the most relevant_info and make it return a variable “relevant_info” then# create my function taking query as my parameterquery_embedding = model.encode(query, convert_to_tensor=True)# turning my query to a vector embedding for comparisonquery_embedding_normalized = query_embedding / query_embedding.norm()# normlaize my query to 1: allows for comparison of meaningchunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)# normalizing chunks for comparison of meaningsimilarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)# using my 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 my querytop_chunks = []for i in top_indices:chunk = chunks[i]# for each index number in top_indices, get back the texttop_chunks.append(chunk) # adding the sentences that are associated with the top indices to the listreturn top_chunksclient = InferenceClient("google/gemma-3-27b-it")def respond(message, history):knowledge_base = get_top_chunks(message)system_message = f"You are a helpful chatbot named scooby, kinda like the cartoon character but not too much like it, who knows alot about pets and their diets and loves helping pet owners, and can only answer questions about pets:{knowledge_base}"messages = [{"role": "system", "content": system_message}]if history:messages.extend(history)messages.append({"role": "user", "content": message})response = ""for messages in client.chat_completion(messages,max_tokens = 2500, stream = True):token = messages.choices[0].delta.contentresponse += tokenyield responsetheme = gr.themes.Monochrome(primary_hue="teal",secondary_hue="orange")welcome_message = """# Welcome to ScoobyAI7 unmodified lines- Creating recipes for pets based on dietary needs"""#chatbot = gr.ChatInterface(respond, type = "messages", theme)import gradio as grwith gr.Blocks(theme=theme) as demo:gr.Image(value="scooby.png",4 unmodified linesshow_download_button=False,container=False)gr.Markdown(welcome_message)with gr.Row():with gr.Column():gr.Markdown(topics)with gr.Tabs():with gr.Tab("Main Page"):with gr.Row():text_input = gr.Textbox(label="Ask Scooby a question about your pet")# image_input = gr.Image(type="pil", label="Upload an image of your pet (optional)")output = gr.Textbox(label="Scooby's Answer")submit_btn = gr.Button("Ask Scooby")submit_btn.click(fn=respond, inputs=text_input, outputs=output)11 unmodified linesclear_btn="🧹 Clear Chat")demo.launch(debug=True)1 unmodified line2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849507 unmodified lines585960616263644 unmodified lines697071727374757677787980818283848511 unmodified lines9798991001011021031041051 unmodified linefrom sentence_transformers import SentenceTransformerimport torchfrom huggingface_hub import InferenceClient# Load and process knowledge basewith open("knowledge.txt", "r", encoding="utf-8") as file:knowledge = file.read()cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]model = SentenceTransformer('all-MiniLM-L6-v2')chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)# Function to retrieve most relevant infodef get_top_chunks(query):query_embedding = model.encode(query, convert_to_tensor=True)query_embedding_normalized = 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_normalized)top_indices = torch.topk(similarities, k=3).indicesreturn [cleaned_chunks[i] for i in top_indices]# Connect to HF inference clientclient = InferenceClient("google/gemma-3-27b-it")# Chatbot functiondef respond(message, history):knowledge_base = get_top_chunks(message)system_message = f"You are a helpful chatbot named Scooby, kind of like the cartoon character but not too much like it. You know a lot about pets and their diets and can only answer questions about pets. Here is what you know: {knowledge_base}"messages = [{"role": "system", "content": system_message}]if history:messages += historymessages.append({"role": "user", "content": message})response = ""for delta in client.chat_completion(messages=messages, max_tokens=2500, stream=True):token = delta.choices[0].delta.get("content", "")response += tokenyield response# Recipe chatbot (simple placeholder)def respond_recipe(message, history):return f"Here's a custom recipe idea for your pet based on: '{message}'. 🐾"# Theme, welcome, topicstheme = gr.themes.Monochrome(primary_hue="teal", secondary_hue="orange")welcome_message = """# Welcome to ScoobyAI7 unmodified lines- Creating recipes for pets based on dietary needs"""# Gradio UIwith gr.Blocks(theme=theme) as demo:gr.Image(value="scooby.png",4 unmodified linesshow_download_button=False,container=False)gr.Markdown(welcome_message)with gr.Row():with gr.Column():gr.Markdown(topics)with gr.Tabs():with gr.Tab("Main Page"):with gr.Row():text_input = gr.Textbox(label="Ask Scooby a question about your pet")output = gr.Textbox(label="Scooby's Answer")submit_btn = gr.Button("Ask Scooby")submit_btn.click(fn=respond, inputs=text_input, outputs=output)11 unmodified linesclear_btn="🧹 Clear Chat")demo.launch(debug=True)
app.py+29-80