media_assistant / app.py
uijnbh's picture
Rebuilt S3 interface
de09d24
Raw History Blame Contribute Delete
6.35 kB
"""
Charity store assistant app
"""
import os
from io import StringIO
from datetime import datetime
import pandas as pd
from pandas import DataFrame
import gradio as gr
from pinecone import Pinecone
import torch
from transformers import AutoTokenizer, AutoModel
from dotenv import load_dotenv
import anthropic
import boto3
TRANSFORMER_MODEL = "sentence-transformers/all-mpnet-base-v2"
LLM = "claude-3-haiku-20240307"
replace_dict = {"/'": "", '"': "", "'": "", "\n\n": " "}
SIMILAR_MEDIA_LIST_LENGTH = 5
RESPONSE_WORD_LIMIT = 50
SYSTEM = f"You are helping to recommend music, movies or books based on a description. \
Your answer must be limited to {RESPONSE_WORD_LIMIT} words."
pc = Pinecone(api_key=os.environ["PINECONE_KEY"])
index = pc.Index("product-recommender")
tokenizer = AutoTokenizer.from_pretrained(TRANSFORMER_MODEL)
model = AutoModel.from_pretrained(TRANSFORMER_MODEL)
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def text_to_embedding(text: str):
"""
Convert text to vector embedding
"""
inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
embeddings = outputs.last_hidden_state.mean(dim=1).numpy()
return embeddings.flatten().tolist()
def find_similar_media(query_text: str, top_k=SIMILAR_MEDIA_LIST_LENGTH):
"""
Find media that is similar to the query
"""
query_embedding = text_to_embedding(query_text)
matches = index.query(
vector=query_embedding,
top_k=top_k,
include_metadata=True,
filter={"vector_type": "test"},
)["matches"]
df = pd.DataFrame(
{"barcode": [m["id"] for m in matches], "score": [m["score"] for m in matches]}
)
for col in ["artist", "title", "media_type", "description", "vector_type"]:
df[col] = [m["metadata"][col] for m in matches]
return df
def get_llm_response(input_text: str) -> str:
"""
Retrieve formatted response from the LLM
"""
response = client.messages.create(
model=LLM,
max_tokens=1000,
temperature=0,
system=SYSTEM,
messages=[{"role": "user", "content": [{"type": "text", "text": input_text}]}],
)
response = response.content[0].text
for k, v in replace_dict.items():
response = response.replace(k, v)
return response
def motivate_recommendation(title: str, artist: str, description: str, query_text: str):
"""
Ask the LLM to motivate why the most similar query matches the search string
"""
no_artist = artist == "(none)"
if no_artist:
prompt = (
f"Based on the description: '{description}', explain why \"{title} "
f"matches the query: '{query_text}'."
)
else:
prompt = (
f'Based on the description: \'{description}\', explain why "{title}" by "{artist}" '
f"matches the query: '{query_text}'."
)
response = get_llm_response(prompt)
if no_artist:
recommendation = (
f'May I suggest "{title}" by "{artist}"? It matches what you were '
f"looking for because {response}"
)
else:
recommendation = (
f'May I suggest "{title}"? It matches what you were '
f"looking for because {response}"
)
return recommendation
def log_dialogue(dialogue: dict):
"""
Log a user query and LLM response dialogue pair to a database
"""
session = boto3.Session()
s3 = session.resource("s3")
bucket_name, s3_file_name = "charityshop-media-assistant", "chat_history.csv"
chat_history = pd.read_csv(
StringIO(
s3.Object(bucket_name, s3_file_name).get()["Body"].read().decode("utf-8")
)
)
timestamp_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
new_dialogue = pd.DataFrame(
{
"store_name": [dialogue["ip_address"], dialogue["ip_address"]],
"location": [dialogue["url"], dialogue["url"]],
"recommendation_barcode": ["(none)", dialogue["recommendation_barcode"]],
"role": ["user", "assistant"],
"chat_text": [dialogue["user_query"], dialogue["motivation"]],
"session_uuid": [dialogue["session_uuid"], dialogue["session_uuid"]],
"timestamp": [timestamp_str, timestamp_str],
}
)
chat_history = pd.concat([chat_history, new_dialogue], ignore_index=True)
csv_buffer = StringIO()
chat_history.to_csv(csv_buffer, index=False)
s3.Object(bucket_name, s3_file_name).put(Body=csv_buffer.getvalue())
def combined_recommendation(
user_query: str, request: gr.Request
) -> tuple[str, DataFrame]:
"""
Wrapper function to receive user query and return
the motivation for the best match and a table with the top 5 matches
"""
session_hash = request.session_hash if request else "(none)"
ip_address = request.client.host if request else "(none)"
url = request.url if request else "(none)"
top_matches = find_similar_media(user_query)
most_similar = top_matches.loc[0]
motivation = motivate_recommendation(
most_similar.title, most_similar.artist, most_similar.description, user_query
)
dialogue = {
"user_query": user_query,
"motivation": motivation,
"recommendation_barcode": top_matches.loc[0, "barcode"],
"session_uuid": session_hash,
"ip_address": ip_address,
"url": url,
}
log_dialogue(dialogue)
return motivation, top_matches[["media_type", "artist", "title"]]
if __name__ == "__main__":
load_dotenv()
with gr.Blocks() as multi_output_app:
input_textbox = gr.Textbox(
show_label=False, placeholder="Type your query here..."
)
submit_button = gr.Button("Submit")
with gr.Tabs():
with gr.TabItem("Best Recommendation"):
motivation_output = gr.Textbox(show_label=False)
with gr.TabItem(f"Top {str(SIMILAR_MEDIA_LIST_LENGTH)} Matches"):
table_output = gr.DataFrame(headers=["Result"])
submit_button.click(
combined_recommendation,
inputs=input_textbox,
outputs=[motivation_output, table_output],
)
multi_output_app.launch()