Spaces:
Sleeping
Sleeping
File size: 4,600 Bytes
050da3e 78b8009 050da3e 78b8009 0e374b2 78b8009 050da3e 78b8009 050da3e 8c9ce47 050da3e 78b8009 e5921df cbc5dc8 7017fb4 78b8009 d103e1b 78b8009 3b463dc 78b8009 3b463dc 4adb088 78b8009 3b463dc 907704a d103e1b 78b8009 90fef10 8ed36b7 8dc2774 5a7a0cc 8dc2774 5a7a0cc 8dc2774 050da3e 78b8009 a9eed4f 8dc2774 eaa9455 34ce21a 1007a99 c4c539c 1007a99 c4c539c 629d540 0567350 f0cdaba b64e603 71b8231 78b8009 f257aba 050da3e 78b8009 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import gradio as gr
from sentence_transformers import SentenceTransformer
import torch
from 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 text
chunks = cleaned_text.split("\n")
# separating the text into one sentence chunks
cleaned_chunks = []
# creating a empty list called cleaned_chunks
for chunk in chunks:
# for every chunk in the chunks list,
stripped_chunk = chunk.strip()
# the chunk is getting stripped
if 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 parameter
query_embedding = model.encode(query, convert_to_tensor=True)
# turning my query to a vector embedding for comparison
query_embedding_normalized = query_embedding / query_embedding.norm()
# normlaize my query to 1: allows for comparison of meaning
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
# normalizing chunks for comparison of meaning
similarities = 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 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) # adding the sentences that are associated with the top indices to the list
return top_chunks
client = 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 and loves helping pet owner, and doesn't talk about specific pet companies or health issues until prompted:{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 = 300, stream = True):
token = messages.choices[0].delta.content
response += token
yield response
theme = theme = gr.themes.Soft(
primary_hue="orange",
secondary_hue="orange",
neutral_hue="emerald",
)
welcome_message = """
# Welcome to ScoobyAI
## We understand that it is difficult to take care of pets, especially after a long day of school or work. That's why we created Scooby, your chatbot assistant for all pet health needs!
"""
topics = """
### Scooby is happy to help you over these topics!
- Explaining pet issues
- Creating feeding schedules
- Creating recipes for pets based on dietary needs
"""
#chatbot = gr.ChatInterface(respond, type = "messages", theme)
with gr.Blocks(theme=theme) as demo:
gr.Image(value="scooby.png", height=450, width = 1536, show_label=False, show_share_button = False, show_download_button = False, container=False)
gr.Markdown(welcome_message)
with gr.Row():
with gr.Column():
gr.Markdown(topics)
with gr.Tabs():
with gr.TabItem("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)
with gr.TabItem("🍲 Recipes"):
gr.Markdown("Ask Scooby about recipes here!")
gr.ChatInterface(fn=respond,title="Pet Recipe Assistant 🍽️",
textbox=gr.Textbox(placeholder="What does your pet need a recipe for?",
label="Recipe Request"))
demo.launch(debug=True)
|