OHA_gradio / app.py
Shritam's picture
Update app.py
f3fe982
import gradio as gr
import pandas as pd
import numpy as np
import pickle
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
model = SentenceTransformer('paraphrase-distilroberta-base-v1')
# Load the dataset
data = pd.read_csv('data/Recommender_data.csv')
# Load the precomputed embeddings
with open('data/Model/tool_embeddings.pkl', 'rb') as f:
tool_embeddings = pickle.load(f)
# # Load the model from the file using pickle
# with open('data/Model/model.pkl', 'rb') as f:
# model = pickle.load(f)
# def get_sentiment(input_text):
# return sentiment(input_text)
# Define the recommendation function
def recommend(product_idea, model=model):#, model=model
# Load the pre-trained model
#model = SentenceTransformer('paraphrase-distilroberta-base-v1')
# Compute the embedding for the product idea
product_embedding = model.encode([product_idea])
# Compute the cosine similarity between the product idea vector and all tool vectors
cosine_sim_product = cosine_similarity(product_embedding, tool_embeddings)
# Sort the results in descending order and get the indices of the most similar tools
indices = np.argsort(cosine_sim_product)[0][::-1]
# Return the top 5 most similar tools
recommendations = {data.iloc[indices[i]]['Tools']: {"Description":data.iloc[indices[i]]['Description'],"URL":data.iloc[indices[i]]['URL'],"Logo":data.iloc[indices[i]]['Logo']} for i in range(4)}
return recommendations
iface = gr.Interface(fn = recommend,
inputs = "text",
outputs = ['text'],
title = 'OHA',
description="Get OHA Recomandation for the given input")
iface.launch(inline = False)