-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (127 loc) · 4.68 KB
/
main.py
File metadata and controls
151 lines (127 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from dotenv import load_dotenv
import traceback
load_dotenv(".env")
import os
from pathlib import Path
from typing import Union
from fastapi import FastAPI
from fastapi import File, UploadFile
from pymongo import MongoClient
from db import db
from datetime import datetime
from pydantic import BaseModel
import embeddings
from typing import List, Dict
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "server is running successfully"}
# create music directory to store tenporary music files
Path("./music").mkdir(parents=True, exist_ok=True)
class ResponseData(BaseModel):
message: str = "Successfully uploaded"
status_code: int = 200
error: str = None
similarity_scores: Dict[str, float] = {"0": 0.0, "1": 0.0}
similarity_scores_song_ids: Dict[str, str] = {"0": "song_id_1", "1": "song_id_2"}
similarity_scores_song_embedding_keys: Dict[str, int] = {"0": 0, "1": 0}
# embeddings: List[List[float]] = [[0.5, 0.95, 0.75]]
song_id: str = "filename.wav"
def __init__(
self,
**data: Union[
str, Dict[str, float], Dict[str, str], Dict[str, int], List[List[float]]
],
):
super().__init__(**data)
@app.post("/embeddings")
def upload(
file: UploadFile = File(...),
song_id: str = None,
ifps_hash: str = None,
artist_id: str = None,
):
try:
# check if all the required parameters are passed
if song_id is None or ifps_hash is None or artist_id is None:
return {
"message": "upload failed",
"status_code": 400,
"error": "Please Add All required Params.",
}
# Check if the file is an audio file
file_type = file.content_type
if file_type.split("/")[0] != "audio":
# return an error response if the uploaded file is not an audio file
return {
"message": "upload failed",
"status_code": 400,
"error": "The uploaded file is not a valid audio file.",
}
# Reset file cursor to read the file again
file.file.seek(0)
# Convert music files to WAV format
if not file.filename.lower().endswith(".wav"):
filename = "./music/" + file.filename.rsplit(".", 1)[0] + ".wav"
else:
filename = "./music/" + file.filename
# Save the file in the music directory
with open(filename, "wb") as f:
f.write(file.file.read())
# get all songs wiht oldest first order except the song which is being uploaded
all_songs = db.songs.find(
{"song_id": {"$ne": song_id}}, sort=[("created_at", 1)]
)
# process the uploaded song
music_info = embeddings.get_embeddings_and_calculate_similarity_with_prev_songs(
filename, all_songs
)
# Save Embeddings to DB
db.songs.find_one_and_update(
{"song_id": song_id},
{
"$set": {
"embeddings": music_info["embeddings"],
"updated_at": datetime.now(),
"ifps_hash": ifps_hash,
"artist_id": artist_id,
"similarity_scores": music_info["similarity_scores"],
"similarity_scores_song_ids": music_info[
"similarity_scores_song_ids"
],
"similarity_scores_song_embedding_keys": music_info[
"similarity_scores_song_embedding_keys"
],
},
"$setOnInsert": {
"created_at": datetime.now(),
},
},
upsert=True,
)
# delete the file
os.remove(filename)
# generate and return the response
response = {
"message": f"Successfully uploaded {file.filename}",
"status_code": 200,
"similarity_scores": music_info["similarity_scores"],
"similarity_scores_song_ids": music_info["similarity_scores_song_ids"],
"similarity_scores_song_embedding_keys": music_info[
"similarity_scores_song_embedding_keys"
],
# embeddings=music_info["embeddings"],
"song_id": song_id,
}
return response
except Exception as e:
traceback.print_exc()
# Handle the exception and print its details
print(f"An exception occurred: {type(e).__name__} - {e}")
return {
"message": "There was an error uploading the file",
"status_code": 400,
"error": str(f"{type(e).__name__} - {e}"),
}
finally:
file.file.close()