This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from neo4j import GraphDatabase | |
| # Declare the connector to the graph database | |
| uri = "bolt://localhost:7687" | |
| driver = GraphDatabase.driver(uri, auth=("neo4j", "pwd"), encrypted=False) | |
| # Build the query to build the relationship | |
| query = f''' | |
| MATCH (u:User), (s:Song) | |
| WHERE u.id = "id of the user" AND s.id = "id of the song" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from kats.consts import TimeSeriesData | |
| def build_kats_timeserie(dfp, column_time = "time", column_value = "value"): | |
| return TimeSeriesData(time=dfp[column_time], value=dfp[column_value]) | |
| kts_test = build_kats_timeserie(dfp_test,"date","value") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from kats import models | |
| # Selection your base model | |
| def build_model(model, kts): | |
| if model == "prophet": | |
| return models.prophet.ProphetModel(kts, params=models.prophet.ProphetParams()) | |
| elif model == "theta": | |
| return models.theta.ThetaModel(kts, params=models.theta.ThetaParams()) | |
| elif model == "holtwinters": |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from time import time | |
| from hyperopt import fmin, tpe, hp, anneal, Trials | |
| import mlflow | |
| from sklearn.metrics import mean_squared_error | |
| import surprise | |
| def evaluate_model(model, dfp_ratings_test): | |
| dfp_evaluation = dfp_ratings_test.copy() | |
| dfp_evaluation["rating_predicted"] = dfp_evaluation.apply(lambda row: compute_ranking(model, str(row["userid"]), str(row["contentid"])), axis=1) | |
| return mean_squared_error(dfp_evaluation["rating"].tolist(), dfp_evaluation["rating_predicted"].tolist(), squared=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def build_recommendations(model, userid, inventory, dfp_items, k=5): | |
| dfp_recommendations = dfp_items[["title", "category", "year", "contentid"]] | |
| dfp_recommendations["contentid"] = dfp_recommendations["contentid"].astype(str) | |
| dfp_recommendations["rating_predicted"] = dfp_recommendations["contentid"].apply(lambda contentid: compute_ranking(model, str(userid), str(contentid))) | |
| dfp_recommendations.sort_values("rating_predicted", ascending=False, inplace=True) | |
| dfp_recommendations = dfp_recommendations.loc[dfp_recommendations["contentid"].isin(inventory) == False] | |
| return dfp_recommendations.head(k).reset_index(drop=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_closest_neighbors(model, entityid, k, type_="item"): | |
| if type_ == "item": | |
| inner_entity_id = model.trainset.to_inner_iid(entityid) | |
| else: | |
| inner_entity_id = model.trainset.to_inner_uid(entityid) | |
| closest_entity_id = model.get_neighbors(inner_entity_id, k) | |
| if type_ == "item": | |
| return [model.trainset.to_raw_iid(id_) for id_ in closest_entity_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for idx, row in dfp_archetypes.iterrows(): | |
| print("ARCHETYPE:", row["userid"]) | |
| inventory_positive = dfp_inventory_positive.loc[row["userid"]] | |
| # Get the candidates | |
| buffer = [] | |
| for contentid in inventory_positive: | |
| closest_contentids = get_closest_neighbors(model_retriever_items, contentid, 10, type_="item") | |
| buffer.extend(closest_contentids) | |
| sp_count_contentids = pd.Series(dict(Counter(buffer))).sort_values(ascending=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import whisper | |
| size_model = "medium" #the type of model in the model card , with .en or not | |
| model = whisper.load_model(size_model, device="cuda") | |
| def get_transcript_local_whisper(model, file, language): | |
| audio = whisper.load_audio(file) | |
| audio = whisper.pad_or_trim(audio) | |
| mel = whisper.log_mel_spectrogram(audio).to(model.device) | |
| result = whisper.decode(model, mel, language=language) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import torch | |
| from transformers import pipeline | |
| device = "cuda:0" if torch.cuda.is_available() else "cpu" | |
| mapping = {"whisper-tiny" : "tiny", "whisper-small" : "small", "whisper-medium" : "medium", "whisper-base" : "base"} | |
| hf_model_name = "whisper-medium" | |
| size_model = mapping[hf_model_name] #tiny, base, small, medium | |
| model = pipeline( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pathlib import Path | |
| from openai import OpenAI | |
| client_openai = OpenAI( | |
| # This is the default and can be omitted | |
| api_key="sk-XXX", | |
| ) | |
| def get_transcript_openai_api(file, language="fr"): | |
| # f = open(file, "rb") |