Spaces:
Running
Running
import gradio as gr | |
from pinecone import Pinecone | |
import torch | |
from transformers import AutoTokenizer, AutoModel | |
import pandas as pd | |
import os | |
import openai | |
import textwrap | |
MODEL_NAME = 'sentence-transformers/all-MiniLM-L6-v2' | |
def text_to_embedding(text): | |
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, top_k=1): | |
query_embedding = text_to_embedding(query_text) | |
results = index.query(vector=query_embedding, top_k=top_k, include_metadata=True) | |
matches=results['matches'] | |
resultsdf=pd.DataFrame({'album':[m['id'] for m in matches],\ | |
'artist':[m['metadata']['artist'] for m in matches],\ | |
'score':[m['score'] for m in matches],\ | |
'description':[m['metadata']['description'] for m in matches]}) | |
return resultsdf | |
def motivate_recommendation(album, artist, description, query_text): | |
prompt = (f"Based on the description: '{description}', explain why \"{album}\" by \"{artist}\" " | |
f"matches the query: '{query_text}'.") | |
response = openai.ChatCompletion.create( | |
model="gpt-4", # | |
messages=[ | |
{"role": "system", "content": "You are helping to recommend music albums based on their description."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
explanation = response.choices[0].message['content'].strip() | |
clean_explanation = explanation.replace('\'', '"').replace('\"', '"') | |
wrapped_explanation = textwrap.fill(clean_explanation, width=70) | |
recommendation = (f"May I suggest \"{album}\" by \"{artist}\"? It matches what you were " | |
f"looking for because {wrapped_explanation}") | |
return textwrap.fill(recommendation, width=70) | |
def recommend(user_query: str) -> str: | |
most_similar = find_similar_media(user_query).loc[0] | |
motivation=motivate_recommendation(most_similar.album, most_similar.artist, most_similar.description, user_query).replace("\n"," ") | |
return motivation | |
pc=Pinecone() | |
index = pc.Index(os.environ['INDEX_NAME']) | |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
model = AutoModel.from_pretrained(MODEL_NAME) | |
openai.api_key = os.environ['OPENAI_API_KEY'] | |
recommender = gr.Interface(fn=recommend, inputs="text", outputs="text") | |
recommender.launch() |