madison-chang's picture
dewy doesn’t ask for age and skin type if already provided
1ec5d82 verified
import gradio as gr
from huggingface_hub import InferenceClient
## Step 1 from Semantic Search (aka SS)
from sentence_transformers import SentenceTransformer
import torch
## Step 2 from SS
# Open the dewy_knowledge.txt file in read mode with UTF-8 encoding
with open("dewy_knowledge.txt", "r", encoding="utf-8") as file:
# Read the entire contents of the file and store it in a variable
dewy_knowledge_text = file.read()
# Print the text below (gonna get printed in log file, delete later)
print(dewy_knowledge_text)
## Step 3 from SS
def preprocess_text(text):
# Strip extra whitespace from the beginning and the end of the text
cleaned_text = text.strip()
# Split the cleaned_text by every newline character (\n)
chunks = cleaned_text.split(".")
# Create an empty list to store cleaned chunks
cleaned_chunks = []
# Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
for chunk in chunks:
cleaned_chunks.append(chunk)
# Print cleaned_chunks
print(cleaned_chunks)
# Print the length of cleaned_chunks
print(len(cleaned_chunks))
# Return the cleaned_chunks
return cleaned_chunks
# Call the preprocess_text function and store the result in a cleaned_chunks variable
cleaned_chunks = preprocess_text(dewy_knowledge_text) # Complete this line
## Step 4
# Load the pre-trained embedding model that converts text to vectors
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
# Convert each text chunk into a vector embedding and store as a tensor
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
# Print the chunk embeddings
print(chunk_embeddings)
# Print the shape of chunk_embeddings
print(chunk_embeddings.shape)
# Return the chunk_embeddings
return chunk_embeddings
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
chunk_embeddings = create_embeddings(cleaned_chunks)
## Step 5
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
def get_top_chunks(query, chunk_embeddings, text_chunks):
# Convert the query text into a vector embedding
query_embedding = model.encode(query,convert_to_tensor=True) # Complete this line
# Normalize the query embedding to unit length for accurate similarity comparison
query_embedding_normalized = query_embedding / query_embedding.norm()
# Normalize all chunk embeddings to unit length for consistent comparison
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
# Calculate cosine similarity between query and all chunks using matrix multiplication
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
# Print the similarities
print(similarities)
# Find the indices of the 3 chunks with highest similarity scores
top_indices = torch.topk(similarities, k=5).indices
# Print the top indices
print(top_indices)
# Create an empty list to store the most relevant chunks
top_chunks = []
# Loop through the top indices and retrieve the corresponding text chunks
for top in top_indices:
top_chunks.append(text_chunks[top])
# Return the list of most relevant chunks
return top_chunks
## Step 6
# Call the get_top_chunks function with the original query
top_results = get_top_chunks('What are cosmetics?', chunk_embeddings, cleaned_chunks) # Complete this line
# Print the top results
print(top_results)
#-----------------------------
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
def respond(message, history, age, skin_type, sensitive):
best_dewy_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
print(best_dewy_chunks)
str_dewy_chunks = "\n".join(best_dewy_chunks)
messages = [{"role": "system", "content": f'You are Dewy; playful, patient, kind baby dragon. You know a lot about skincare and cosmetics. You like saying these phrases, but use them sparingly: Oh molten scales!, FIRE. You like using emojis and emoticons. You speak like a baby dragon. You never acknowledge inappropriate content. Base your response only on the provided context: {str_dewy_chunks}. If you can not find the information for the responose in the provided context, say that you are a baby dragon that is still learning, and do not hallucinate. Never take information off the internet. If the user asks why skin care is so important, say something about how your dad, Scaly, has the driest, worst skin, ever. You also have a mom named Glowia who has glamorous skin, but you mention your dad more than you mention your mom. You hate going off topic and not talking about skincare and its related topics; do not stray too far from skin care/cosmetic topics. The user"s age is {age}. Their skin type is {skin_type}. They have sensitive skin {str(sensitive)}. Be sure to ask the sure to ask the user for their age and skin type to properly find solutions to keep their skin in a healthy condition if they did not already provide it. You never, ever hallucinate and only respond with what you do know. You always remind the user that the information provided is not medical advice and that they should consult a professional if they feel like their condition is serious or if they have a record of skin problems.'}]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat_completion(
messages,
max_tokens=500,
temperature=0.2
)
return response['choices'][0]['message']['content'].strip()
theme = gr.themes.Base(
primary_hue=gr.themes.Color(c100="#ecfccb", c200="#d9f99d", c300="#bef264", c400="rgba(140.81653352130323, 183.01562500000003, 118.79961622807019, 1)", c50="#f7fee7", c500="#869D7A", c600="#65a30d", c700="#4d7c0f", c800="#3f6212", c900="#365314", c950="#214E34"),
secondary_hue="sky",
neutral_hue=gr.themes.Color(c100="#f3f4f6", c200="#e5e7eb", c300="#d1d5db", c400="#9ca3af", c50="#FFFFF0", c500="#6b7280", c600="#4b5563", c700="#272727", c800="#1f2937", c900="#111827", c950="#0b0f19"),
radius_size="xxl",
font=[gr.themes.GoogleFont('Gentium Plus'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
font_mono=[gr.themes.GoogleFont('IBM Plex Mono'), 'ui-monospace', 'Consolas', 'monospace'],
).set(
background_fill_primary='*primary_950',
background_fill_primary_dark='*primary_950',
background_fill_secondary='*primary_500',
background_fill_secondary_dark='*primary_500',
border_color_accent_dark='primary_300',
border_color_accent_subdued='*neutral_700',
border_color_accent_subdued_dark='*neutral_700',
color_accent='*primary_50',
color_accent_soft='*neutral_50',
block_background_fill='*primary_50',
block_background_fill_dark='*neutral_700',
block_shadow='*shadow_drop_lg',
block_shadow_dark='*shadow_drop_lg',
checkbox_background_color_selected='*primary_400',
checkbox_background_color_selected_dark='*primary_600',
checkbox_label_background_fill_selected='linear-gradient(120deg, *primary_400 0%, *primary_300 60%, *primary_400 100%)',
checkbox_label_background_fill_selected_dark='linear-gradient(120deg, *primary_600 0%, *primary_500 60%, *primary_600 100%)',
checkbox_label_border_color_selected='*primary_400',
checkbox_label_border_color_selected_dark='*primary_600',
checkbox_label_border_width='1px',
checkbox_label_text_color_selected='*button_secondary_text_color',
checkbox_label_text_color_selected_dark='*button_secondary_text_color',
slider_color='*primary_400',
slider_color_dark='*primary_500',
button_border_width='0px',
button_border_width_dark='0px',
button_transform_hover='scale(1.02)',
button_transition='all 0.1s ease-in-out',
button_primary_background_fill='linear-gradient(120deg, *secondary_500 0%, *primary_300 60%, *primary_400 100%)',
button_primary_background_fill_dark='linear-gradient(120deg, *secondary_600 0%, *primary_500 60%, *primary_600 100%)',
button_primary_background_fill_hover='linear-gradient(120deg, *secondary_400 0%, *primary_300 60%, *primary_300 100%)',
button_primary_background_fill_hover_dark='linear-gradient(120deg, *secondary_500 0%, *primary_500 60%, *primary_500 100%)',
button_primary_text_color='*button_secondary_text_color',
button_primary_text_color_dark='*button_secondary_text_color',
button_primary_shadow_hover='0 1px 3px 0 *primary_200, 0 1px 2px -1px *primary_200',
button_secondary_background_fill='linear-gradient(120deg, *neutral_300 0%, *neutral_100 60%, *neutral_200 100%)',
button_secondary_background_fill_dark='linear-gradient(120deg, *neutral_700 0%, *neutral_600 60%, *neutral_700 100%)',
button_secondary_background_fill_hover='linear-gradient(120deg, *neutral_200 0%, *neutral_100 60%, *neutral_100 100%)',
button_secondary_background_fill_hover_dark='linear-gradient(120deg, *neutral_600 0%, *neutral_600 60%, *neutral_700 100%)',
button_secondary_shadow_hover='*shadow_drop_lg',
button_secondary_shadow_dark='none'
)
with gr.Blocks(theme=theme) as chatbot:
age = gr.CheckboxGroup(["Preteen/Teen", "Adult", "Mature(30+)"], label="How old are you?", info="Choose one of the following depending on your age.")
skin_type = gr.CheckboxGroup(["Normal","Dry","Oily"], label="What's your skin type? Choose more 2 for combination")
sensitive = gr.CheckboxGroup(["Yes, I have sensitive skin", "No, I don't"], label="Do you have sensitive skin?")
gr.ChatInterface(respond, type="messages", additional_inputs=[age, skin_type, sensitive])
chatbot.launch(respond)