| """ |
| import gradio as gr |
| |
| def greet(name): |
| return "Hello " + name + "!!" |
| |
| demo = gr.Interface(fn=greet, inputs="text", outputs="text") |
| demo.launch()""" |
|
|
| import gradio as gr |
| import pandas as pd |
| import torch |
| from sentence_transformers import SentenceTransformer, util |
| import google.generativeai as genai |
| import pathlib |
| import textwrap |
| from IPython.display import display |
| from IPython.display import Markdown |
| import time |
|
|
| |
| url = 'https://raw.githubusercontent.com/TRASJEPS/Vinicunca_AI_Project1/refs/heads/main/_Data-20241116T221029Z-001/Data/Cleaned%20Data/Combined%20Cleaned%20Data.csv' |
| df = pd.read_csv(url) |
|
|
| |
| df["combined"] = ( |
| "Product Name: " + df.Product_Name.str.strip()+"; Brand: " + df.Brand.str.strip() |
| + "; Category: " + df.Product_Category.str.strip() |
| + "; Details: " + df.Product_Details.str.strip() |
| + "; Ingredients: " + df.Ingredients.str.strip() |
| + "; Price: " + df["Cleaned Price"].str.strip() |
| |
| ) |
|
|
| |
| df['combined'] = df['combined'].fillna('') |
|
|
| |
| df['combined'] = df['combined'].astype(str) |
|
|
| |
| df_combined = df.copy() |
| df_combined['combined'] = df_combined['combined'].str.lower() |
|
|
| |
| model = SentenceTransformer('all-MiniLM-L6-v2') |
| if torch.cuda.is_available(): |
| modle = model.to('cuda') |
|
|
| |
| df['embeddings'] = df['combined'].apply(lambda x: model.encode(x)) |
| df["embedding"] = df.combined.apply(lambda x: model.encode(x)) |
|
|
| |
| def gradio_search(query): |
| |
| n = 3 |
|
|
| |
| query_embedding = model.encode(query) |
|
|
| |
| df["similarity"] = df.embedding.apply(lambda x: util.cos_sim(x, query_embedding).item()) |
|
|
| |
| results = df.sort_values("similarity", ascending=False).head(n) |
| resultlist = [] |
|
|
| |
| for r in results.index: |
| resultlist.append({ |
| "Product Name": results.Product_Name[r], |
| "Score": results.similarity[r], |
| "Category": results.Product_Category[r], |
| "Price": results["Cleaned Price"][r], |
| "Details": results.Product_Details[r], |
| "Ingredients": results.Ingredients[r] |
| }) |
| return resultlist |
|
|
| |
| def chatbot_response(history, query): |
| history.append(("User", query)) |
| results = gradio_search(query) |
| |
| if len(results) == 0: |
| reply = "I couldn't find any matching products. Could you provide more details or rephrase your request?" |
| else: |
| reply = "\n".join([f"Product: {r['Product Name']}\nCategory: {r['Category']}\nPrice: {r['Price']}\nDetails: {r['Details']}\n" for r in results]) |
|
|
| history.append(("Vinuca", reply)) |
| return history, reply |
|
|
| |
| with gr.Blocks(title="Vinuca AI") as iface: |
| gr.Markdown("# Vinuca AI, Your Personal Haircare Assistant") |
| chatbot = gr.Chatbot() |
| user_input = gr.Textbox(placeholder="Ask me about haircare products...") |
| clear = gr.Button("Clear") |
|
|
| history = [] |
|
|
| def interact(query): |
| global history |
| history, reply = chatbot_response(history, query) |
| return history |
|
|
| user_input.submit(interact, user_input, chatbot) |
| clear.click(lambda: [], None, chatbot) |
|
|
| """iface = gr.Interface( |
| fn=gradio_search, |
| inputs="text", |
| outputs="json", |
| title="Ulta Hair Recommendation Search", |
| description="Enter your preferences to find matching products!" |
| )""" |
|
|
| |
| if __name__ == "__main__": |
| iface.launch() |
|
|